Skip to content

Commit b89ca3f

Browse files
committed
[GR-26781] Implement rb_imemo_tmpbuf_auto_free_pointer() which is needed by Ripper
PullRequest: truffleruby/2152
2 parents 75b0456 + d74dc6d commit b89ca3f

File tree

8 files changed

+35
-30
lines changed

8 files changed

+35
-30
lines changed

lib/cext/include/internal.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,10 @@ typedef struct rb_imemo_tmpbuf_struct {
6565
size_t cnt; /* buffer size in VALUE */
6666
} rb_imemo_tmpbuf_t;
6767

68-
#define rb_imemo_tmpbuf_auto_free_pointer() rb_imemo_new(imemo_tmpbuf, 0, 0, 0, 0)
69-
rb_imemo_tmpbuf_t *rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
68+
VALUE rb_imemo_tmpbuf_auto_free_pointer(void);
69+
void* rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr);
7070

71-
static inline
72-
void* rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr) {
73-
return ((rb_imemo_tmpbuf_t *)v)->ptr = ptr;
74-
}
71+
rb_imemo_tmpbuf_t *rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
7572

7673
#if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE) || defined(_WIN32)
7774
#define ruby_sized_xrealloc(ptr, new_size, old_size) ruby_xrealloc(ptr, new_size)

lib/truffle/truffle/cext.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,4 +1785,23 @@ def rb_sprintf(format, *args)
17851785
def test_cext_wrap(value)
17861786
Primitive.cext_wrap(value)
17871787
end
1788+
1789+
# An object that once unreachable frees the associated native pointer using ruby_xfree()
1790+
class IMemoTmpBufAutoFree
1791+
def pointer=(address)
1792+
ObjectSpace.define_finalizer(self, self.class.free(address))
1793+
end
1794+
1795+
def self.free(address)
1796+
-> _id { LIBTRUFFLERUBY.ruby_xfree(address) }
1797+
end
1798+
end
1799+
1800+
def rb_imemo_tmpbuf_auto_free_pointer
1801+
IMemoTmpBufAutoFree.new
1802+
end
1803+
1804+
def rb_imemo_tmpbuf_set_ptr(obj, ptr)
1805+
obj.pointer = ptr
1806+
end
17881807
end

src/main/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ zlib/zlib.$(DLEXT): zlib/Makefile zlib/zlib.c
8484
ripper/Makefile: ripper/extconf.rb $(EXTCONF_DEPS)
8585
$(Q) cd ripper && $(RUBY) extconf.rb || $(IF_EXTCONF_FAIL)
8686

87-
ripper/ripper.$(DLEXT): ripper/Makefile
87+
ripper/ripper.$(DLEXT): ripper/Makefile ripper/*.c ripper/*.h
8888
$(Q) cd ripper && $(MAKE) $(MKMF_MAKEFILE_SUBPROCESS_FLAGS)
8989

9090
# syslog

src/main/c/cext/alloc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@ void rb_mem_clear(VALUE *mem, long n) {
7474
mem[i] = Qnil;
7575
}
7676
}
77+
78+
VALUE rb_imemo_tmpbuf_auto_free_pointer(void) {
79+
return RUBY_CEXT_INVOKE("rb_imemo_tmpbuf_auto_free_pointer");
80+
}
81+
82+
void* rb_imemo_tmpbuf_set_ptr(VALUE imemo, void *ptr) {
83+
polyglot_invoke(RUBY_CEXT, "rb_imemo_tmpbuf_set_ptr", rb_tr_unwrap(imemo), ptr);
84+
return ptr;
85+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.truffleruby.cext.ValueWrapperManager.TRUE_HANDLE;
1414
import static org.truffleruby.cext.ValueWrapperManager.UNDEF_HANDLE;
1515

16+
import com.oracle.truffle.api.CompilerDirectives;
1617
import org.truffleruby.RubyContext;
1718
import org.truffleruby.RubyLanguage;
1819
import org.truffleruby.cext.UnwrapNodeGen.NativeToWrapperNodeGen;
@@ -88,15 +89,15 @@ protected Object unwrapTaggedObject(long handle,
8889

8990
@TruffleBoundary
9091
private void raiseError(long handle) {
91-
throw new RuntimeException("dead handle 0x" + Long.toHexString(handle));
92+
throw CompilerDirectives.shouldNotReachHere("dead handle 0x" + Long.toHexString(handle));
9293
}
9394

9495
@Fallback
9596
@TruffleBoundary
9697
protected ValueWrapper unWrapUnexpectedHandle(long handle) {
9798
// Avoid throwing a specialization exception when given an uninitialized or corrupt
9899
// handle.
99-
throw new RuntimeException("corrupt handle 0x" + Long.toHexString(handle));
100+
throw CompilerDirectives.shouldNotReachHere("corrupt handle 0x" + Long.toHexString(handle));
100101
}
101102

102103
public static UnwrapNativeNode create() {

test/mri/excludes/Ripper/TestInput.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
exclude :test_aryptn, "needs investigation"
21
exclude :test_block_variables, "needs investigation"
3-
exclude :test_event_coverage, "needs investigation"

test/mri/excludes/TestRipper/Sexp.rb

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)