summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Kanis <lars@greiz-reinsdorf.de>2023-03-06 21:55:58 +0100
committerGitHub <noreply@github.com>2023-03-06 21:55:58 +0100
commit9c704ce922352d623f405d7f18c3c9247079af5d (patch)
tree398eca226b25d750446395ecbfb9266d2ef96d2c
parentb10b794cb143b54ce7dad04509349d331ae85162 (diff)
parentc6ba1bed4d25878e51385cebae415f522e584a91 (diff)
downloadffi-9c704ce922352d623f405d7f18c3c9247079af5d.tar.gz
Merge pull request #1012 from larskanis/type-check-by-typed-data
Use type checking by TypedData and remove duplocated check by rb_obj_is_kind_of
-rw-r--r--ext/ffi_c/AbstractMemory.c15
-rw-r--r--ext/ffi_c/AbstractMemory.h4
-rw-r--r--ext/ffi_c/Buffer.c20
-rw-r--r--ext/ffi_c/MemoryPointer.c2
-rw-r--r--ext/ffi_c/Pointer.c2
-rw-r--r--ext/ffi_c/Variadic.c2
6 files changed, 20 insertions, 25 deletions
diff --git a/ext/ffi_c/AbstractMemory.c b/ext/ffi_c/AbstractMemory.c
index 5f09b9e..3cf16a7 100644
--- a/ext/ffi_c/AbstractMemory.c
+++ b/ext/ffi_c/AbstractMemory.c
@@ -682,22 +682,17 @@ memory_copy_from(VALUE self, VALUE rbsrc, VALUE rblen)
TypedData_Get_Struct(self, AbstractMemory, &rbffi_abstract_memory_data_type, dst);
- memcpy(dst->address, rbffi_AbstractMemory_Cast(rbsrc, rbffi_AbstractMemoryClass)->address, NUM2INT(rblen));
+ memcpy(dst->address, rbffi_AbstractMemory_Cast(rbsrc, &rbffi_abstract_memory_data_type)->address, NUM2INT(rblen));
return self;
}
AbstractMemory*
-rbffi_AbstractMemory_Cast(VALUE obj, VALUE klass)
+rbffi_AbstractMemory_Cast(VALUE obj, const rb_data_type_t *data_type)
{
- if (rb_obj_is_kind_of(obj, klass)) {
- AbstractMemory* memory;
- TypedData_Get_Struct(obj, AbstractMemory, &rbffi_abstract_memory_data_type, memory);
- return memory;
- }
-
- rb_raise(rb_eArgError, "Invalid Memory object");
- return NULL;
+ AbstractMemory* memory;
+ TypedData_Get_Struct(obj, AbstractMemory, data_type, memory);
+ return memory;
}
void
diff --git a/ext/ffi_c/AbstractMemory.h b/ext/ffi_c/AbstractMemory.h
index 8c75373..5973bac 100644
--- a/ext/ffi_c/AbstractMemory.h
+++ b/ext/ffi_c/AbstractMemory.h
@@ -92,7 +92,7 @@ extern MemoryOps rbffi_AbstractMemoryOps;
extern void rbffi_AbstractMemory_Init(VALUE ffiModule);
-extern AbstractMemory* rbffi_AbstractMemory_Cast(VALUE obj, VALUE klass);
+extern AbstractMemory* rbffi_AbstractMemory_Cast(VALUE obj, const rb_data_type_t *data_type);
extern void rbffi_AbstractMemory_Error(AbstractMemory *, int op);
@@ -162,7 +162,7 @@ get_memory_op(Type* type)
}
}
-#define MEMORY(obj) rbffi_AbstractMemory_Cast((obj), rbffi_AbstractMemoryClass)
+#define MEMORY(obj) rbffi_AbstractMemory_Cast((obj), &rbffi_abstract_memory_data_type)
#define MEMORY_PTR(obj) MEMORY((obj))->address
#define MEMORY_LEN(obj) MEMORY((obj))->size
diff --git a/ext/ffi_c/Buffer.c b/ext/ffi_c/Buffer.c
index 75f4873..b3be778 100644
--- a/ext/ffi_c/Buffer.c
+++ b/ext/ffi_c/Buffer.c
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2008-2010 Wayne Meissner
* Copyright (C) 2009 Aman Gupta <aman@tmm1.net>
- *
+ *
* Copyright (c) 2008-2013, Ruby FFI project contributors
* All rights reserved.
*
@@ -39,7 +39,7 @@
#define BUFFER_EMBED_MAXLEN (8)
typedef struct Buffer {
AbstractMemory memory;
-
+
union {
VALUE rbParent; /* link to parent buffer */
char* storage; /* start of malloc area */
@@ -105,7 +105,7 @@ buffer_release(void *data)
xfree(ptr->data.storage);
ptr->data.storage = NULL;
}
-
+
xfree(ptr);
}
@@ -140,11 +140,11 @@ buffer_initialize(int argc, VALUE* argv, VALUE self)
/* ensure the memory is aligned on at least a 8 byte boundary */
p->memory.address = (void *) (((uintptr_t) p->data.storage + 0x7) & (uintptr_t) ~0x7ULL);
-
+
if (p->memory.size > 0 && (nargs < 3 || RTEST(rbClear))) {
memset(p->memory.address, 0, p->memory.size);
}
-
+
} else {
p->memory.flags |= MEM_EMBED;
p->memory.address = (void *) &p->data.embed[0];
@@ -169,7 +169,7 @@ buffer_initialize_copy(VALUE self, VALUE other)
Buffer* dst;
TypedData_Get_Struct(self, Buffer, &buffer_data_type, dst);
- src = rbffi_AbstractMemory_Cast(other, BufferClass);
+ src = rbffi_AbstractMemory_Cast(other, &buffer_data_type);
if ((dst->memory.flags & MEM_EMBED) == 0 && dst->data.storage != NULL) {
xfree(dst->data.storage);
}
@@ -178,11 +178,11 @@ buffer_initialize_copy(VALUE self, VALUE other)
rb_raise(rb_eNoMemError, "failed to allocate memory size=%lu bytes", src->size);
return Qnil;
}
-
+
dst->memory.address = (void *) (((uintptr_t) dst->data.storage + 0x7) & (uintptr_t) ~0x7ULL);
dst->memory.size = src->size;
dst->memory.typeSize = src->typeSize;
-
+
/* finally, copy the actual buffer contents */
memcpy(dst->memory.address, src->address, src->size);
@@ -259,7 +259,7 @@ buffer_inspect(VALUE self)
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);
-
+
return rb_str_new2(tmp);
}
@@ -396,7 +396,7 @@ rbffi_Buffer_Init(VALUE moduleFFI)
rb_define_alias(rb_singleton_class(BufferClass), "new_in", "alloc_in");
rb_define_alias(rb_singleton_class(BufferClass), "new_out", "alloc_out");
rb_define_alias(rb_singleton_class(BufferClass), "new_inout", "alloc_inout");
-
+
rb_define_method(BufferClass, "initialize", buffer_initialize, -1);
rb_define_method(BufferClass, "initialize_copy", buffer_initialize_copy, 1);
rb_define_method(BufferClass, "order", buffer_order, -1);
diff --git a/ext/ffi_c/MemoryPointer.c b/ext/ffi_c/MemoryPointer.c
index 0a747fb..8227183 100644
--- a/ext/ffi_c/MemoryPointer.c
+++ b/ext/ffi_c/MemoryPointer.c
@@ -46,7 +46,7 @@ static VALUE memptr_free(VALUE self);
VALUE rbffi_MemoryPointerClass;
-#define MEMPTR(obj) ((MemoryPointer *) rbffi_AbstractMemory_Cast(obj, rbffi_MemoryPointerClass))
+#define MEMPTR(obj) ((MemoryPointer *) rbffi_AbstractMemory_Cast(obj, &memory_pointer_data_type))
VALUE
rbffi_MemoryPointer_NewInstance(long size, long count, bool clear)
diff --git a/ext/ffi_c/Pointer.c b/ext/ffi_c/Pointer.c
index cfa841e..c926fe9 100644
--- a/ext/ffi_c/Pointer.c
+++ b/ext/ffi_c/Pointer.c
@@ -36,7 +36,7 @@
#include "AbstractMemory.h"
#include "Pointer.h"
-#define POINTER(obj) rbffi_AbstractMemory_Cast((obj), rbffi_PointerClass)
+#define POINTER(obj) rbffi_AbstractMemory_Cast((obj), &rbffi_pointer_data_type)
VALUE rbffi_PointerClass = Qnil;
VALUE rbffi_NullPointerSingleton = Qnil;
diff --git a/ext/ffi_c/Variadic.c b/ext/ffi_c/Variadic.c
index 014fac2..04efd24 100644
--- a/ext/ffi_c/Variadic.c
+++ b/ext/ffi_c/Variadic.c
@@ -130,7 +130,7 @@ variadic_initialize(VALUE self, VALUE rbFunction, VALUE rbParameterTypes, VALUE
TypedData_Get_Struct(self, VariadicInvoker, &variadic_data_type, invoker);
RB_OBJ_WRITE(self, &invoker->rbEnums, rb_hash_aref(options, ID2SYM(rb_intern("enums"))));
RB_OBJ_WRITE(self, &invoker->rbAddress, rbFunction);
- invoker->function = rbffi_AbstractMemory_Cast(rbFunction, rbffi_PointerClass)->address;
+ invoker->function = rbffi_AbstractMemory_Cast(rbFunction, &rbffi_pointer_data_type)->address;
invoker->blocking = RTEST(rb_hash_aref(options, ID2SYM(rb_intern("blocking"))));
#if defined(X86_WIN32)