Skip to content

Commit 34a5808

Browse files
committed
Refactor to make handle block cleanable final.
1 parent 5b6cad0 commit 34a5808

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -78,34 +78,25 @@ public ValueWrapper doubleWrapper(double value) {
7878
}
7979

8080
@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);
8283
int blockIndex = block.getIndex();
83-
long blockBase = block.getBase();
84-
HandleBlockAllocator allocator = language.handleBlockAllocator;
8584
HandleBlockWeakReference[] map = growMapIfRequired(blockMap, blockIndex);
8685
blockMap = map;
8786
map[blockIndex] = new HandleBlockWeakReference(block);
8887

89-
block.cleanable = RubyLanguage.cleaner.register(block, () -> {
90-
this.blockMap[blockIndex] = null;
91-
allocator.addFreeBlock(blockBase);
92-
});
88+
return block;
9389
}
9490

9591
@TruffleBoundary
96-
public void addToSharedBlockMap(HandleBlock block, RubyContext context, RubyLanguage language) {
92+
public HandleBlock addToSharedBlockMap(RubyContext context, RubyLanguage language) {
9793
synchronized (language) {
94+
HandleBlock block = new HandleBlock(context, language, this);
9895
int blockIndex = block.getIndex();
99-
long blockBase = block.getBase();
100-
HandleBlockAllocator allocator = language.handleBlockAllocator;
10196
HandleBlockWeakReference[] map = growMapIfRequired(language.handleBlockSharedMap, blockIndex);
10297
language.handleBlockSharedMap = map;
10398
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;
109100
}
110101
}
111102

@@ -218,27 +209,39 @@ public synchronized void addFreeBlock(long blockBase) {
218209

219210
public static class HandleBlock {
220211

221-
public static final HandleBlock DUMMY_BLOCK = new HandleBlock(null, 0, null);
212+
public static final HandleBlock DUMMY_BLOCK = new HandleBlock();
222213

223214
private static final Set<HandleBlock> keepAlive = ConcurrentHashMap.newKeySet();
224215

225216
private final long base;
226217
@SuppressWarnings("rawtypes") private final ValueWrapper[] wrappers;
227218
private int count;
228219

229-
private Cleanable cleanable;
220+
@SuppressWarnings("unused") private Cleanable cleanable;
230221

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;
233226
}
234227

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();
236231
if (context != null && context.getOptions().CEXTS_KEEP_HANDLES_ALIVE) {
237232
keepAlive(this);
238233
}
239234
this.base = base;
240-
this.wrappers = wrappers;
235+
this.wrappers = new ValueWrapper[BLOCK_SIZE];
241236
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+
};
242245
}
243246

244247
@TruffleBoundary
@@ -330,11 +333,11 @@ protected static long allocateHandle(ValueWrapper wrapper, RubyContext context,
330333
context.getMarkingService().queueForMarking(block);
331334
}
332335
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;
335338
} 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;
338341
}
339342

340343
}
@@ -357,9 +360,9 @@ public static HandleBlock allocateNewBlock(RubyContext context, RubyLanguage lan
357360
if (block != null) {
358361
context.getMarkingService().queueForMarking(block);
359362
}
360-
block = (holder.handleBlock = new HandleBlock(context, language.handleBlockAllocator));
361-
context.getValueWrapperManager().addToBlockMap(block, context, language);
363+
block = context.getValueWrapperManager().addToBlockMap(context, language);
362364

365+
holder.handleBlock = block;
363366
return block;
364367
}
365368

0 commit comments

Comments
 (0)