Introduce CSSLayoutSetMemoryFuncs

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 a07abe7188
commit e864c40be8
6 changed files with 98 additions and 14 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;
}
}

View File

@ -47,6 +47,11 @@ typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node,
typedef void (*CSSPrintFunc)(CSSNodeRef node);
typedef int (*CSSLogger)(CSSLogLevel level, const char *format, va_list args);
typedef void *(*CSSMalloc)(size_t size);
typedef void *(*CSSCalloc)(size_t count, size_t size);
typedef void *(*CSSRealloc)(void *ptr, size_t size);
typedef void (*CSSFree)(void *ptr);
// CSSNode
WIN_EXPORT CSSNodeRef CSSNodeNew(void);
WIN_EXPORT void CSSNodeInit(const CSSNodeRef node);
@ -156,4 +161,9 @@ WIN_EXPORT void CSSLog(CSSLogLevel level, const char *message, ...);
WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature, bool enabled);
WIN_EXPORT bool CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeature feature);
WIN_EXPORT void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,
CSSCalloc cssCalloc,
CSSRealloc cssRealloc,
CSSFree cssFree);
CSS_EXTERN_C_END

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");
}

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;
}
}

View File

@ -47,6 +47,11 @@ typedef CSSSize (*CSSMeasureFunc)(CSSNodeRef node,
typedef void (*CSSPrintFunc)(CSSNodeRef node);
typedef int (*CSSLogger)(CSSLogLevel level, const char *format, va_list args);
typedef void *(*CSSMalloc)(size_t size);
typedef void *(*CSSCalloc)(size_t count, size_t size);
typedef void *(*CSSRealloc)(void *ptr, size_t size);
typedef void (*CSSFree)(void *ptr);
// CSSNode
WIN_EXPORT CSSNodeRef CSSNodeNew(void);
WIN_EXPORT void CSSNodeInit(const CSSNodeRef node);
@ -156,4 +161,9 @@ WIN_EXPORT void CSSLog(CSSLogLevel level, const char *message, ...);
WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature, bool enabled);
WIN_EXPORT bool CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeature feature);
WIN_EXPORT void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,
CSSCalloc cssCalloc,
CSSRealloc cssRealloc,
CSSFree cssFree);
CSS_EXTERN_C_END

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");
}