ruby-changes:66785
From: Kenta <ko1@a...>
Date: Wed, 14 Jul 2021 19:00:39 +0900 (JST)
Subject: [ruby-changes:66785] 818c74b7f4 (master): [ruby/fiddle] Return the module handle value in Fiddle::Handle#to_i and add FIddle::Handle#to_ptr (https://github.com/ruby/fiddle/pull/87)
https://git.ruby-lang.org/ruby.git/commit/?id=818c74b7f4 From 818c74b7f4d5b88833af26226fc81e563b5d11b9 Mon Sep 17 00:00:00 2001 From: Kenta Murata <mrkn@u...> Date: Wed, 14 Jul 2021 11:26:52 +0900 Subject: [ruby/fiddle] Return the module handle value in Fiddle::Handle#to_i and add FIddle::Handle#to_ptr (https://github.com/ruby/fiddle/pull/87) https://github.com/ruby/fiddle/commit/170111a0cb --- ext/fiddle/fiddle.h | 3 +++ ext/fiddle/handle.c | 17 ++++++++++++++++- ext/fiddle/pointer.c | 16 ++++++++++++---- test/fiddle/test_handle.rb | 6 ++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/ext/fiddle/fiddle.h b/ext/fiddle/fiddle.h index e8ed4ef..823689e 100644 --- a/ext/fiddle/fiddle.h +++ b/ext/fiddle/fiddle.h @@ -198,5 +198,8 @@ extern VALUE rb_eFiddleDLError; https://github.com/ruby/ruby/blob/trunk/ext/fiddle/fiddle.h#L198 VALUE rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type); +typedef void (*rb_fiddle_freefunc_t)(void*); +VALUE rb_fiddle_ptr_new_wrap(void *ptr, long size, rb_fiddle_freefunc_t func, VALUE wrap0, VALUE wrap1); + #endif /* vim: set noet sws=4 sw=4: */ diff --git a/ext/fiddle/handle.c b/ext/fiddle/handle.c index c1b2db5..a4a32f1 100644 --- a/ext/fiddle/handle.c +++ b/ext/fiddle/handle.c @@ -259,7 +259,21 @@ rb_fiddle_handle_to_i(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/fiddle/handle.c#L259 struct dl_handle *fiddle_handle; TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle); - return PTR2NUM(fiddle_handle); + return PTR2NUM(fiddle_handle->ptr); +} + +/* + * call-seq: to_ptr + * + * Returns the Fiddle::Pointer of this handle. + */ +static VALUE +rb_fiddle_handle_to_ptr(VALUE self) +{ + struct dl_handle *fiddle_handle; + + TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle); + return rb_fiddle_ptr_new_wrap(fiddle_handle->ptr, 0, 0, self, 0); } static VALUE fiddle_handle_sym(void *handle, VALUE symbol); @@ -466,6 +480,7 @@ Init_fiddle_handle(void) https://github.com/ruby/ruby/blob/trunk/ext/fiddle/handle.c#L480 rb_define_method(rb_cHandle, "initialize", rb_fiddle_handle_initialize, -1); rb_define_method(rb_cHandle, "to_i", rb_fiddle_handle_to_i, 0); + rb_define_method(rb_cHandle, "to_ptr", rb_fiddle_handle_to_ptr, 0); rb_define_method(rb_cHandle, "close", rb_fiddle_handle_close, 0); rb_define_method(rb_cHandle, "sym", rb_fiddle_handle_sym, 1); rb_define_method(rb_cHandle, "[]", rb_fiddle_handle_sym, 1); diff --git a/ext/fiddle/pointer.c b/ext/fiddle/pointer.c index ae9eb16..9d05c13 100644 --- a/ext/fiddle/pointer.c +++ b/ext/fiddle/pointer.c @@ -24,7 +24,7 @@ https://github.com/ruby/ruby/blob/trunk/ext/fiddle/pointer.c#L24 VALUE rb_cPointer; -typedef void (*freefunc_t)(void*); +typedef rb_fiddle_freefunc_t freefunc_t; struct ptr_data { void *ptr; @@ -125,7 +125,7 @@ static const rb_memory_view_entry_t fiddle_ptr_memory_view_entry = { https://github.com/ruby/ruby/blob/trunk/ext/fiddle/pointer.c#L125 #endif static VALUE -rb_fiddle_ptr_new2(VALUE klass, void *ptr, long size, freefunc_t func) +rb_fiddle_ptr_new2(VALUE klass, void *ptr, long size, freefunc_t func, VALUE wrap0, VALUE wrap1) { struct ptr_data *data; VALUE val; @@ -135,14 +135,22 @@ rb_fiddle_ptr_new2(VALUE klass, void *ptr, long size, freefunc_t func) https://github.com/ruby/ruby/blob/trunk/ext/fiddle/pointer.c#L135 data->free = func; data->freed = false; data->size = size; + data->wrap[0] = wrap0; + data->wrap[1] = wrap1; return val; } +VALUE +rb_fiddle_ptr_new_wrap(void *ptr, long size, freefunc_t func, VALUE wrap0, VALUE wrap1) +{ + return rb_fiddle_ptr_new2(rb_cPointer, ptr, size, func, wrap0, wrap1); +} + static VALUE rb_fiddle_ptr_new(void *ptr, long size, freefunc_t func) { - return rb_fiddle_ptr_new2(rb_cPointer, ptr, size, func); + return rb_fiddle_ptr_new2(rb_cPointer, ptr, size, func, 0, 0); } static VALUE @@ -152,7 +160,7 @@ rb_fiddle_ptr_malloc(VALUE klass, long size, freefunc_t func) https://github.com/ruby/ruby/blob/trunk/ext/fiddle/pointer.c#L160 ptr = ruby_xmalloc((size_t)size); memset(ptr,0,(size_t)size); - return rb_fiddle_ptr_new2(klass, ptr, size, func); + return rb_fiddle_ptr_new2(klass, ptr, size, func, 0, 0); } static void * diff --git a/test/fiddle/test_handle.rb b/test/fiddle/test_handle.rb index 17f9c92..e89ad53 100644 --- a/test/fiddle/test_handle.rb +++ b/test/fiddle/test_handle.rb @@ -13,6 +13,12 @@ module Fiddle https://github.com/ruby/ruby/blob/trunk/test/fiddle/test_handle.rb#L13 assert_kind_of Integer, handle.to_i end + def test_to_ptr + handle = Fiddle::Handle.new(LIBC_SO) + ptr = handle.to_ptr + assert_equal ptr.to_i, handle.to_i + end + def test_static_sym_unknown assert_raise(DLError) { Fiddle::Handle.sym('fooo') } assert_raise(DLError) { Fiddle::Handle['fooo'] } -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/