Skip to content

Commit 016c8b2

Browse files
committed
Make NativeReferenceStack generic
1 parent 0058f86 commit 016c8b2

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
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
@@ -105,7 +105,7 @@ public final class CApiContext extends CExtContext {
105105

106106
private final ReferenceQueue<Object> nativeObjectsQueue;
107107
private Map<Object, AllocInfo> allocatedNativeMemory;
108-
private final NativeReferenceStack nativeObjectWrapperList;
108+
private final NativeReferenceStack<NativeObjectReference> nativeObjectWrapperList;
109109
private TraceMallocDomain[] traceMallocDomains;
110110

111111
/** Container of pointers that have seen to be free'd. */
@@ -135,7 +135,7 @@ public final class CApiContext extends CExtContext {
135135
public CApiContext(PythonContext context, Object hpyLibrary) {
136136
super(context, hpyLibrary, CAPIConversionNodeSupplier.INSTANCE);
137137
nativeObjectsQueue = new ReferenceQueue<>();
138-
nativeObjectWrapperList = new NativeReferenceStack();
138+
nativeObjectWrapperList = new NativeReferenceStack<>(NativeObjectReference.class);
139139

140140
// avoid 0 to be used as ID
141141
int nullID = nativeObjectWrapperList.reserve();

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,21 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.cext.capi;
4242

43+
import java.lang.reflect.Array;
4344
import java.util.Arrays;
4445
import java.util.Iterator;
4546

46-
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext.NativeObjectReference;
4747
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4848

49-
public final class NativeReferenceStack implements Iterable<NativeObjectReference> {
49+
public final class NativeReferenceStack<T> implements Iterable<T> {
5050
private static final int INITIAL_CAPACITY = 64;
5151

5252
private final IntegerStack freeList;
53-
private NativeObjectReference[] nativeObjectWrapperList;
53+
private T[] nativeObjectWrapperList;
5454

55-
public NativeReferenceStack() {
56-
nativeObjectWrapperList = new NativeObjectReference[INITIAL_CAPACITY];
55+
@TruffleBoundary
56+
public NativeReferenceStack(Class<T> elementClazz) {
57+
nativeObjectWrapperList = (T[]) Array.newInstance(elementClazz, INITIAL_CAPACITY);
5758
freeList = new IntegerStack(INITIAL_CAPACITY);
5859
freeList.addToFreeList(0, INITIAL_CAPACITY);
5960
}
@@ -66,14 +67,14 @@ private void enlargeNativeReferenceList() {
6667
freeList.addToFreeList(oldSize, newSize);
6768
}
6869

69-
public NativeObjectReference get(int idx) {
70+
public T get(int idx) {
7071
assert 0 <= idx && idx < nativeObjectWrapperList.length;
7172
return nativeObjectWrapperList[idx];
7273
}
7374

74-
public NativeObjectReference remove(int idx) {
75+
public T remove(int idx) {
7576
assert 0 <= idx && idx < nativeObjectWrapperList.length;
76-
NativeObjectReference ref = nativeObjectWrapperList[idx];
77+
T ref = nativeObjectWrapperList[idx];
7778
nativeObjectWrapperList[idx] = null;
7879
freeList.push(idx);
7980
return ref;
@@ -89,21 +90,21 @@ public int reserve() {
8990
return nativeRefID;
9091
}
9192

92-
public void commit(int idx, NativeObjectReference nativeObjectReference) {
93+
public void commit(int idx, T nativeObjectReference) {
9394
assert 0 <= idx && idx < nativeObjectWrapperList.length;
9495
assert nativeObjectWrapperList[idx] == null : "cannot overwrite an allocated native object reference slot";
9596
nativeObjectWrapperList[idx] = nativeObjectReference;
9697
}
9798

98-
public NativeObjectReference resurrect(int idx, NativeObjectReference nativeObjectReference) {
99+
public T resurrect(int idx, T nativeObjectReference) {
99100
assert 0 <= idx && idx < nativeObjectWrapperList.length;
100-
NativeObjectReference old = nativeObjectWrapperList[idx];
101+
T old = nativeObjectWrapperList[idx];
101102
nativeObjectWrapperList[idx] = nativeObjectReference;
102103
return old;
103104
}
104105

105106
@Override
106-
public Iterator<NativeObjectReference> iterator() {
107+
public Iterator<T> iterator() {
107108
return Arrays.asList(nativeObjectWrapperList).iterator();
108109
}
109110

0 commit comments

Comments
 (0)