Skip to content

Commit 42b7bc3

Browse files
committed
Handle managed pointers in DecRefPointerNode
1 parent ccb7460 commit 42b7bc3

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import com.oracle.graal.python.builtins.objects.cext.common.CExtContext;
111111
import com.oracle.graal.python.builtins.objects.cext.common.CExtContext.ModuleSpec;
112112
import com.oracle.graal.python.builtins.objects.cext.common.GetNextVaArgNode;
113+
import com.oracle.graal.python.builtins.objects.cext.common.NativePointer;
113114
import com.oracle.graal.python.builtins.objects.cext.structs.CFields;
114115
import com.oracle.graal.python.builtins.objects.cext.structs.CStructAccess;
115116
import com.oracle.graal.python.builtins.objects.cext.structs.CStructs;
@@ -1104,20 +1105,36 @@ public static void executeUncached(Object pointer) {
11041105
public abstract void execute(Node inliningTarget, Object pointer);
11051106

11061107
@Specialization
1107-
static void doDecref(Node inliningTarget, Object pointer,
1108+
static void doDecref(Node inliningTarget, Object pointerObj,
1109+
@CachedLibrary(limit = "2") InteropLibrary lib,
11081110
@Cached(inline = false) CApiTransitions.ToPythonWrapperNode toPythonWrapperNode,
11091111
@Cached InlinedBranchProfile isWrapperProfile,
11101112
@Cached InlinedBranchProfile isNativeObject,
11111113
@Cached(inline = false) CStructAccess.ReadI64Node readRefcount,
11121114
@Cached(inline = false) CStructAccess.WriteLongNode writeRefcount,
11131115
@Cached(inline = false) PCallCapiFunction callDealloc) {
1116+
long pointer;
1117+
if (pointerObj instanceof Long longPointer) {
1118+
pointer = longPointer;
1119+
} else {
1120+
if (lib.isPointer(pointerObj)) {
1121+
try {
1122+
pointer = lib.asPointer(pointerObj);
1123+
} catch (UnsupportedMessageException e) {
1124+
throw CompilerDirectives.shouldNotReachHere();
1125+
}
1126+
} else {
1127+
// No refcounting in managed mode
1128+
return;
1129+
}
1130+
}
11141131
PythonNativeWrapper wrapper = toPythonWrapperNode.executeWrapper(pointer, false);
11151132
if (wrapper instanceof PythonAbstractObjectNativeWrapper objectWrapper) {
11161133
isWrapperProfile.enter(inliningTarget);
11171134
objectWrapper.decRef();
11181135
} else if (wrapper == null) {
11191136
isNativeObject.enter(inliningTarget);
1120-
assert NativeToPythonNode.executeUncached(pointer) instanceof PythonAbstractNativeObject;
1137+
assert NativeToPythonNode.executeUncached(new NativePointer(pointer)) instanceof PythonAbstractNativeObject;
11211138
long refcount = readRefcount.read(pointer, PyObject__ob_refcnt);
11221139
if (refcount != IMMORTAL_REFCNT) {
11231140
refcount--;

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,10 @@ public final long getRefCount() {
198198
return MANAGED_REFCNT;
199199
}
200200

201-
public final void setRefCount(Node inliningTarget, long refCount, InlinedConditionProfile hasRefProfile) {
202-
CApiTransitions.writeNativeRefCount(HandlePointerConverter.pointerToStub(getNativePointer()), refCount);
203-
updateRef(inliningTarget, refCount, hasRefProfile);
204-
}
205-
206201
/**
207202
* Adjusts the native wrapper's reference to be weak (if {@code refCount <= MANAGED_REFCNT})
208203
* or to be strong (if {@code refCount > MANAGED_REFCNT}) if there is a reference. This
209-
* method should be called at appropriate points in the program, e.g., method
210-
* {@link #setRefCount(Node, long, InlinedConditionProfile)} uses it, or it should be called
204+
* method should be called at appropriate points in the program, e.g., it should be called
211205
* from native code if the refcount falls below {@link #MANAGED_REFCNT}.
212206
*/
213207
public final void updateRef(Node inliningTarget, long refCount, InlinedConditionProfile hasRefProfile) {
@@ -233,6 +227,7 @@ public void incRef() {
233227

234228
@TruffleBoundary(allowInlining = true)
235229
public long decRef() {
230+
assert isNative();
236231
long pointer = HandlePointerConverter.pointerToStub(getNativePointer());
237232
long refCount = CApiTransitions.readNativeRefCount(pointer);
238233
if (refCount != IMMORTAL_REFCNT) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/SequenceStorage.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public final int getCapacity() {
8181

8282
public abstract SequenceStorage copy();
8383

84-
8584
/**
8685
* Get internal array object without copying. Note: The length must be taken from the sequence
8786
* storage object.

0 commit comments

Comments
 (0)