Skip to content

Commit cb4c1e7

Browse files
committed
Remove PyTruffleObjectFree node
1 parent 88224cf commit cb4c1e7

File tree

5 files changed

+49
-151
lines changed

5 files changed

+49
-151
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ private void freeSingletonNativeWrappers(HandleContext handleContext) {
496496
if (singletonNativeWrapper.ref != null) {
497497
CApiTransitions.nativeStubLookupRemove(handleContext, singletonNativeWrapper.ref);
498498
}
499-
PyTruffleObjectFree.releaseNativeWrapperUncached(singletonNativeWrapper);
499+
CApiTransitions.releaseNativeWrapperUncached(singletonNativeWrapper);
500500
}
501501
}
502502

@@ -560,7 +560,7 @@ private void freeSmallInts(HandleContext handleContext) {
560560
if (wrapper.ref != null) {
561561
CApiTransitions.nativeStubLookupRemove(handleContext, wrapper.ref);
562562
}
563-
PyTruffleObjectFree.releaseNativeWrapperUncached(wrapper);
563+
CApiTransitions.releaseNativeWrapperUncached(wrapper);
564564
}
565565
}
566566

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/PyTruffleObjectFree.java

Lines changed: 0 additions & 146 deletions
This file was deleted.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,46 @@ private static void releaseNativeObjects(PythonContext context, NativeObjectRefe
574574
}
575575
}
576576

577+
@TruffleBoundary
578+
public static void releaseNativeWrapperUncached(PythonNativeWrapper nativeWrapper) {
579+
releaseNativeWrapper(nativeWrapper, FreeNode.getUncached());
580+
}
581+
582+
/**
583+
* Releases a native wrapper. This requires to remove the native wrapper from any lookup tables
584+
* and to free potentially allocated native resources. If native wrappers receive
585+
* {@code toNative}, either a <it>handle pointer</it> is allocated or some off-heap memory is
586+
* allocated. This method takes care of that and will also free any off-heap memory.
587+
*/
588+
public static void releaseNativeWrapper(PythonNativeWrapper nativeWrapper, FreeNode freeNode) {
589+
590+
// If wrapper already received toNative, release the handle or free the native memory.
591+
if (nativeWrapper.isNative()) {
592+
long nativePointer = nativeWrapper.getNativePointer();
593+
if (LOGGER.isLoggable(Level.FINE)) {
594+
LOGGER.fine(PythonUtils.formatJString("Freeing pointer: 0x%x (wrapper: %s ;; object: %s)", nativePointer, nativeWrapper, nativeWrapper.getDelegate()));
595+
}
596+
if (HandlePointerConverter.pointsToPyHandleSpace(nativePointer)) {
597+
// In this case, we are up to free a native object stub.
598+
assert tableEntryRemoved(PythonContext.get(freeNode).nativeContext, nativeWrapper);
599+
nativePointer = HandlePointerConverter.pointerToStub(nativePointer);
600+
} else {
601+
nativeLookupRemove(PythonContext.get(freeNode).nativeContext, nativePointer);
602+
}
603+
freeNode.free(nativePointer);
604+
}
605+
}
606+
607+
private static boolean tableEntryRemoved(HandleContext context, PythonNativeWrapper nativeWrapper) {
608+
PythonObjectReference ref = nativeWrapper.ref;
609+
if (ref != null) {
610+
int id = ref.getHandleTableIndex();
611+
return id <= 0 || nativeStubLookupGet(context, nativeWrapper.getNativePointer(), id) == null;
612+
}
613+
// there cannot be a table entry if the wrapper does not have a PythonObjectReference
614+
return true;
615+
}
616+
577617
public static void freeClassReplacements(HandleContext handleContext) {
578618
assert PythonContext.get(null).ownsGil();
579619
handleContext.nativeLookup.forEach((l, ref) -> {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/structs/CStructAccess.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ static void freeManaged(Object pointer,
225225
public static FreeNode create() {
226226
return FreeNodeGen.create();
227227
}
228+
229+
public static FreeNode getUncached() {
230+
return FreeNodeGen.getUncached();
231+
}
228232
}
229233

230234
public abstract static class ReadBaseNode extends Node implements CStructAccessNode {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@
109109
import com.oracle.graal.python.builtins.objects.cext.PythonNativeClass;
110110
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext;
111111
import com.oracle.graal.python.builtins.objects.cext.capi.PThreadState;
112-
import com.oracle.graal.python.builtins.objects.cext.capi.PyTruffleObjectFree;
113112
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapper.PythonAbstractObjectNativeWrapper;
113+
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions;
114114
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.HandleContext;
115115
import com.oracle.graal.python.builtins.objects.cext.common.NativePointer;
116116
import com.oracle.graal.python.builtins.objects.cext.structs.CStructAccess;
@@ -483,12 +483,12 @@ public void dispose(PythonContext context, boolean canRunGuestCode) {
483483
if (dict != null) {
484484
PythonAbstractObjectNativeWrapper dictNativeWrapper = dict.getNativeWrapper();
485485
if (dictNativeWrapper != null && dictNativeWrapper.ref == null) {
486-
PyTruffleObjectFree.releaseNativeWrapperUncached(dictNativeWrapper);
486+
CApiTransitions.releaseNativeWrapperUncached(dictNativeWrapper);
487487
}
488488
}
489489
dict = null;
490490
if (nativeWrapper != null && nativeWrapper.ref == null) {
491-
PyTruffleObjectFree.releaseNativeWrapperUncached(nativeWrapper);
491+
CApiTransitions.releaseNativeWrapperUncached(nativeWrapper);
492492
nativeWrapper = null;
493493
}
494494
/*

0 commit comments

Comments
 (0)