From 8a45ed96713804d982ff0be8156e56e3869b26cf Mon Sep 17 00:00:00 2001 From: Kazuki Sakamoto Date: Mon, 10 Apr 2017 14:22:23 -0700 Subject: [PATCH] Add YGConfigGetInstanceCount Summary: - depends on #496 - For memory leak unit test - Expose the API for C# Closes https://github.com/facebook/yoga/pull/497 Reviewed By: emilsjolander Differential Revision: D4796190 Pulled By: splhack fbshipit-source-id: 99e4e78e8dfb3d459cf6cd7103ab252c3748e5a6 --- csharp/Facebook.Yoga/Native.cs | 3 +++ csharp/Facebook.Yoga/YogaConfig.cs | 5 +++++ yoga/Yoga.c | 10 +++++++++- yoga/Yoga.h | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/csharp/Facebook.Yoga/Native.cs b/csharp/Facebook.Yoga/Native.cs index 1fa960151b..9ec74145d9 100644 --- a/csharp/Facebook.Yoga/Native.cs +++ b/csharp/Facebook.Yoga/Native.cs @@ -138,6 +138,9 @@ public static extern void YGInteropSetLogger( [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] public static extern int YGNodeGetInstanceCount(); + [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] + public static extern int YGConfigGetInstanceCount(); + [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] public static extern void YGConfigSetExperimentalFeatureEnabled( YGConfigHandle config, diff --git a/csharp/Facebook.Yoga/YogaConfig.cs b/csharp/Facebook.Yoga/YogaConfig.cs index 75ab288181..c35773ff75 100644 --- a/csharp/Facebook.Yoga/YogaConfig.cs +++ b/csharp/Facebook.Yoga/YogaConfig.cs @@ -63,5 +63,10 @@ public float PointScaleFactor Native.YGConfigSetPointScaleFactor(_ygConfig, value); } } + + public static int GetInstanceCount() + { + return Native.YGConfigGetInstanceCount(); + } } } diff --git a/yoga/Yoga.c b/yoga/Yoga.c index 4222c230cd..6a550206af 100644 --- a/yoga/Yoga.c +++ b/yoga/Yoga.c @@ -308,6 +308,7 @@ static inline float YGResolveValueMargin(const YGValue *const value, const float } int32_t gNodeInstanceCount = 0; +int32_t gConfigInstanceCount = 0; WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config) { const YGNodeRef node = gYGMalloc(sizeof(YGNode)); @@ -373,15 +374,21 @@ int32_t YGNodeGetInstanceCount(void) { return gNodeInstanceCount; } +int32_t YGConfigGetInstanceCount(void) { + return gConfigInstanceCount; +} + YGConfigRef YGConfigNew(void) { const YGConfigRef config = gYGMalloc(sizeof(YGConfig)); YG_ASSERT(config, "Could not allocate memory for config"); + gConfigInstanceCount++; memcpy(config, &gYGConfigDefaults, sizeof(YGConfig)); return config; } void YGConfigFree(const YGConfigRef config) { gYGFree(config); + gConfigInstanceCount--; } static void YGNodeMarkDirtyInternal(const YGNodeRef node) { @@ -3426,7 +3433,8 @@ bool YGConfigGetUseWebDefaults(const YGConfigRef config) { } void YGSetMemoryFuncs(YGMalloc ygmalloc, YGCalloc yccalloc, YGRealloc ygrealloc, YGFree ygfree) { - YG_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first"); + YG_ASSERT(gNodeInstanceCount == 0 && gConfigInstanceCount == 0, + "Cannot set memory functions: all node must be freed first"); YG_ASSERT((ygmalloc == NULL && yccalloc == NULL && ygrealloc == NULL && ygfree == NULL) || (ygmalloc != NULL && yccalloc != NULL && ygrealloc != NULL && ygfree != NULL), "Cannot set memory functions: functions must be all NULL or Non-NULL"); diff --git a/yoga/Yoga.h b/yoga/Yoga.h index d5c5258a7d..86b63dedb1 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -228,6 +228,7 @@ WIN_EXPORT void YGConfigSetPointScaleFactor(const YGConfigRef config, const floa // YGConfig WIN_EXPORT YGConfigRef YGConfigNew(void); WIN_EXPORT void YGConfigFree(const YGConfigRef config); +WIN_EXPORT int32_t YGConfigGetInstanceCount(void); WIN_EXPORT void YGConfigSetExperimentalFeatureEnabled(const YGConfigRef config, const YGExperimentalFeature feature,