Add PhantomRef based YogaNode subclass

Summary: Adds a subclass of `YogaNodeJNIBase` that uses `PhantomReference` for deallocating native memory rather than `Object#finalize()`. This should help making garbage collection more efficient.

Reviewed By: amir-shalem

Differential Revision: D16182667

fbshipit-source-id: d310fdb6af184168c43462b24f5e18ab5d0d7ad0
This commit is contained in:
David Aurelio
2019-07-19 17:16:46 -07:00
committed by Facebook Github Bot
parent 4e4ef06de1
commit 8c0eed3c75
6 changed files with 88 additions and 29 deletions

View File

@@ -0,0 +1,34 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
package com.facebook.yoga;
public class YogaNodeJNIFinalizer extends YogaNodeJNIBase {
public YogaNodeJNIFinalizer() {
super();
}
public YogaNodeJNIFinalizer(YogaConfig config) {
super(config);
}
@Override
protected void finalize() throws Throwable {
try {
freeNatives();
} finally {
super.finalize();
}
}
public void freeNatives() {
if (mNativePointer != 0) {
long nativePointer = mNativePointer;
mNativePointer = 0;
YogaNative.jni_YGNodeFree(nativePointer);
}
}
}