44
44
import java .lang .ref .WeakReference ;
45
45
import java .util .ArrayList ;
46
46
import java .util .HashMap ;
47
+ import java .util .HashSet ;
48
+ import java .util .Set ;
47
49
import java .util .WeakHashMap ;
48
50
import java .util .logging .Level ;
49
51
50
52
import com .oracle .graal .python .builtins .objects .PNone ;
51
53
import com .oracle .graal .python .builtins .objects .PythonAbstractObject ;
52
54
import com .oracle .graal .python .builtins .objects .cext .PythonAbstractNativeObject ;
55
+ import com .oracle .graal .python .builtins .objects .cext .capi .CApiContext ;
53
56
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .FromCharPointerNode ;
54
57
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodes .PCallCapiFunction ;
55
58
import com .oracle .graal .python .builtins .objects .cext .capi .CExtNodesFactory .FromCharPointerNodeGen ;
56
59
import com .oracle .graal .python .builtins .objects .cext .capi .DynamicObjectNativeWrapper .PrimitiveNativeWrapper ;
57
- import com .oracle .graal .python .builtins .objects .cext .capi .CApiContext ;
58
60
import com .oracle .graal .python .builtins .objects .cext .capi .NativeCAPISymbol ;
59
61
import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativePointer ;
60
62
import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativeWrapper ;
61
63
import com .oracle .graal .python .builtins .objects .cext .capi .TruffleObjectNativeWrapper ;
62
64
import com .oracle .graal .python .builtins .objects .getsetdescriptor .DescriptorDeleteMarker ;
63
65
import com .oracle .graal .python .runtime .PythonContext ;
66
+ import com .oracle .graal .python .runtime .sequence .storage .NativeSequenceStorage ;
67
+ import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage .ListStorageType ;
64
68
import com .oracle .graal .python .util .PythonUtils ;
65
69
import com .oracle .truffle .api .CompilerAsserts ;
66
70
import com .oracle .truffle .api .CompilerDirectives ;
67
- import com .oracle .truffle .api .TruffleLogger ;
68
71
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
72
+ import com .oracle .truffle .api .TruffleLogger ;
69
73
import com .oracle .truffle .api .dsl .Cached ;
70
74
import com .oracle .truffle .api .interop .InteropLibrary ;
71
75
import com .oracle .truffle .api .interop .TruffleObject ;
@@ -94,6 +98,7 @@ public static final class HandleContext {
94
98
public final HashMap <Long , IdReference <?>> nativeLookup = new HashMap <>();
95
99
public final WeakHashMap <Object , WeakReference <PythonAbstractNativeObject >> managedNativeLookup = new WeakHashMap <>();
96
100
public final ArrayList <PythonObjectReference > nativeHandles = new ArrayList <>();
101
+ public final Set <NativeStorageReference > nativeStorageReferences = new HashSet <>();
97
102
98
103
public final ReferenceQueue <Object > referenceQueue = new ReferenceQueue <>();
99
104
@@ -146,7 +151,7 @@ public NativeObjectReference(PythonAbstractNativeObject referent, long pointer)
146
151
this .pointer = pointer ;
147
152
referent .ref = this ;
148
153
assert (pointer & 7 ) == 0 ;
149
- LOGGER .finer (() -> PythonUtils .formatJString ("new NativeObjectReference reference to %s" , referent ));
154
+ LOGGER .finer (() -> PythonUtils .formatJString ("new NativeObjectReference<%s> to %s" , Long . toHexString ( pointer ) , referent ));
150
155
}
151
156
152
157
@ Override
@@ -155,6 +160,41 @@ public String toString() {
155
160
}
156
161
}
157
162
163
+ public static final class NativeStorageReference extends IdReference <NativeSequenceStorage > {
164
+ private final ListStorageType type ;
165
+ private Object ptr ;
166
+ private int size ;
167
+
168
+ public NativeStorageReference (NativeSequenceStorage storage ) {
169
+ super (storage );
170
+ type = storage .getElementType ();
171
+ ptr = storage .getPtr ();
172
+ LOGGER .finer (() -> PythonUtils .formatJString ("new NativeStorageReference<%s>" , ptr ));
173
+ }
174
+
175
+ public Object getPtr () {
176
+ return ptr ;
177
+ }
178
+
179
+ public void setPtr (Object ptr ) {
180
+ this .ptr = ptr ;
181
+ }
182
+
183
+ public int getSize () {
184
+ return size ;
185
+ }
186
+
187
+ public void setSize (int size ) {
188
+ this .size = size ;
189
+ }
190
+ }
191
+
192
+ public static void registerNativeSequenceStorage (NativeSequenceStorage storage ) {
193
+ NativeStorageReference ref = new NativeStorageReference (storage );
194
+ storage .setReference (ref );
195
+ getContext ().nativeStorageReferences .add (ref );
196
+ }
197
+
158
198
@ TruffleBoundary
159
199
public static void pollReferenceQueue () {
160
200
HandleContext context = getContext ();
@@ -178,8 +218,7 @@ public static void pollReferenceQueue() {
178
218
assert context .referenceQueuePollActive ;
179
219
}
180
220
count ++;
181
- if (entry instanceof PythonObjectReference ) {
182
- PythonObjectReference reference = (PythonObjectReference ) entry ;
221
+ if (entry instanceof PythonObjectReference reference ) {
183
222
LOGGER .finer (() -> PythonUtils .formatJString ("releasing PythonObjectReference %s" , reference ));
184
223
185
224
if (HandleTester .pointsToPyHandleSpace (reference .pointer )) {
@@ -190,8 +229,7 @@ public static void pollReferenceQueue() {
190
229
assert nativeLookupGet (context , reference .pointer ) != null : Long .toHexString (reference .pointer );
191
230
nativeLookupRemove (context , reference .pointer );
192
231
}
193
- } else {
194
- NativeObjectReference reference = (NativeObjectReference ) entry ;
232
+ } else if (entry instanceof NativeObjectReference reference ) {
195
233
LOGGER .finer (() -> PythonUtils .formatJString ("releasing NativeObjectReference %s" , reference ));
196
234
nativeLookupRemove (context , reference .pointer );
197
235
Object object = reference .object ;
@@ -203,6 +241,15 @@ public static void pollReferenceQueue() {
203
241
}
204
242
}
205
243
PCallCapiFunction .getUncached ().call (NativeCAPISymbol .FUN_SUBREF , object , PythonNativeWrapper .MANAGED_REFCNT );
244
+ } else if (entry instanceof NativeStorageReference reference ) {
245
+ LOGGER .finer (() -> PythonUtils .formatJString ("releasing NativeStorageReference %s" , reference ));
246
+ context .nativeStorageReferences .remove (entry );
247
+ if (reference .type == ListStorageType .Generic ) {
248
+ PCallCapiFunction .getUncached ().call (NativeCAPISymbol .FUN_PY_TRUFFLE_OBJECT_ARRAY_FREE , reference .ptr , reference .size );
249
+ } else {
250
+ assert reference .type != ListStorageType .Uninitialized ;
251
+ PCallCapiFunction .getUncached ().call (NativeCAPISymbol .FUN_PY_TRUFFLE_PRIMITIVE_ARRAY_FREE , reference .ptr );
252
+ }
206
253
}
207
254
}
208
255
}
0 commit comments