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
40 changes: 26 additions & 14 deletions ext/ffi_c/AbstractMemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,22 @@ VALUE rbffi_AbstractMemoryClass = Qnil;
static VALUE NullPointerErrorClass = Qnil;
static ID id_to_ptr = 0, id_plus = 0, id_call = 0;

const rb_data_type_t rbffi_abstract_memory_data_type = { /* extern */
.wrap_struct_name = "FFI::AbstractMemory",
.function = {
.dmark = NULL,
.dfree = RUBY_TYPED_DEFAULT_FREE,
.dsize = NULL,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY
};

static VALUE
memory_allocate(VALUE klass)
{
AbstractMemory* memory;
VALUE obj;
obj = Data_Make_Struct(klass, AbstractMemory, NULL, -1, memory);
obj = TypedData_Make_Struct(klass, AbstractMemory, &rbffi_abstract_memory_data_type, memory);
memory->flags = MEM_RD | MEM_WR;

return obj;
Expand All @@ -87,7 +97,7 @@ static VALUE \
memory_put_##name(VALUE self, VALUE offset, VALUE value) \
{ \
AbstractMemory* memory; \
Data_Get_Struct(self, AbstractMemory, memory); \
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, memory); \
memory_op_put_##name(memory, NUM2LONG(offset), value); \
return self; \
} \
Expand All @@ -96,7 +106,7 @@ static VALUE \
memory_write_##name(VALUE self, VALUE value) \
{ \
AbstractMemory* memory; \
Data_Get_Struct(self, AbstractMemory, memory); \
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, memory); \
memory_op_put_##name(memory, 0, value); \
return self; \
} \
Expand All @@ -115,15 +125,15 @@ static VALUE \
memory_get_##name(VALUE self, VALUE offset) \
{ \
AbstractMemory* memory; \
Data_Get_Struct(self, AbstractMemory, memory); \
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, memory); \
return memory_op_get_##name(memory, NUM2LONG(offset)); \
} \
static VALUE memory_read_##name(VALUE self); \
static VALUE \
memory_read_##name(VALUE self) \
{ \
AbstractMemory* memory; \
Data_Get_Struct(self, AbstractMemory, memory); \
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, memory); \
return memory_op_get_##name(memory, 0); \
} \
static MemoryOp memory_op_##name = { memory_op_get_##name, memory_op_put_##name }; \
Expand Down Expand Up @@ -321,7 +331,7 @@ memory_size(VALUE self)
{
AbstractMemory* ptr;

Data_Get_Struct(self, AbstractMemory, ptr);
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, ptr);

return LONG2NUM(ptr->size);
}
Expand All @@ -344,7 +354,7 @@ memory_get(VALUE self, VALUE type_name, VALUE offset)
nType = rbffi_Type_Lookup(type_name);
if(NIL_P(nType)) goto undefined_type;

Data_Get_Struct(self, AbstractMemory, ptr);
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, ptr);
Data_Get_Struct(nType, Type, type);

MemoryOp *op = get_memory_op(type);
Expand Down Expand Up @@ -376,7 +386,7 @@ memory_put(VALUE self, VALUE type_name, VALUE offset, VALUE value)
nType = rbffi_Type_Lookup(type_name);
if(NIL_P(nType)) goto undefined_type;

Data_Get_Struct(self, AbstractMemory, ptr);
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, ptr);
Data_Get_Struct(nType, Type, type);

MemoryOp *op = get_memory_op(type);
Expand Down Expand Up @@ -442,7 +452,7 @@ memory_get_array_of_string(int argc, VALUE* argv, VALUE self)
count = (countnum == Qnil ? 0 : NUM2INT(countnum));
retVal = rb_ary_new2(count);

Data_Get_Struct(self, AbstractMemory, ptr);
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, ptr);
checkRead(ptr);

if (countnum != Qnil) {
Expand Down Expand Up @@ -633,7 +643,7 @@ memory_type_size(VALUE self)
{
AbstractMemory* ptr;

Data_Get_Struct(self, AbstractMemory, ptr);
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, ptr);

return INT2NUM(ptr->typeSize);
}
Expand All @@ -651,7 +661,7 @@ memory_aref(VALUE self, VALUE idx)
AbstractMemory* ptr;
VALUE rbOffset = Qnil;

Data_Get_Struct(self, AbstractMemory, ptr);
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, ptr);

rbOffset = ULONG2NUM(NUM2ULONG(idx) * ptr->typeSize);

Expand All @@ -661,15 +671,17 @@ memory_aref(VALUE self, VALUE idx)
static inline char*
memory_address(VALUE obj)
{
return ((AbstractMemory *) DATA_PTR(obj))->address;
AbstractMemory *mem;
TypedData_Get_Struct(obj, AbstractMemory, &rbffi_abstract_memory_data_type, mem);
return mem->address;
}

static VALUE
memory_copy_from(VALUE self, VALUE rbsrc, VALUE rblen)
{
AbstractMemory* dst;

Data_Get_Struct(self, AbstractMemory, dst);
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, dst);

memcpy(dst->address, rbffi_AbstractMemory_Cast(rbsrc, rbffi_AbstractMemoryClass)->address, NUM2INT(rblen));

Expand All @@ -681,7 +693,7 @@ rbffi_AbstractMemory_Cast(VALUE obj, VALUE klass)
{
if (rb_obj_is_kind_of(obj, klass)) {
AbstractMemory* memory;
Data_Get_Struct(obj, AbstractMemory, memory);
TypedData_Get_Struct(obj, AbstractMemory, &rbffi_abstract_memory_data_type, memory);
return memory;
}

Expand Down
1 change: 1 addition & 0 deletions ext/ffi_c/AbstractMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct AbstractMemory_ {
};


extern const rb_data_type_t rbffi_abstract_memory_data_type;
extern VALUE rbffi_AbstractMemoryClass;
extern MemoryOps rbffi_AbstractMemoryOps;

Expand Down
57 changes: 41 additions & 16 deletions ext/ffi_c/Buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,33 @@ typedef struct Buffer {

static VALUE buffer_allocate(VALUE klass);
static VALUE buffer_initialize(int argc, VALUE* argv, VALUE self);
static void buffer_release(Buffer* ptr);
static void buffer_mark(Buffer* ptr);
static void buffer_release(void *data);
static void buffer_mark(void *data);
static VALUE buffer_free(VALUE self);

static const rb_data_type_t buffer_data_type = {
.wrap_struct_name = "FFI::Buffer",
.function = {
.dmark = buffer_mark,
.dfree = RUBY_TYPED_DEFAULT_FREE,
.dsize = NULL,
},
.parent = &rbffi_abstract_memory_data_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY
};

static const rb_data_type_t allocated_buffer_data_type = {
.wrap_struct_name = "FFI::Buffer(allocated)",
.function = {
.dmark = NULL,
.dfree = buffer_release,
.dsize = NULL,
},
.parent = &buffer_data_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY
};


static VALUE BufferClass = Qnil;

static VALUE
Expand All @@ -61,16 +84,17 @@ buffer_allocate(VALUE klass)
Buffer* buffer;
VALUE obj;

obj = Data_Make_Struct(klass, Buffer, NULL, buffer_release, buffer);
obj = TypedData_Make_Struct(klass, Buffer, &allocated_buffer_data_type, buffer);
buffer->data.rbParent = Qnil;
buffer->memory.flags = MEM_RD | MEM_WR;

return obj;
}

static void
buffer_release(Buffer* ptr)
buffer_release(void *data)
{
Buffer *ptr = (Buffer *)data;
if ((ptr->memory.flags & MEM_EMBED) == 0 && ptr->data.storage != NULL) {
xfree(ptr->data.storage);
ptr->data.storage = NULL;
Expand All @@ -95,7 +119,7 @@ buffer_initialize(int argc, VALUE* argv, VALUE self)
Buffer* p;
int nargs;

Data_Get_Struct(self, Buffer, p);
TypedData_Get_Struct(self, Buffer, &buffer_data_type, p);

nargs = rb_scan_args(argc, argv, "12", &rbSize, &rbCount, &rbClear);
p->memory.typeSize = rbffi_type_size(rbSize);
Expand Down Expand Up @@ -137,8 +161,8 @@ buffer_initialize_copy(VALUE self, VALUE other)
{
AbstractMemory* src;
Buffer* dst;
Data_Get_Struct(self, Buffer, dst);

TypedData_Get_Struct(self, Buffer, &buffer_data_type, dst);
src = rbffi_AbstractMemory_Cast(other, BufferClass);
if ((dst->memory.flags & MEM_EMBED) == 0 && dst->data.storage != NULL) {
xfree(dst->data.storage);
Expand Down Expand Up @@ -171,11 +195,11 @@ slice(VALUE self, long offset, long len)
Buffer* ptr;
Buffer* result;
VALUE obj = Qnil;
Data_Get_Struct(self, Buffer, ptr);

TypedData_Get_Struct(self, Buffer, &buffer_data_type, ptr);
checkBounds(&ptr->memory, offset, len);

obj = Data_Make_Struct(BufferClass, Buffer, buffer_mark, -1, result);
obj = TypedData_Make_Struct(BufferClass, Buffer, &buffer_data_type, result);
result->memory.address = ptr->memory.address + offset;
result->memory.size = len;
result->memory.flags = ptr->memory.flags;
Expand All @@ -197,7 +221,7 @@ buffer_plus(VALUE self, VALUE rbOffset)
Buffer* ptr;
long offset = NUM2LONG(rbOffset);

Data_Get_Struct(self, Buffer, ptr);
TypedData_Get_Struct(self, Buffer, &buffer_data_type, ptr);

return slice(self, offset, ptr->memory.size - offset);
}
Expand Down Expand Up @@ -226,7 +250,7 @@ buffer_inspect(VALUE self)
char tmp[100];
Buffer* ptr;

Data_Get_Struct(self, Buffer, ptr);
TypedData_Get_Struct(self, Buffer, &buffer_data_type, ptr);

snprintf(tmp, sizeof(tmp), "#<FFI:Buffer:%p address=%p size=%ld>", ptr, ptr->memory.address, ptr->memory.size);

Expand Down Expand Up @@ -255,7 +279,7 @@ buffer_order(int argc, VALUE* argv, VALUE self)
{
Buffer* ptr;

Data_Get_Struct(self, Buffer, ptr);
TypedData_Get_Struct(self, Buffer, &buffer_data_type, ptr);
if (argc == 0) {
int order = (ptr->memory.flags & MEM_SWAP) == 0 ? BYTE_ORDER : SWAPPED_ORDER;
return order == BIG_ENDIAN ? ID2SYM(rb_intern("big")) : ID2SYM(rb_intern("little"));
Expand All @@ -279,7 +303,7 @@ buffer_order(int argc, VALUE* argv, VALUE self)
Buffer* p2;
VALUE retval = slice(self, 0, ptr->memory.size);

Data_Get_Struct(retval, Buffer, p2);
TypedData_Get_Struct(retval, Buffer, &buffer_data_type, p2);
p2->memory.flags |= MEM_SWAP;
return retval;
}
Expand All @@ -294,7 +318,7 @@ buffer_free(VALUE self)
{
Buffer* ptr;

Data_Get_Struct(self, Buffer, ptr);
TypedData_Get_Struct(self, Buffer, &buffer_data_type, ptr);
if ((ptr->memory.flags & MEM_EMBED) == 0 && ptr->data.storage != NULL) {
xfree(ptr->data.storage);
ptr->data.storage = NULL;
Expand All @@ -304,8 +328,9 @@ buffer_free(VALUE self)
}

static void
buffer_mark(Buffer* ptr)
buffer_mark(void *data)
{
Buffer *ptr = (Buffer *)data;
rb_gc_mark(ptr->data.rbParent);
}

Expand Down
14 changes: 10 additions & 4 deletions ext/ffi_c/Call.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,9 @@ getPointer(VALUE value, int type)
{
if (likely(type == T_DATA && rb_obj_is_kind_of(value, rbffi_AbstractMemoryClass))) {

return ((AbstractMemory *) DATA_PTR(value))->address;
AbstractMemory *mem;
TypedData_Get_Struct(value, AbstractMemory, &rbffi_abstract_memory_data_type, mem);
return mem->address;

} else if (type == T_DATA && rb_obj_is_kind_of(value, rbffi_StructClass)) {

Expand All @@ -439,7 +441,9 @@ getPointer(VALUE value, int type)

VALUE ptr = rb_funcall2(value, id_to_ptr, 0, NULL);
if (rb_obj_is_kind_of(ptr, rbffi_AbstractMemoryClass) && TYPE(ptr) == T_DATA) {
return ((AbstractMemory *) DATA_PTR(ptr))->address;
AbstractMemory *mem;
TypedData_Get_Struct(ptr, AbstractMemory, &rbffi_abstract_memory_data_type, mem);
return mem->address;
}
rb_raise(rb_eArgError, "to_ptr returned an invalid pointer");
}
Expand All @@ -466,14 +470,16 @@ callback_param(VALUE proc, VALUE cbInfo)
/* Handle Function pointers here */
if (rb_obj_is_kind_of(proc, rbffi_FunctionClass)) {
AbstractMemory* ptr;
Data_Get_Struct(proc, AbstractMemory, ptr);
TypedData_Get_Struct(proc, AbstractMemory, &rbffi_abstract_memory_data_type, ptr);
return ptr->address;
}

callback = rbffi_Function_ForProc(cbInfo, proc);
RB_GC_GUARD(callback);

return ((AbstractMemory *) DATA_PTR(callback))->address;
AbstractMemory *mem;
TypedData_Get_Struct(callback, AbstractMemory, &rbffi_abstract_memory_data_type, mem);
return mem->address;
}


Expand Down
Loading