@@ -78,34 +78,25 @@ public ValueWrapper doubleWrapper(double value) {
78
78
}
79
79
80
80
@ TruffleBoundary
81
- public synchronized void addToBlockMap (HandleBlock block , RubyContext context , RubyLanguage language ) {
81
+ public synchronized HandleBlock addToBlockMap (RubyContext context , RubyLanguage language ) {
82
+ HandleBlock block = new HandleBlock (context , language , this );
82
83
int blockIndex = block .getIndex ();
83
- long blockBase = block .getBase ();
84
- HandleBlockAllocator allocator = language .handleBlockAllocator ;
85
84
HandleBlockWeakReference [] map = growMapIfRequired (blockMap , blockIndex );
86
85
blockMap = map ;
87
86
map [blockIndex ] = new HandleBlockWeakReference (block );
88
87
89
- block .cleanable = RubyLanguage .cleaner .register (block , () -> {
90
- this .blockMap [blockIndex ] = null ;
91
- allocator .addFreeBlock (blockBase );
92
- });
88
+ return block ;
93
89
}
94
90
95
91
@ TruffleBoundary
96
- public void addToSharedBlockMap (HandleBlock block , RubyContext context , RubyLanguage language ) {
92
+ public HandleBlock addToSharedBlockMap (RubyContext context , RubyLanguage language ) {
97
93
synchronized (language ) {
94
+ HandleBlock block = new HandleBlock (context , language , this );
98
95
int blockIndex = block .getIndex ();
99
- long blockBase = block .getBase ();
100
- HandleBlockAllocator allocator = language .handleBlockAllocator ;
101
96
HandleBlockWeakReference [] map = growMapIfRequired (language .handleBlockSharedMap , blockIndex );
102
97
language .handleBlockSharedMap = map ;
103
98
map [blockIndex ] = new HandleBlockWeakReference (block );
104
-
105
- block .cleanable = RubyLanguage .cleaner .register (block , () -> {
106
- language .handleBlockSharedMap [blockIndex ] = null ;
107
- allocator .addFreeBlock (blockBase );
108
- });
99
+ return block ;
109
100
}
110
101
}
111
102
@@ -218,27 +209,39 @@ public synchronized void addFreeBlock(long blockBase) {
218
209
219
210
public static class HandleBlock {
220
211
221
- public static final HandleBlock DUMMY_BLOCK = new HandleBlock (null , 0 , null );
212
+ public static final HandleBlock DUMMY_BLOCK = new HandleBlock ();
222
213
223
214
private static final Set <HandleBlock > keepAlive = ConcurrentHashMap .newKeySet ();
224
215
225
216
private final long base ;
226
217
@ SuppressWarnings ("rawtypes" ) private final ValueWrapper [] wrappers ;
227
218
private int count ;
228
219
229
- private Cleanable cleanable ;
220
+ @ SuppressWarnings ( "unused" ) private Cleanable cleanable ;
230
221
231
- public HandleBlock (RubyContext context , HandleBlockAllocator allocator ) {
232
- this (context , allocator .getFreeBlock (), new ValueWrapper [BLOCK_SIZE ]);
222
+ private HandleBlock () {
223
+ base = 0 ;
224
+ cleanable = null ;
225
+ wrappers = null ;
233
226
}
234
227
235
- private HandleBlock (RubyContext context , long base , ValueWrapper [] wrappers ) {
228
+ public HandleBlock (RubyContext context , RubyLanguage language , ValueWrapperManager manager ) {
229
+ HandleBlockAllocator allocator = language .handleBlockAllocator ;
230
+ long base = allocator .getFreeBlock ();
236
231
if (context != null && context .getOptions ().CEXTS_KEEP_HANDLES_ALIVE ) {
237
232
keepAlive (this );
238
233
}
239
234
this .base = base ;
240
- this .wrappers = wrappers ;
235
+ this .wrappers = new ValueWrapper [ BLOCK_SIZE ] ;
241
236
this .count = 0 ;
237
+ this .cleanable = RubyLanguage .cleaner .register (this , HandleBlock .makeCleaner (manager , base , allocator ));
238
+ }
239
+
240
+ private static Runnable makeCleaner (ValueWrapperManager manager , long base , HandleBlockAllocator allocator ) {
241
+ return () -> {
242
+ manager .blockMap [(int ) ((base - ALLOCATION_BASE ) >> BLOCK_BITS )] = null ;
243
+ allocator .addFreeBlock (base );
244
+ };
242
245
}
243
246
244
247
@ TruffleBoundary
@@ -330,11 +333,11 @@ protected static long allocateHandle(ValueWrapper wrapper, RubyContext context,
330
333
context .getMarkingService ().queueForMarking (block );
331
334
}
332
335
if (shared ) {
333
- block = ( holder . sharedHandleBlock = new HandleBlock (context , language . handleBlockAllocator ) );
334
- context . getValueWrapperManager (). addToSharedBlockMap ( block , context , language ) ;
336
+ block = context . getValueWrapperManager (). addToSharedBlockMap (context , language );
337
+ holder . sharedHandleBlock = block ;
335
338
} else {
336
- block = ( holder . handleBlock = new HandleBlock (context , language . handleBlockAllocator ) );
337
- context . getValueWrapperManager (). addToBlockMap ( block , context , language ) ;
339
+ block = context . getValueWrapperManager (). addToBlockMap (context , language );
340
+ holder . handleBlock = block ;
338
341
}
339
342
340
343
}
@@ -357,9 +360,9 @@ public static HandleBlock allocateNewBlock(RubyContext context, RubyLanguage lan
357
360
if (block != null ) {
358
361
context .getMarkingService ().queueForMarking (block );
359
362
}
360
- block = (holder .handleBlock = new HandleBlock (context , language .handleBlockAllocator ));
361
- context .getValueWrapperManager ().addToBlockMap (block , context , language );
363
+ block = context .getValueWrapperManager ().addToBlockMap (context , language );
362
364
365
+ holder .handleBlock = block ;
363
366
return block ;
364
367
}
365
368
0 commit comments