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

@@ -102,6 +102,11 @@ typedef struct CSSNode {
static void _CSSNodeMarkDirty(const CSSNodeRef node);
CSSMalloc gCSSMalloc = &malloc;
CSSCalloc gCSSCalloc = &calloc;
CSSRealloc gCSSRealloc = &realloc;
CSSFree gCSSFree = &free;
#ifdef ANDROID
#include <android/log.h>
static int _csslayoutAndroidLog(CSSLogLevel level, const char *format, va_list args) {
@@ -178,7 +183,7 @@ static inline float computedEdgeValue(const float edges[CSSEdgeCount],
static int32_t gNodeInstanceCount = 0;
CSSNodeRef CSSNodeNew(void) {
const CSSNodeRef node = calloc(1, sizeof(CSSNode));
const CSSNodeRef node = gCSSCalloc(1, sizeof(CSSNode));
CSS_ASSERT(node, "Could not allocate memory for node");
gNodeInstanceCount++;
@@ -199,7 +204,7 @@ void CSSNodeFree(const CSSNodeRef node) {
}
CSSNodeListFree(node->children);
free(node);
gCSSFree(node);
gNodeInstanceCount--;
}
@@ -2523,3 +2528,26 @@ void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature, bool
bool CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeature feature) {
return experimentalFeatures[feature];
}
void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,
CSSCalloc cssCalloc,
CSSRealloc cssRealloc,
CSSFree cssFree) {
CSS_ASSERT(gNodeInstanceCount == 0,
"Cannot set memory functions: all node must be freed first");
CSS_ASSERT((cssMalloc == NULL && cssCalloc == NULL && cssRealloc == NULL && cssFree == NULL) ||
(cssMalloc != NULL && cssCalloc != NULL && cssRealloc != NULL && cssFree != NULL),
"Cannot set memory functions: functions must be all NULL or Non-NULL");
if (cssMalloc == NULL || cssCalloc == NULL || cssRealloc == NULL || cssFree == NULL) {
gCSSMalloc = &malloc;
gCSSCalloc = &calloc;
gCSSRealloc = &realloc;
gCSSFree = &free;
} else {
gCSSMalloc = cssMalloc;
gCSSCalloc = cssCalloc;
gCSSRealloc = cssRealloc;
gCSSFree = cssFree;
}
}