Skip to content

Commit f40cbd2

Browse files
committed
Implement RB_GC_GUARD.
1 parent 3106ef8 commit f40cbd2

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

lib/cext/include/ruby/ruby.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,11 @@ MUST_INLINE int RB_FLOAT_TYPE_P(VALUE obj);
498498

499499
bool RB_TYPE_P(VALUE value, int type);
500500

501-
#define HAVE_RB_GC_GUARDED_PTR_VAL 1
502-
#define rb_gc_guarded_ptr_val(ptr, val) (ptr)
503-
#define RB_GC_GUARD(v) (v)
504-
#define RB_GC_GUARD_PTR(ptr) (ptr)
501+
#define RB_GC_GUARD(v) \
502+
(*__extension__ ({ \
503+
VALUE *rb_gc_guarded_ptr = rb_tr_gc_guard(v); \
504+
rb_gc_guarded_ptr; \
505+
}))
505506

506507
#ifdef __GNUC__
507508
#define RB_UNUSED_VAR(x) x __attribute__ ((unused))

lib/cext/include/truffleruby/truffleruby-pre.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ MUST_INLINE VALUE rb_tr_unwrap(VALUE object) {
4848
return polyglot_invoke(RUBY_CEXT, "rb_tr_unwrap", object);
4949
}
5050

51+
// Needed for GC guarding
52+
53+
MUST_INLINE VALUE *rb_tr_gc_guard(VALUE v) {
54+
VALUE *ptr = &v;
55+
polyglot_invoke(RUBY_CEXT, "rb_tr_gc_guard", v);
56+
return ptr;
57+
}
58+
5159
#include <ruby/thread_native.h>
5260

5361
// Helpers

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,32 @@ protected UnwrapNode createUnwrapNode() {
13841384
}
13851385
}
13861386

1387+
@CoreMethod(names = "rb_tr_gc_guard", onSingleton = true, required = 1)
1388+
public abstract static class GCGuardNode extends CoreMethodArrayArgumentsNode {
1389+
1390+
@Specialization
1391+
public DynamicObject addToMarkList(VirtualFrame frmae, Object guardedObject,
1392+
@Cached("create()") BranchProfile exceptionProfile,
1393+
@Cached("create()") UnwrapNode.ToWrapperNode toWrapperNode) {
1394+
ValueWrapper wrappedValue = toWrapperNode.execute(guardedObject);
1395+
if (wrappedValue == null) {
1396+
exceptionProfile.enter();
1397+
throw new RaiseException(getContext(), coreExceptions().runtimeError(exceptionMessage(guardedObject), this));
1398+
}
1399+
getContext().getMarkingService().keepObject(guardedObject);
1400+
return nil();
1401+
}
1402+
1403+
@TruffleBoundary
1404+
private String exceptionMessage(Object value) {
1405+
return String.format("native handle not found (%s) to guard it", value);
1406+
}
1407+
1408+
protected UnwrapNode createUnwrapNode() {
1409+
return UnwrapNodeGen.create();
1410+
}
1411+
}
1412+
13871413
@CoreMethod(names = "set_mark_list_on_object", onSingleton = true, required = 1)
13881414
public abstract static class SetMarkList extends CoreMethodArrayArgumentsNode {
13891415

0 commit comments

Comments
 (0)