diff options
Diffstat (limited to 'ext/ffi_c/Buffer.c')
-rw-r--r-- | ext/ffi_c/Buffer.c | 57 |
1 files changed, 41 insertions, 16 deletions
diff --git a/ext/ffi_c/Buffer.c b/ext/ffi_c/Buffer.c index b5f39a4..e3ed90b 100644 --- a/ext/ffi_c/Buffer.c +++ b/ext/ffi_c/Buffer.c @@ -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 @@ -61,7 +84,7 @@ 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; @@ -69,8 +92,9 @@ buffer_allocate(VALUE klass) } 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; @@ -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); @@ -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); @@ -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; @@ -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); } @@ -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); @@ -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")); @@ -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; } @@ -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; @@ -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); } |