summaryrefslogtreecommitdiff
path: root/ext/fiddle
diff options
context:
space:
mode:
authorKenta Murata <mrkn@users.noreply.github.com>2021-07-14 11:26:52 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-07-14 18:43:32 +0900
commit818c74b7f4d5b88833af26226fc81e563b5d11b9 (patch)
tree8b5271350f2d3394e43e648ce3dd70fbaf2eed80 /ext/fiddle
parent57a743efa408f0c47fc18dd16758a1a1cd54d296 (diff)
[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
Diffstat (limited to 'ext/fiddle')
-rw-r--r--ext/fiddle/fiddle.h3
-rw-r--r--ext/fiddle/handle.c17
-rw-r--r--ext/fiddle/pointer.c16
3 files changed, 31 insertions, 5 deletions
diff --git a/ext/fiddle/fiddle.h b/ext/fiddle/fiddle.h
index e8ed4ef2a3b..823689e0910 100644
--- a/ext/fiddle/fiddle.h
+++ b/ext/fiddle/fiddle.h
@@ -198,5 +198,8 @@ extern VALUE rb_eFiddleDLError;
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 c1b2db557a6..a4a32f1ecba 100644
--- a/ext/fiddle/handle.c
+++ b/ext/fiddle/handle.c
@@ -259,7 +259,21 @@ rb_fiddle_handle_to_i(VALUE self)
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)
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 ae9eb166ecf..9d05c137b05 100644
--- a/ext/fiddle/pointer.c
+++ b/ext/fiddle/pointer.c
@@ -24,7 +24,7 @@
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 = {
#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)
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)
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 *