forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
2b9f7ba
commit 5ebc0ab
Showing
4 changed files
with
86 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
ReactAndroid/src/main/java/com/facebook/yoga/YogaNodeJNIFinalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
ReactAndroid/src/main/java/com/facebook/yoga/YogaNodeJNIPhantomRefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* 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; | ||
|
||
import com.facebook.jni.DestructorThread; | ||
|
||
public class YogaNodeJNIPhantomRefs extends YogaNodeJNIBase { | ||
public YogaNodeJNIPhantomRefs() { | ||
super(); | ||
registerPhantomRef(this, mNativePointer); | ||
} | ||
|
||
public YogaNodeJNIPhantomRefs(YogaConfig config) { | ||
super(config); | ||
registerPhantomRef(this, mNativePointer); | ||
} | ||
|
||
@Override | ||
public YogaNodeJNIPhantomRefs cloneWithoutChildren() { | ||
YogaNodeJNIPhantomRefs clone = (YogaNodeJNIPhantomRefs) super.cloneWithoutChildren(); | ||
registerPhantomRef(clone, clone.mNativePointer); | ||
return clone; | ||
} | ||
|
||
private static final void registerPhantomRef(YogaNode node, final long nativePointer) { | ||
new DestructorThread.Destructor(node) { | ||
private long mNativePointer = nativePointer; | ||
@Override | ||
protected void destruct() { | ||
if (mNativePointer != 0) { | ||
YogaNative.jni_YGNodeFree(mNativePointer); | ||
mNativePointer = 0; | ||
} | ||
} | ||
}; | ||
} | ||
} |