From d4e00b31c55d2dda031bbfb68f5589eb230d749a Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Mon, 6 Nov 2023 23:12:19 -0800 Subject: [PATCH] Remove Yoga-internal.h (#1452) Summary: X-link: https://github.com/facebook/react-native/pull/41346 This removes the last remnant from `Yoga-interna.h`, `YGNodeDellocate()`. The API is renamed to `YGNodeFinalize` to give it the explicit purpose of freeing the node from a garbage collector, and made public with that documented contract. With that, every top-level header is now a public API, and Yoga's JNI bindings do not need to rely on private headers anymore. Changelog: [Internal] Reviewed By: joevilches Differential Revision: D51014340 --- Yoga.podspec | 2 +- java/com/facebook/yoga/YogaNative.java | 2 +- .../facebook/yoga/YogaNodeJNIFinalizer.java | 2 +- java/jni/YGJNIVanilla.cpp | 15 +++++-------- tests/YGRoundingFunctionTest.cpp | 1 - yoga/Yoga-internal.h | 22 ------------------- yoga/Yoga.cpp | 7 +++--- yoga/Yoga.h | 6 +++++ 8 files changed, 19 insertions(+), 38 deletions(-) delete mode 100644 yoga/Yoga-internal.h diff --git a/Yoga.podspec b/Yoga.podspec index e8e4dc0cc1..0dd171e297 100644 --- a/Yoga.podspec +++ b/Yoga.podspec @@ -41,7 +41,7 @@ Pod::Spec.new do |spec| spec.source_files = 'yoga/**/*.{h,cpp}' spec.header_mappings_dir = 'yoga' - public_header_files = 'yoga/{Yoga,YGEnums,YGMacros,YGValue}.h' + public_header_files = 'yoga/*.h' spec.public_header_files = public_header_files all_header_files = 'yoga/**/*.h' diff --git a/java/com/facebook/yoga/YogaNative.java b/java/com/facebook/yoga/YogaNative.java index 6ae00f0f2e..a0b7c165f1 100644 --- a/java/com/facebook/yoga/YogaNative.java +++ b/java/com/facebook/yoga/YogaNative.java @@ -31,7 +31,7 @@ public class YogaNative { // YGNode related static native long jni_YGNodeNewJNI(); static native long jni_YGNodeNewWithConfigJNI(long configPointer); - static native void jni_YGNodeDeallocateJNI(long nativePointer); + static native void jni_YGNodeFinalizeJNI(long nativePointer); static native void jni_YGNodeResetJNI(long nativePointer); static native void jni_YGNodeInsertChildJNI(long nativePointer, long childPointer, int index); static native void jni_YGNodeSwapChildJNI(long nativePointer, long childPointer, int index); diff --git a/java/com/facebook/yoga/YogaNodeJNIFinalizer.java b/java/com/facebook/yoga/YogaNodeJNIFinalizer.java index ab617c7f16..23cf2a31e5 100644 --- a/java/com/facebook/yoga/YogaNodeJNIFinalizer.java +++ b/java/com/facebook/yoga/YogaNodeJNIFinalizer.java @@ -29,7 +29,7 @@ public void freeNatives() { if (mNativePointer != 0) { long nativePointer = mNativePointer; mNativePointer = 0; - YogaNative.jni_YGNodeDeallocateJNI(nativePointer); + YogaNative.jni_YGNodeFinalizeJNI(nativePointer); } } } diff --git a/java/jni/YGJNIVanilla.cpp b/java/jni/YGJNIVanilla.cpp index e01aa24875..b3b564866e 100644 --- a/java/jni/YGJNIVanilla.cpp +++ b/java/jni/YGJNIVanilla.cpp @@ -6,6 +6,7 @@ */ #include "YGJNIVanilla.h" +#include #include #include #include @@ -16,9 +17,6 @@ #include "common.h" #include "jni.h" -#include -#include - using namespace facebook; using namespace facebook::yoga; using namespace facebook::yoga::vanillajni; @@ -190,12 +188,12 @@ static void jni_YGConfigSetLoggerJNI( } static void -jni_YGNodeDeallocateJNI(JNIEnv* /*env*/, jobject /*obj*/, jlong nativePointer) { +jni_YGNodeFinalizeJNI(JNIEnv* /*env*/, jobject /*obj*/, jlong nativePointer) { if (nativePointer == 0) { return; } const YGNodeRef node = _jlong2YGNodeRef(nativePointer); - YGNodeDeallocate(node); + YGNodeFinalize(node); } static void @@ -637,9 +635,8 @@ static YGSize YGJNIMeasureFunc( uint32_t wBits = 0xFFFFFFFF & (measureResult >> 32); uint32_t hBits = 0xFFFFFFFF & measureResult; - - const float measuredWidth = yoga::bit_cast(wBits); - const float measuredHeight = yoga::bit_cast(hBits); + float measuredWidth = std::bit_cast(wBits); + float measuredHeight = std::bit_cast(hBits); return YGSize{measuredWidth, measuredHeight}; } else { @@ -751,7 +748,7 @@ static JNINativeMethod methods[] = { (void*)jni_YGConfigSetLoggerJNI}, {"jni_YGNodeNewJNI", "()J", (void*)jni_YGNodeNewJNI}, {"jni_YGNodeNewWithConfigJNI", "(J)J", (void*)jni_YGNodeNewWithConfigJNI}, - {"jni_YGNodeDeallocateJNI", "(J)V", (void*)jni_YGNodeDeallocateJNI}, + {"jni_YGNodeFinalizeJNI", "(J)V", (void*)jni_YGNodeFinalizeJNI}, {"jni_YGNodeResetJNI", "(J)V", (void*)jni_YGNodeResetJNI}, {"jni_YGNodeInsertChildJNI", "(JJI)V", (void*)jni_YGNodeInsertChildJNI}, {"jni_YGNodeSwapChildJNI", "(JJI)V", (void*)jni_YGNodeSwapChildJNI}, diff --git a/tests/YGRoundingFunctionTest.cpp b/tests/YGRoundingFunctionTest.cpp index 7fb2d30bdb..9639a92088 100644 --- a/tests/YGRoundingFunctionTest.cpp +++ b/tests/YGRoundingFunctionTest.cpp @@ -6,7 +6,6 @@ */ #include -#include #include TEST(YogaTest, rounding_value) { diff --git a/yoga/Yoga-internal.h b/yoga/Yoga-internal.h deleted file mode 100644 index 7c1caaa378..0000000000 --- a/yoga/Yoga-internal.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include -#include - -#include - -YG_EXTERN_C_BEGIN - -// Deallocates a Yoga Node. Unlike YGNodeFree, does not remove the node from -// its parent or children. -YG_EXPORT void YGNodeDeallocate(YGNodeRef node); - -YG_EXTERN_C_END diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 5109223fe3..1909183c68 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -#include #include #include @@ -136,10 +135,12 @@ void YGNodeFree(const YGNodeRef nodeRef) { } node->clearChildren(); - YGNodeDeallocate(node); + + Event::publish(node, {YGNodeGetConfig(node)}); + delete resolveRef(node); } -void YGNodeDeallocate(const YGNodeRef node) { +void YGNodeFinalize(const YGNodeRef node) { Event::publish(node, {YGNodeGetConfig(node)}); delete resolveRef(node); } diff --git a/yoga/Yoga.h b/yoga/Yoga.h index 86f09fa0c4..ce2d4ec2d6 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -59,6 +59,12 @@ YG_EXPORT void YGNodeFreeRecursiveWithCleanupFunc( YGNodeRef node, YGNodeCleanupFunc cleanup); YG_EXPORT void YGNodeFreeRecursive(YGNodeRef node); + +// Frees the Yoga node without disconnecting it from its owner or children. +// Allows garbage collecting Yoga nodes in parallel when the entire tree is +// unrechable. +YG_EXPORT void YGNodeFinalize(YGNodeRef node); + YG_EXPORT void YGNodeReset(YGNodeRef node); YG_EXPORT void YGNodeInsertChild(YGNodeRef node, YGNodeRef child, size_t index);