@@ -71,6 +71,7 @@ static JNIEnv* jniEnv;
71
71
UPCALL(TypeGenericNew, SIG_HPY, SIG_HPY) \
72
72
UPCALL(AsStruct, SIG_HPY, SIG_PTR) \
73
73
UPCALL(Close, SIG_HPY, SIG_VOID) \
74
+ UPCALL(BulkClose, SIG_PTR SIG_INT, SIG_VOID) \
74
75
UPCALL(FloatFromDouble, SIG_DOUBLE, SIG_HPY) \
75
76
UPCALL(FloatAsDouble, SIG_HPY, SIG_DOUBLE) \
76
77
UPCALL(LongAsLong, SIG_HPY, SIG_LONG) \
@@ -197,25 +198,6 @@ static HPy ctx_ListNew_jni(HPyContext *ctx, HPy_ssize_t len) {
197
198
//*************************
198
199
// BOXING
199
200
200
- #define NAN_BOXING_BASE (0x0007000000000000llu)
201
- #define NAN_BOXING_MASK (0xFFFF000000000000llu)
202
- #define NAN_BOXING_INT (0x0001000000000000llu)
203
- #define NAN_BOXING_INT_MASK (0x00000000FFFFFFFFllu)
204
- #define NAN_BOXING_MAX_HANDLE (0x000000007FFFFFFFllu)
205
- #define IMMUTABLE_HANDLES (0x0000000000000100llu)
206
-
207
- static bool isBoxedDouble (uint64_t value ) {
208
- return value >= NAN_BOXING_BASE ;
209
- }
210
-
211
- static bool isBoxedHandle (uint64_t value ) {
212
- return value <= NAN_BOXING_MAX_HANDLE ;
213
- }
214
-
215
- static bool isBoxedInt (uint64_t value ) {
216
- return (value & NAN_BOXING_MASK ) == NAN_BOXING_INT ;
217
- }
218
-
219
201
static double unboxDouble (uint64_t value ) {
220
202
uint64_t doubleBits = value - NAN_BOXING_BASE ;
221
203
return * ((double * ) & doubleBits );
@@ -227,32 +209,6 @@ static uint64_t boxDouble(double value) {
227
209
return doubleBits + NAN_BOXING_BASE ;
228
210
}
229
211
230
- static uint64_t unboxHandle (uint64_t value ) {
231
- return value ;
232
- }
233
-
234
- static uint64_t boxHandle (uint64_t handle ) {
235
- return handle ;
236
- }
237
-
238
- static int32_t unboxInt (uint64_t value ) {
239
- return (int32_t ) (value - NAN_BOXING_INT );
240
- }
241
-
242
- static uint64_t boxInt (int32_t value ) {
243
- return (value & NAN_BOXING_INT_MASK ) + NAN_BOXING_INT ;
244
- }
245
-
246
- static inline uint64_t toBits (HPy ptr ) {
247
- /* return * ((uint64_t*) &ptr._i); */
248
- return (uint64_t ) (ptr ._i );
249
- }
250
-
251
- static inline HPy toPtr (uint64_t ptr ) {
252
- /* return * ((void**) &ptr); */
253
- return (HPy ) { (HPy_ssize_t ) ptr };
254
- }
255
-
256
212
//*************************
257
213
// direct fast paths that handle certain calls on the native side:
258
214
@@ -269,6 +225,19 @@ static int (*original_TypeCheck)(HPyContext *ctx, HPy h, HPy type);
269
225
static void (* original_Close )(HPyContext * ctx , HPy h );
270
226
static HPy (* original_UnicodeFromWideChar )(HPyContext * ctx , const wchar_t * arr , HPy_ssize_t size );
271
227
static HPy (* original_TupleFromArray )(HPyContext * ctx , HPy * items , HPy_ssize_t nitems );
228
+ static int (* original_Is )(HPyContext * ctx , HPy a , HPy b );
229
+
230
+ static int augment_Is (HPyContext * ctx , HPy a , HPy b ) {
231
+ long bitsA = toBits (a );
232
+ long bitsB = toBits (b );
233
+ if (bitsA == bitsB ) {
234
+ return 1 ;
235
+ } else if (isBoxedHandle (bitsA ) && isBoxedHandle (bitsB )) {
236
+ return original_Is (ctx , a , b );
237
+ } else {
238
+ return 0 ;
239
+ }
240
+ }
272
241
273
242
static void * augment_AsStruct (HPyContext * ctx , HPy h ) {
274
243
uint64_t bits = toBits (h );
@@ -322,12 +291,25 @@ static HPy augment_LongFromLong(HPyContext *ctx, long l) {
322
291
}
323
292
}
324
293
294
+ #define MAX_UNCLOSED_HANDLES 32
295
+ static int32_t unclosedHandleTop = 0 ;
296
+ static HPy unclosedHandles [MAX_UNCLOSED_HANDLES ];
297
+
325
298
static void augment_Close (HPyContext * ctx , HPy h ) {
326
299
uint64_t bits = toBits (h );
327
300
if (!bits ) {
328
301
return ;
329
302
} else if (isBoxedHandle (bits )) {
330
- return original_Close (ctx , h );
303
+ if (bits < IMMUTABLE_HANDLES ) {
304
+ return ;
305
+ }
306
+ if (unclosedHandleTop < MAX_UNCLOSED_HANDLES ) {
307
+ unclosedHandles [unclosedHandleTop ++ ] = h ;
308
+ } else {
309
+ upcallBulkClose (ctx , unclosedHandles , unclosedHandleTop );
310
+ memset (unclosedHandles , 0 , sizeof (uint64_t ) * unclosedHandleTop );
311
+ unclosedHandleTop = 0 ;
312
+ }
331
313
}
332
314
}
333
315
@@ -440,6 +422,9 @@ static HPy augment_TupleFromArray(HPyContext *ctx, HPy *items, HPy_ssize_t nitem
440
422
return upcallTupleFromArray (ctx , items , nitems , JNI_FALSE );
441
423
}
442
424
425
+ _HPy_HIDDEN void upcallBulkClose (HPyContext * ctx , HPy * items , HPy_ssize_t nitems ) {
426
+ DO_UPCALL_VOID (CONTEXT_INSTANCE (ctx ), BulkClose , items , nitems );
427
+ }
443
428
444
429
void initDirectFastPaths (HPyContext * context ) {
445
430
LOG ("%p" , context );
@@ -466,6 +451,8 @@ void initDirectFastPaths(HPyContext *context) {
466
451
original_AsStruct = context -> ctx_AsStruct ;
467
452
context -> ctx_AsStruct = augment_AsStruct ;
468
453
454
+ context -> ctx_AsStructLegacy = augment_AsStruct ;
455
+
469
456
original_Dup = context -> ctx_Dup ;
470
457
context -> ctx_Dup = augment_Dup ;
471
458
@@ -483,6 +470,9 @@ void initDirectFastPaths(HPyContext *context) {
483
470
484
471
original_TupleFromArray = context -> ctx_Tuple_FromArray ;
485
472
context -> ctx_Tuple_FromArray = augment_TupleFromArray ;
473
+
474
+ original_Is = context -> ctx_Is ;
475
+ context -> ctx_Is = augment_Is ;
486
476
}
487
477
488
478
void setHPyContextNativeSpace (HPyContext * context , void * * nativeSpace ) {
0 commit comments