summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Kanis <lars@greiz-reinsdorf.de>2023-03-04 07:16:06 +0100
committerGitHub <noreply@github.com>2023-03-04 07:16:06 +0100
commite1cf98e4c0d0cb01c6c936a33578288df735090d (patch)
tree7642340d9a271d76a02d75e1aafee57dfc6f5141
parent5daf2d381badcafcc71f18832d7933a9dab4dadb (diff)
parentf7e0004e802b459e42e8233771b4cd7403859525 (diff)
downloadffi-e1cf98e4c0d0cb01c6c936a33578288df735090d.tar.gz
Merge pull request #1000 from larskanis/typeddata
Convert FFI::DynamicLibrary to TypedData
-rw-r--r--ext/ffi_c/DynamicLibrary.c26
-rw-r--r--ext/ffi_c/StructLayout.c6
2 files changed, 20 insertions, 12 deletions
diff --git a/ext/ffi_c/DynamicLibrary.c b/ext/ffi_c/DynamicLibrary.c
index eb55fc2..108394c 100644
--- a/ext/ffi_c/DynamicLibrary.c
+++ b/ext/ffi_c/DynamicLibrary.c
@@ -56,13 +56,23 @@ typedef struct LibrarySymbol_ {
static VALUE library_initialize(VALUE self, VALUE libname, VALUE libflags);
-static void library_free(Library* lib);
+static void library_free(void *);
static VALUE symbol_allocate(VALUE klass);
static VALUE symbol_new(VALUE library, void* address, VALUE name);
static void symbol_mark(void *data);
+static const rb_data_type_t rbffi_library_data_type = {
+ .wrap_struct_name = "FFI::DynamicLibrary",
+ .function = {
+ .dmark = NULL,
+ .dfree = library_free,
+ .dsize = NULL,
+ },
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
+};
+
static const rb_data_type_t library_symbol_data_type = {
.wrap_struct_name = "FFI::DynamicLibrary::Symbol",
.function = {
@@ -92,7 +102,7 @@ static VALUE
library_allocate(VALUE klass)
{
Library* library;
- return Data_Make_Struct(klass, Library, NULL, library_free, library);
+ return TypedData_Make_Struct(klass, Library, &rbffi_library_data_type, library);
}
/*
@@ -125,9 +135,9 @@ library_initialize(VALUE self, VALUE libname, VALUE libflags)
Check_Type(libflags, T_FIXNUM);
- Data_Get_Struct(self, Library, library);
+ TypedData_Get_Struct(self, Library, &rbffi_library_data_type, library);
flags = libflags != Qnil ? NUM2UINT(libflags) : 0;
-
+
library->handle = dl_open(libname != Qnil ? StringValueCStr(libname) : NULL, flags);
if (library->handle == NULL) {
char errmsg[1024];
@@ -156,9 +166,9 @@ library_dlsym(VALUE self, VALUE name)
void* address = NULL;
Check_Type(name, T_STRING);
- Data_Get_Struct(self, Library, library);
+ TypedData_Get_Struct(self, Library, &rbffi_library_data_type, library);
address = dl_sym(library->handle, StringValueCStr(name));
-
+
return address != NULL ? symbol_new(self, address, name) : Qnil;
}
@@ -175,8 +185,10 @@ library_dlerror(VALUE self)
}
static void
-library_free(Library* library)
+library_free(void *data)
{
+ Library *library = (Library*)data;
+
/* dlclose() on MacOS tends to segfault - avoid it */
#ifndef __APPLE__
if (library->handle != NULL) {
diff --git a/ext/ffi_c/StructLayout.c b/ext/ffi_c/StructLayout.c
index 08d01b6..2371de6 100644
--- a/ext/ffi_c/StructLayout.c
+++ b/ext/ffi_c/StructLayout.c
@@ -406,12 +406,8 @@ array_field_put(VALUE self, VALUE pointer, VALUE value)
VALUE entry = rb_ary_entry(value, i);
Struct* s;
- if (!rb_obj_is_kind_of(entry, rbffi_StructClass)) {
- rb_raise(rb_eTypeError, "array element not an instance of FFI::Struct");
- break;
- }
+ TypedData_Get_Struct(entry, Struct, &rbffi_struct_data_type, s);
- Data_Get_Struct(entry, Struct, s);
checkRead(s->pointer);
checkBounds(s->pointer, 0, array->componentType->ffiType->size);