Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions ext/ffi_c/DynamicLibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
6 changes: 1 addition & 5 deletions ext/ffi_c/StructLayout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down