Introduce CSSLayoutSetMemoryFuncs

Summary:
- Add CSSLayoutSetMemoryFuncs function which allows application to replace malloc, calloc, realloc and free with application's functions, like zalloc and zfree of zlib.
- Fixed memory leaks in tests.
- Close #247

For example, to use dlmalloc with USE_DL_PREFIX

    CSSLayoutSetMemoryFuncs(&dlmalloc, &dlcalloc, &dlrealloc, &dlfree);

Reviewed By: emilsjolander

Differential Revision: D4178386

fbshipit-source-id: a79dbdaf82a512f42cc43f99dbc49faba296903b
This commit is contained in:
Kazuki Sakamoto
2016-11-15 20:20:09 -08:00
committed by Facebook Github Bot
parent 667858990c
commit ef81d4b0c7
6 changed files with 127 additions and 8 deletions

View File

@@ -9,6 +9,10 @@
#include "CSSNodeList.h"
extern CSSMalloc gCSSMalloc;
extern CSSRealloc gCSSRealloc;
extern CSSFree gCSSFree;
struct CSSNodeList {
uint32_t capacity;
uint32_t count;
@@ -16,12 +20,12 @@ struct CSSNodeList {
};
CSSNodeListRef CSSNodeListNew(const uint32_t initialCapacity) {
const CSSNodeListRef list = malloc(sizeof(struct CSSNodeList));
const CSSNodeListRef list = gCSSMalloc(sizeof(struct CSSNodeList));
CSS_ASSERT(list != NULL, "Could not allocate memory for list");
list->capacity = initialCapacity;
list->count = 0;
list->items = malloc(sizeof(CSSNodeRef) * list->capacity);
list->items = gCSSMalloc(sizeof(CSSNodeRef) * list->capacity);
CSS_ASSERT(list->items != NULL, "Could not allocate memory for items");
return list;
@@ -29,8 +33,8 @@ CSSNodeListRef CSSNodeListNew(const uint32_t initialCapacity) {
void CSSNodeListFree(const CSSNodeListRef list) {
if (list) {
free(list->items);
free(list);
gCSSFree(list->items);
gCSSFree(list);
}
}
@@ -56,7 +60,7 @@ void CSSNodeListInsert(CSSNodeListRef *listp, const CSSNodeRef node, const uint3
if (list->count == list->capacity) {
list->capacity *= 2;
list->items = realloc(list->items, sizeof(CSSNodeRef) * list->capacity);
list->items = gCSSRealloc(list->items, sizeof(CSSNodeRef) * list->capacity);
CSS_ASSERT(list->items != NULL, "Could not extend allocation for items");
}