Skip to content

Commit 5277d08

Browse files
committed
Get rid of NativeObjectReferenceArrayWrapper
1 parent 1334b20 commit 5277d08

File tree

4 files changed

+25
-124
lines changed

4 files changed

+25
-124
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public enum NativeCAPISymbol implements NativeCExtSymbol {
112112
FUN_OBJECT_ARRAY_RELEASE("GraalPyPrivate_ObjectArrayRelease", ArgDescriptor.Void, Pointer, Int),
113113
FUN_PY_OBJECT_NEW("GraalPyPrivate_ObjectNew", PyObjectTransfer, PyTypeObject),
114114
FUN_GRAALPY_OBJECT_GC_DEL("GraalPyPrivate_Object_GC_Del", Void, Pointer),
115-
FUN_BULK_DEALLOC("GraalPyPrivate_BulkDealloc", Py_ssize_t, Pointer, INT64_T),
115+
FUN_BULK_DEALLOC("GraalPyPrivate_BulkDealloc", Py_ssize_t, ArgDescriptor.UINTPTR_T, INT64_T),
116116
FUN_SHUTDOWN_BULK_DEALLOC("GraalPyPrivate_BulkDeallocOnShutdown", Py_ssize_t, Pointer, INT64_T),
117117
FUN_GET_CURRENT_RSS("GraalPyPrivate_GetCurrentRSS", SIZE_T),
118118
FUN_ADD_SUBOFFSET("GraalPyPrivate_AddSuboffset", Pointer, Pointer, Py_ssize_t, Py_ssize_t),

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
import java.lang.ref.ReferenceQueue;
4747
import java.lang.ref.WeakReference;
48+
import java.util.ArrayList;
4849
import java.util.Arrays;
4950
import java.util.HashMap;
5051
import java.util.HashSet;
@@ -166,7 +167,7 @@ public HandleContext(boolean useShadowTable) {
166167
nativeStubLookupFreeStack.pushRange(1, DEFAULT_CAPACITY);
167168
}
168169

169-
public final NativeObjectReferenceArrayWrapper referencesToBeFreed = new NativeObjectReferenceArrayWrapper();
170+
public final ArrayList<Long> referencesToBeFreed = new ArrayList<>();
170171
public final HashMap<Long, IdReference<?>> nativeLookup = new HashMap<>();
171172
public final ConcurrentHashMap<Long, Long> nativeWeakRef = new ConcurrentHashMap<>();
172173
public final WeakHashMap<Object, WeakReference<Object>> managedNativeLookup = new WeakHashMap<>();
@@ -417,7 +418,7 @@ public static int pollReferenceQueue() {
417418
ReferenceQueue<Object> queue = handleContext.referenceQueue;
418419
int count = 0;
419420
long start = 0;
420-
NativeObjectReferenceArrayWrapper referencesToBeFreed = handleContext.referencesToBeFreed;
421+
ArrayList<Long> referencesToBeFreed = handleContext.referencesToBeFreed;
421422
while (true) {
422423
Object entry = queue.poll();
423424
if (entry == null) {
@@ -509,7 +510,7 @@ public static int pollReferenceQueue() {
509510
* to be freed. Therefore, this method neither frees any native memory nor runs any object
510511
* destructor (guest code).
511512
*/
512-
private static void processNativeObjectReference(NativeObjectReference reference, NativeObjectReferenceArrayWrapper referencesToBeFreed) {
513+
private static void processNativeObjectReference(NativeObjectReference reference, ArrayList<Long> referencesToBeFreed) {
513514
LOGGER.fine(() -> PythonUtils.formatJString("releasing %s", reference.toString()));
514515
if (subNativeRefCount(reference.pointer, MANAGED_REFCNT) == 0) {
515516
referencesToBeFreed.add(reference.pointer);
@@ -548,26 +549,29 @@ private static void processPyCapsuleReference(PyCapsuleReference reference) {
548549
* element. This method may therefore run arbitrary guest code and strictly requires the GIL to
549550
* be held at the time of invocation.
550551
*/
551-
private static void releaseNativeObjects(PythonContext context, NativeObjectReferenceArrayWrapper referencesToBeFreed) {
552+
private static void releaseNativeObjects(PythonContext context, ArrayList<Long> referencesToBeFreed) {
552553
if (!referencesToBeFreed.isEmpty()) {
553554
/*
554555
* This needs the GIL because this will call the native objects' destructors which can
555556
* be arbitrary guest code.
556557
*/
557-
assert PythonContext.get(null).ownsGil();
558+
assert context.ownsGil();
558559
PythonContext.PythonThreadState threadState = context.getThreadState(context.getLanguage());
559560
/*
560561
* There can be an active exception. Since we might be calling arbitary python, we need
561562
* to stash it.
562563
*/
563564
Object savedException = CExtCommonNodes.ReadAndClearNativeException.executeUncached(threadState);
564565
try {
565-
LOGGER.fine(() -> PythonUtils.formatJString("releasing %d NativeObjectReference instances", referencesToBeFreed.getArraySize()));
566-
Object array = AllocateNode.allocUncached(referencesToBeFreed.getArraySize() * Long.BYTES);
567-
CStructAccess.WriteLongNode.getUncached().writeLongArray(array, referencesToBeFreed.getArray(), (int) referencesToBeFreed.getArraySize(), 0, 0);
568-
PCallCapiFunction.callUncached(NativeCAPISymbol.FUN_BULK_DEALLOC, array, referencesToBeFreed.getArraySize());
569-
FreeNode.executeUncached(array);
570-
referencesToBeFreed.reset();
566+
int size = referencesToBeFreed.size();
567+
LOGGER.fine(() -> PythonUtils.formatJString("releasing %d NativeObjectReference instances", size));
568+
long pointer = AllocateNode.allocUncachedPointer(size * Long.BYTES);
569+
for (int i = 0; i < size; i++) {
570+
CStructAccess.WriteLongNode.writeLong(pointer, i * Long.BYTES, referencesToBeFreed.get(i));
571+
}
572+
PCallCapiFunction.callUncached(NativeCAPISymbol.FUN_BULK_DEALLOC, pointer, size);
573+
FreeNode.freeLong(pointer);
574+
referencesToBeFreed.clear();
571575
} finally {
572576
CExtCommonNodes.ReadAndClearNativeException.executeUncached(threadState);
573577
if (savedException != PNone.NO_VALUE) {

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

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

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ static Object allocLong(long count, long size, @SuppressWarnings("unused") boole
131131
// non-zero size to get unique pointers
132132
try {
133133
long totalSize = Math.multiplyExact(count, size);
134-
long memory = UNSAFE.allocateMemory(totalSize == 0 ? 1 : totalSize);
135-
UNSAFE.setMemory(memory, totalSize, (byte) 0);
134+
long memory = allocUncachedPointer(totalSize);
136135
return new NativePointer(memory);
137136
} catch (ArithmeticException e) {
138137
overflowProfile.enter(inliningTarget);
@@ -159,6 +158,12 @@ public static Object allocUncached(long size) {
159158
return AllocateNodeGen.getUncached().alloc(size);
160159
}
161160

161+
public static long allocUncachedPointer(long size) {
162+
long memory = UNSAFE.allocateMemory(size == 0 ? 1 : size);
163+
UNSAFE.setMemory(memory, size, (byte) 0);
164+
return memory;
165+
}
166+
162167
public static Object callocUncached(long count, long elSize) {
163168
return AllocateNodeGen.getUncached().calloc(count, elSize);
164169
}
@@ -179,7 +184,7 @@ public final void free(Object pointer) {
179184
}
180185

181186
@Specialization
182-
static void freeLong(long pointer) {
187+
public static void freeLong(long pointer) {
183188
UNSAFE.freeMemory(pointer);
184189
}
185190

@@ -1104,16 +1109,6 @@ public final void write(Object pointer, long value) {
11041109
execute(pointer, 0, value);
11051110
}
11061111

1107-
public final void writeLongArray(Object pointer, long[] values) {
1108-
writeLongArray(pointer, values, values.length, 0, 0);
1109-
}
1110-
1111-
public final void writeLongArray(Object pointer, long[] values, int length, int sourceOffset, long targetOffset) {
1112-
for (int i = 0; i < length; i++) {
1113-
execute(pointer, (i + targetOffset) * Long.BYTES, values[i + sourceOffset]);
1114-
}
1115-
}
1116-
11171112
public final void writeIntArray(Object pointer, int[] values) {
11181113
writeIntArray(pointer, values, values.length, 0, 0);
11191114
}
@@ -1129,7 +1124,7 @@ public final boolean accepts(ArgDescriptor desc) {
11291124
}
11301125

11311126
@Specialization
1132-
static void writeLong(long pointer, long offset, long value) {
1127+
public static void writeLong(long pointer, long offset, long value) {
11331128
assert offset >= 0;
11341129
UNSAFE.putLong(pointer + offset, value);
11351130
}

0 commit comments

Comments
 (0)