Skip to content

Commit d1270bc

Browse files
committed
Make the Ruby Language cleaner non-static.
1 parent 34a5808 commit d1270bc

File tree

9 files changed

+45
-44
lines changed

9 files changed

+45
-44
lines changed

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ public final class RubyLanguage extends TruffleLanguage<RubyContext> {
167167
public static final String MIME_TYPE_MAIN_SCRIPT = "application/x-ruby;main-script=true";
168168
public static final String[] MIME_TYPES = { MIME_TYPE, MIME_TYPE_COVERAGE, MIME_TYPE_MAIN_SCRIPT };
169169

170-
public static final Cleaner cleaner = Cleaner.create();
171-
172170
public static final String PLATFORM = String.format(
173171
"%s-%s%s",
174172
Platform.getArchName(),
@@ -213,6 +211,8 @@ public static final class ThreadLocalState {
213211
public final SymbolTable symbolTable;
214212
public final KeywordArgumentsDescriptorManager keywordArgumentsDescriptorManager = new KeywordArgumentsDescriptorManager();
215213
public final FrozenStringLiterals frozenStringLiterals;
214+
public final Cleaner cleaner = Cleaner.create();
215+
216216

217217
public volatile ValueWrapperManager.HandleBlockWeakReference[] handleBlockSharedMap = new ValueWrapperManager.HandleBlockWeakReference[0];
218218
public final ValueWrapperManager.HandleBlockAllocator handleBlockAllocator = new ValueWrapperManager.HandleBlockAllocator();

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ public abstract static class RbStrNewNulNode extends CoreMethodArrayArgumentsNod
708708
@Specialization
709709
protected RubyString rbStrNewNul(int byteLength,
710710
@Cached StringNodes.MakeStringNode makeStringNode) {
711-
final Rope rope = NativeRope.newBuffer(getContext(), byteLength, byteLength);
711+
final Rope rope = NativeRope.newBuffer(getLanguage(), byteLength, byteLength);
712712

713713
return makeStringNode.fromRope(rope, Encodings.BINARY);
714714
}
@@ -766,7 +766,7 @@ protected RubyString rbStrResize(RubyString string, int newByteLength,
766766
nativeRope.clearCodeRange();
767767
return string;
768768
} else {
769-
final NativeRope newRope = nativeRope.resize(getContext(), newByteLength);
769+
final NativeRope newRope = nativeRope.resize(getLanguage(), newByteLength);
770770

771771
// Like MRI's rb_str_resize()
772772
newRope.clearCodeRange();
@@ -788,7 +788,7 @@ protected RubyString trStrCapaResize(RubyString string, int newCapacity,
788788
if (nativeRope.getCapacity() == newCapacity) {
789789
return string;
790790
} else {
791-
final NativeRope newRope = nativeRope.expandCapacity(getContext(), newCapacity);
791+
final NativeRope newRope = nativeRope.expandCapacity(getLanguage(), newCapacity);
792792
string.setRope(newRope);
793793
return string;
794794
}
@@ -1215,7 +1215,8 @@ protected NativeRope toNative(RubyString string,
12151215
bytesNode.execute(currentRope),
12161216
currentRope.getEncoding(),
12171217
characterLengthNode.execute(currentRope),
1218-
codeRangeNode.execute(currentRope));
1218+
codeRangeNode.execute(currentRope),
1219+
getLanguage());
12191220
string.setRope(nativeRope);
12201221
}
12211222

@@ -1224,7 +1225,7 @@ protected NativeRope toNative(RubyString string,
12241225

12251226
@Specialization
12261227
protected NativeRope toNativeImmutable(ImmutableRubyString string) {
1227-
return string.getNativeRope();
1228+
return string.getNativeRope(getLanguage());
12281229
}
12291230

12301231
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public HandleBlock(RubyContext context, RubyLanguage language, ValueWrapperManag
234234
this.base = base;
235235
this.wrappers = new ValueWrapper[BLOCK_SIZE];
236236
this.count = 0;
237-
this.cleanable = RubyLanguage.cleaner.register(this, HandleBlock.makeCleaner(manager, base, allocator));
237+
this.cleanable = language.cleaner.register(this, HandleBlock.makeCleaner(manager, base, allocator));
238238
}
239239

240240
private static Runnable makeCleaner(ValueWrapperManager manager, long base, HandleBlockAllocator allocator) {

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2349,7 +2349,7 @@ protected RubyArray storeToNative(RubyArray array,
23492349
@CachedLibrary("store") ArrayStoreLibrary stores,
23502350
@Cached IntValueProfile arraySizeProfile) {
23512351
final int size = arraySizeProfile.profile(array.size);
2352-
Pointer pointer = Pointer.mallocAutoRelease(size * Pointer.SIZE);
2352+
Pointer pointer = Pointer.mallocAutoRelease(size * Pointer.SIZE, getLanguage());
23532353
NativeArrayStorage newStore = new NativeArrayStorage(pointer, size);
23542354
stores.copyContents(store, 0, newStore, 0, size);
23552355
getContext().getMarkingService().addMarker(

src/main/java/org/truffleruby/core/rope/NativeRope.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.jcodings.Encoding;
1414
import org.jcodings.specific.ASCIIEncoding;
1515
import org.truffleruby.RubyContext;
16+
import org.truffleruby.RubyLanguage;
1617
import org.truffleruby.core.string.StringAttributes;
1718
import org.truffleruby.core.string.StringSupport;
1819
import org.truffleruby.extra.ffi.Pointer;
@@ -31,8 +32,9 @@ public NativeRope(
3132
byte[] bytes,
3233
Encoding encoding,
3334
int characterLength,
34-
CodeRange codeRange) {
35-
this(allocateNativePointer(bytes), bytes.length, encoding, characterLength, codeRange);
35+
CodeRange codeRange,
36+
RubyLanguage language) {
37+
this(allocateNativePointer(bytes, language), bytes.length, encoding, characterLength, codeRange);
3638
}
3739

3840
private NativeRope(Pointer pointer, int byteLength, Encoding encoding, int characterLength, CodeRange codeRange) {
@@ -44,23 +46,23 @@ private NativeRope(Pointer pointer, int byteLength, Encoding encoding, int chara
4446
this.pointer = pointer;
4547
}
4648

47-
private static Pointer allocateNativePointer(byte[] bytes) {
48-
final Pointer pointer = Pointer.mallocAutoRelease(bytes.length + 1);
49+
private static Pointer allocateNativePointer(byte[] bytes, RubyLanguage language) {
50+
final Pointer pointer = Pointer.mallocAutoRelease(bytes.length + 1, language);
4951
pointer.writeBytes(0, bytes, 0, bytes.length);
5052
pointer.writeByte(bytes.length, (byte) 0);
5153
return pointer;
5254
}
5355

54-
private static Pointer copyNativePointer(RubyContext context, Pointer existing) {
55-
final Pointer pointer = Pointer.mallocAutoRelease(existing.getSize());
56+
private static Pointer copyNativePointer(RubyLanguage language, Pointer existing) {
57+
final Pointer pointer = Pointer.mallocAutoRelease(existing.getSize(), language);
5658
pointer.writeBytes(0, existing, 0, existing.getSize());
5759
return pointer;
5860
}
5961

60-
public static NativeRope newBuffer(RubyContext context, int byteCapacity, int byteLength) {
62+
public static NativeRope newBuffer(RubyLanguage language, int byteCapacity, int byteLength) {
6163
assert byteCapacity >= byteLength;
6264

63-
final Pointer pointer = Pointer.callocAutoRelease(byteCapacity + 1);
65+
final Pointer pointer = Pointer.callocAutoRelease(byteCapacity + 1, language);
6466

6567
return new NativeRope(
6668
pointer,
@@ -75,15 +77,15 @@ public NativeRope withByteLength(int newByteLength, int characterLength, CodeRan
7577
return new NativeRope(pointer, newByteLength, getEncoding(), characterLength, codeRange);
7678
}
7779

78-
public NativeRope makeCopy(RubyContext context) {
79-
final Pointer newPointer = copyNativePointer(context, pointer);
80+
public NativeRope makeCopy(RubyLanguage language) {
81+
final Pointer newPointer = copyNativePointer(language, pointer);
8082
return new NativeRope(newPointer, byteLength(), getEncoding(), characterLength(), getCodeRange());
8183
}
8284

83-
public NativeRope resize(RubyContext context, int newByteLength) {
85+
public NativeRope resize(RubyLanguage language, int newByteLength) {
8486
assert byteLength() != newByteLength;
8587

86-
final Pointer pointer = Pointer.mallocAutoRelease(newByteLength + 1);
88+
final Pointer pointer = Pointer.mallocAutoRelease(newByteLength + 1, language);
8789
pointer.writeBytes(0, this.pointer, 0, Math.min(getNativePointer().getSize(), newByteLength));
8890
pointer.writeByte(newByteLength, (byte) 0); // Like MRI
8991
return new NativeRope(pointer, newByteLength, getEncoding(), UNKNOWN_CHARACTER_LENGTH, CodeRange.CR_UNKNOWN);
@@ -94,9 +96,9 @@ public NativeRope resize(RubyContext context, int newByteLength) {
9496
* @param context the Ruby context
9597
* @param newCapacity the size in bytes minus one of the new pointer length
9698
* @return the new NativeRope */
97-
public NativeRope expandCapacity(RubyContext context, int newCapacity) {
99+
public NativeRope expandCapacity(RubyLanguage language, int newCapacity) {
98100
assert getCapacity() != newCapacity;
99-
final Pointer pointer = Pointer.mallocAutoRelease(newCapacity + 1);
101+
final Pointer pointer = Pointer.mallocAutoRelease(newCapacity + 1, language);
100102
pointer.writeBytes(0, this.pointer, 0, Math.min(getNativePointer().getSize(), newCapacity));
101103
pointer.writeByte(newCapacity, (byte) 0); // Like MRI
102104
return new NativeRope(

src/main/java/org/truffleruby/core/string/ImmutableRubyString.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
public class ImmutableRubyString extends ImmutableRubyObject implements TruffleObject {
4040

4141
public final LeafRope rope;
42-
private volatile NativeRope nativeRope = null;
4342
public final RubyEncoding encoding;
43+
private NativeRope nativeRope = null;
4444

4545
ImmutableRubyString(LeafRope rope, RubyEncoding encoding) {
4646
assert rope.encoding == encoding.jcoding;
@@ -58,16 +58,16 @@ public boolean isNative() {
5858
return nativeRope != null;
5959
}
6060

61-
public synchronized NativeRope getNativeRope() {
61+
public synchronized NativeRope getNativeRope(RubyLanguage language) {
6262
if (nativeRope == null) {
63-
nativeRope = createNativeRope();
63+
nativeRope = createNativeRope(language);
6464
}
6565
return nativeRope;
6666
}
6767

6868
@TruffleBoundary
69-
private NativeRope createNativeRope() {
70-
return new NativeRope(rope.getBytes(), rope.getEncoding(), rope.characterLength(), rope.getCodeRange());
69+
private NativeRope createNativeRope(RubyLanguage language) {
70+
return new NativeRope(rope.getBytes(), rope.getEncoding(), rope.characterLength(), rope.getCodeRange(), language);
7171
}
7272

7373
// region RubyStringLibrary messages

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ protected Object initializeCopyFromNative(RubyString self, Object from,
17511751
@CachedLibrary(limit = "LIBSTRING_CACHE") RubyStringLibrary stringsFrom,
17521752
@Cached @Shared("stringGetAssociatedNode") StringGetAssociatedNode stringGetAssociatedNode) {
17531753
self.setRope(
1754-
((NativeRope) stringsFrom.getRope(from)).makeCopy(getContext()),
1754+
((NativeRope) stringsFrom.getRope(from)).makeCopy(getLanguage()),
17551755
stringsFrom.getEncoding(from));
17561756
final Object associated = stringGetAssociatedNode.execute(from);
17571757
copyAssociated(self, associated);

src/main/java/org/truffleruby/extra/ffi/Pointer.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public static Pointer malloc(long size) {
3939
return new Pointer(UNSAFE.allocateMemory(size), size);
4040
}
4141

42-
/** Includes {@link #enableAutorelease(RubyContext)} and avoids locking for it */
43-
public static Pointer mallocAutoRelease(long size) {
44-
return new Pointer(UNSAFE.allocateMemory(size), size, true);
42+
/** Includes {@link #enableAutorelease(RubyLanguage)} and avoids locking for it */
43+
public static Pointer mallocAutoRelease(long size, RubyLanguage langauge) {
44+
return new Pointer(UNSAFE.allocateMemory(size), size, langauge);
4545
}
4646

4747
/** Allocates memory and produces a pointer to it. Clears the memory before returning it. Use {@link #malloc} if you
@@ -52,9 +52,9 @@ public static Pointer calloc(long size) {
5252
return pointer;
5353
}
5454

55-
/** Includes {@link #enableAutorelease(RubyContext)} and avoids locking for it */
56-
public static Pointer callocAutoRelease(long size) {
57-
final Pointer pointer = mallocAutoRelease(size);
55+
/** Includes {@link #enableAutorelease(RubyLanguage)} and avoids locking for it */
56+
public static Pointer callocAutoRelease(long size, RubyLanguage langauge) {
57+
final Pointer pointer = mallocAutoRelease(size, langauge);
5858
pointer.writeBytes(0, size, (byte) 0);
5959
return pointer;
6060
}
@@ -93,12 +93,10 @@ public Pointer(long address, long size) {
9393
this.size = size;
9494
}
9595

96-
private Pointer(long address, long size, boolean autoRelease) {
96+
private Pointer(long address, long size, RubyLanguage langauge) {
9797
this.address = address;
9898
this.size = size;
99-
if (autoRelease) {
100-
enableAutoreleaseUnsynchronized();
101-
}
99+
enableAutoreleaseUnsynchronized(langauge);
102100
}
103101

104102
public boolean isNull() {
@@ -344,20 +342,20 @@ public synchronized boolean isAutorelease() {
344342
}
345343

346344
@TruffleBoundary
347-
public synchronized void enableAutorelease(RubyContext context) {
345+
public synchronized void enableAutorelease(RubyLanguage language) {
348346
if (cleanable != null) {
349347
return;
350348
}
351349

352-
enableAutoreleaseUnsynchronized();
350+
enableAutoreleaseUnsynchronized(language);
353351
}
354352

355353
@TruffleBoundary
356-
private void enableAutoreleaseUnsynchronized() {
354+
private void enableAutoreleaseUnsynchronized(RubyLanguage language) {
357355
// We must be careful here that the finalizer does not capture the Pointer itself that we'd
358356
// like to finalize.
359357
autoReleaseState = new AutoReleaseState(address);
360-
cleanable = RubyLanguage.cleaner.register(this, autoReleaseState);
358+
cleanable = language.cleaner.register(this, autoReleaseState);
361359

362360
}
363361

src/main/java/org/truffleruby/extra/ffi/PointerNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public abstract static class PointerSetAutoreleaseNode extends CoreMethodArrayAr
198198

199199
@Specialization(guards = "autorelease")
200200
protected boolean enableAutorelease(RubyPointer pointer, boolean autorelease) {
201-
pointer.pointer.enableAutorelease(getContext());
201+
pointer.pointer.enableAutorelease(getLanguage());
202202
return autorelease;
203203
}
204204

0 commit comments

Comments
 (0)