summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWayne Meissner <wmeissner@gmail.com>2009-11-27 15:34:13 +1000
committerWayne Meissner <wmeissner@gmail.com>2009-11-27 15:34:13 +1000
commitcc5e2de3977f00b635fb5f0982a613d56193f47f (patch)
treecb9a027d3dedc8951bd1c01bc114babdfb62a658
parent9d68c7f87ad7c869ee1ce3139269525af70e5df4 (diff)
downloadffi-cc5e2de3977f00b635fb5f0982a613d56193f47f.tar.gz
Remove the MemoryOps pointer from every AbstractMemory object
-rw-r--r--ext/ffi_c/AbstractMemory.c1
-rw-r--r--ext/ffi_c/AbstractMemory.h42
-rw-r--r--ext/ffi_c/AutoPointer.c1
-rw-r--r--ext/ffi_c/Buffer.c2
-rw-r--r--ext/ffi_c/DynamicLibrary.c1
-rw-r--r--ext/ffi_c/Function.c1
-rw-r--r--ext/ffi_c/MemoryPointer.c1
-rw-r--r--ext/ffi_c/Pointer.c3
-rw-r--r--ext/ffi_c/Struct.c23
-rw-r--r--ext/ffi_c/Struct.h2
-rw-r--r--ext/ffi_c/StructLayout.c42
11 files changed, 41 insertions, 78 deletions
diff --git a/ext/ffi_c/AbstractMemory.c b/ext/ffi_c/AbstractMemory.c
index 202c1e8..088cba6 100644
--- a/ext/ffi_c/AbstractMemory.c
+++ b/ext/ffi_c/AbstractMemory.c
@@ -49,7 +49,6 @@ memory_allocate(VALUE klass)
AbstractMemory* memory;
VALUE obj;
obj = Data_Make_Struct(klass, AbstractMemory, NULL, -1, memory);
- memory->ops = &rbffi_AbstractMemoryOps;
memory->access = MEM_RD | MEM_WR;
return obj;
diff --git a/ext/ffi_c/AbstractMemory.h b/ext/ffi_c/AbstractMemory.h
index 3909ef1..65cf4ee 100644
--- a/ext/ffi_c/AbstractMemory.h
+++ b/ext/ffi_c/AbstractMemory.h
@@ -74,9 +74,12 @@ struct AbstractMemory_ {
long size;
int access;
int typeSize;
- MemoryOps* ops;
};
+
+extern VALUE rbffi_AbstractMemoryClass;
+extern MemoryOps rbffi_AbstractMemoryOps;
+
extern void rbffi_AbstractMemory_Init(VALUE ffiModule);
extern AbstractMemory* rbffi_AbstractMemory_Cast(VALUE obj, VALUE klass);
@@ -109,40 +112,37 @@ checkWrite(AbstractMemory* mem)
}
static inline MemoryOp*
-memory_get_op(AbstractMemory* ptr, Type* type)
+get_memory_op(Type* type)
{
- if (ptr == NULL || ptr->ops == NULL || type == NULL) {
- return NULL;
- }
switch (type->nativeType) {
case NATIVE_INT8:
- return ptr->ops->int8;
+ return rbffi_AbstractMemoryOps.int8;
case NATIVE_UINT8:
- return ptr->ops->uint8;
+ return rbffi_AbstractMemoryOps.uint8;
case NATIVE_INT16:
- return ptr->ops->int16;
+ return rbffi_AbstractMemoryOps.int16;
case NATIVE_UINT16:
- return ptr->ops->uint16;
+ return rbffi_AbstractMemoryOps.uint16;
case NATIVE_INT32:
- return ptr->ops->int32;
+ return rbffi_AbstractMemoryOps.int32;
case NATIVE_UINT32:
- return ptr->ops->uint32;
+ return rbffi_AbstractMemoryOps.uint32;
case NATIVE_INT64:
- return ptr->ops->int64;
+ return rbffi_AbstractMemoryOps.int64;
case NATIVE_UINT64:
- return ptr->ops->uint64;
+ return rbffi_AbstractMemoryOps.uint64;
case NATIVE_LONG:
- return ptr->ops->slong;
+ return rbffi_AbstractMemoryOps.slong;
case NATIVE_ULONG:
- return ptr->ops->ulong;
+ return rbffi_AbstractMemoryOps.ulong;
case NATIVE_FLOAT32:
- return ptr->ops->float32;
+ return rbffi_AbstractMemoryOps.float32;
case NATIVE_FLOAT64:
- return ptr->ops->float64;
+ return rbffi_AbstractMemoryOps.float64;
case NATIVE_POINTER:
- return ptr->ops->pointer;
+ return rbffi_AbstractMemoryOps.pointer;
case NATIVE_STRING:
- return ptr->ops->strptr;
+ return rbffi_AbstractMemoryOps.strptr;
default:
return NULL;
}
@@ -154,10 +154,6 @@ memory_get_op(AbstractMemory* ptr, Type* type)
-extern VALUE rbffi_AbstractMemoryClass;
-extern MemoryOps rbffi_AbstractMemoryOps;
-
-
#ifdef __cplusplus
}
#endif
diff --git a/ext/ffi_c/AutoPointer.c b/ext/ffi_c/AutoPointer.c
index 128f7a4..8f7b60d 100644
--- a/ext/ffi_c/AutoPointer.c
+++ b/ext/ffi_c/AutoPointer.c
@@ -25,7 +25,6 @@ autoptr_allocate(VALUE klass)
AutoPointer* p;
VALUE obj = Data_Make_Struct(klass, AutoPointer, autoptr_mark, -1, p);
p->parent = Qnil;
- p->memory.ops = &rbffi_AbstractMemoryOps;
p->memory.access = MEM_RD | MEM_WR;
return obj;
diff --git a/ext/ffi_c/Buffer.c b/ext/ffi_c/Buffer.c
index bd98320..5755b0a 100644
--- a/ext/ffi_c/Buffer.c
+++ b/ext/ffi_c/Buffer.c
@@ -54,7 +54,6 @@ buffer_allocate(VALUE klass)
obj = Data_Make_Struct(klass, Buffer, NULL, buffer_release, buffer);
buffer->rbParent = Qnil;
- buffer->memory.ops = &rbffi_AbstractMemoryOps;
buffer->memory.access = MEM_RD | MEM_WR;
return obj;
@@ -124,7 +123,6 @@ buffer_plus(VALUE self, VALUE rbOffset)
obj = Data_Make_Struct(BufferClass, Buffer, buffer_mark, -1, result);
result->memory.address = ptr->memory.address + offset;
result->memory.size = ptr->memory.size - offset;
- result->memory.ops = ptr->memory.ops;
result->memory.access = ptr->memory.access;
result->memory.typeSize = ptr->memory.typeSize;
result->rbParent = self;
diff --git a/ext/ffi_c/DynamicLibrary.c b/ext/ffi_c/DynamicLibrary.c
index 7f1d342..3c9fbb3 100644
--- a/ext/ffi_c/DynamicLibrary.c
+++ b/ext/ffi_c/DynamicLibrary.c
@@ -157,7 +157,6 @@ symbol_new(VALUE library, void* address, VALUE name)
sym->memory.size = LONG_MAX;
sym->memory.typeSize = 1;
sym->memory.access = MEM_RD | MEM_WR;
- sym->memory.ops = &rbffi_AbstractMemoryOps;
sym->library = library;
sym->name = name;
diff --git a/ext/ffi_c/Function.c b/ext/ffi_c/Function.c
index 76fe1f4..08d563f 100644
--- a/ext/ffi_c/Function.c
+++ b/ext/ffi_c/Function.c
@@ -81,7 +81,6 @@ function_allocate(VALUE klass)
obj = Data_Make_Struct(klass, Function, function_mark, function_free, fn);
fn->memory.access = MEM_RD;
- fn->memory.ops = &rbffi_AbstractMemoryOps;
fn->rbProc = Qnil;
fn->rbFunctionInfo = Qnil;
diff --git a/ext/ffi_c/MemoryPointer.c b/ext/ffi_c/MemoryPointer.c
index 46d69d8..9d0e632 100644
--- a/ext/ffi_c/MemoryPointer.c
+++ b/ext/ffi_c/MemoryPointer.c
@@ -63,7 +63,6 @@ memptr_allocate(VALUE klass)
{
MemoryPointer* p;
VALUE obj = Data_Make_Struct(klass, MemoryPointer, NULL, memptr_release, p);
- p->memory.ops = &rbffi_AbstractMemoryOps;
p->memory.access = MEM_RD | MEM_WR;
return obj;
diff --git a/ext/ffi_c/Pointer.c b/ext/ffi_c/Pointer.c
index 76b67d7..0b51516 100644
--- a/ext/ffi_c/Pointer.c
+++ b/ext/ffi_c/Pointer.c
@@ -59,7 +59,6 @@ rbffi_Pointer_NewInstance(void* addr)
obj = Data_Make_Struct(rbffi_PointerClass, Pointer, NULL, -1, p);
p->memory.address = addr;
p->memory.size = LONG_MAX;
- p->memory.ops = &rbffi_AbstractMemoryOps;
p->memory.access = (addr == NULL) ? 0 : (MEM_RD | MEM_WR);
p->memory.typeSize = 1;
p->parent = Qnil;
@@ -75,7 +74,6 @@ ptr_allocate(VALUE klass)
obj = Data_Make_Struct(klass, Pointer, NULL, -1, p);
p->parent = Qnil;
- p->memory.ops = &rbffi_AbstractMemoryOps;
p->memory.access = MEM_RD | MEM_WR;
return obj;
@@ -146,7 +144,6 @@ ptr_plus(VALUE self, VALUE offset)
p->memory.address = ptr->address + off;
p->memory.size = ptr->size == LONG_MAX ? LONG_MAX : ptr->size - off;
- p->memory.ops = &rbffi_AbstractMemoryOps;
p->memory.access = ptr->access;
p->memory.typeSize = ptr->typeSize;
p->parent = self;
diff --git a/ext/ffi_c/Struct.c b/ext/ffi_c/Struct.c
index a75632f..6a6d066 100644
--- a/ext/ffi_c/Struct.c
+++ b/ext/ffi_c/Struct.c
@@ -132,11 +132,6 @@ struct_initialize(int argc, VALUE* argv, VALUE self)
s->pointer = (AbstractMemory *) DATA_PTR(s->rbPointer);
}
- if (s->pointer->ops == NULL) {
- VALUE name = rb_class_name(CLASS_OF(s->rbPointer));
- rb_raise(rb_eRuntimeError, "No memory ops set for %s", StringValueCStr(name));
- }
-
return self;
}
@@ -171,8 +166,7 @@ struct_aref(VALUE self, VALUE fieldName)
Struct* s;
VALUE rbField;
StructField* f;
- MemoryOp* op;
-
+
Data_Get_Struct(self, Struct, s);
rbField = struct_field(s, fieldName);
f = (StructField *) DATA_PTR(rbField);
@@ -180,8 +174,8 @@ struct_aref(VALUE self, VALUE fieldName)
if (f->get != NULL) {
return (*f->get)(f, s);
- } else if ((op = memory_get_op(s->pointer, f->type)) != NULL) {
- return (*op->get)(s->pointer, f->offset);
+ } else if (f->memoryOp != NULL) {
+ return (*f->memoryOp->get)(s->pointer, f->offset);
} else {
@@ -196,8 +190,7 @@ struct_aset(VALUE self, VALUE fieldName, VALUE value)
Struct* s;
VALUE rbField;
StructField* f;
- MemoryOp* op;
- VALUE argv[2];
+
Data_Get_Struct(self, Struct, s);
rbField = struct_field(s, fieldName);
@@ -205,13 +198,13 @@ struct_aset(VALUE self, VALUE fieldName, VALUE value)
if (f->put != NULL) {
(*f->put)(f, s, value);
- } else if ((op = memory_get_op(s->pointer, f->type)) != NULL) {
+ } else if (f->memoryOp != NULL) {
- (*op->put)(s->pointer, f->offset, value);
+ (*f->memoryOp->put)(s->pointer, f->offset, value);
} else {
-
/* call up to the ruby code to set the value */
+ VALUE argv[2];
argv[0] = s->rbPointer;
argv[1] = value;
rb_funcall2(rbField, id_put, 2, argv);
@@ -574,7 +567,7 @@ inline_array_initialize(VALUE self, VALUE rbMemory, VALUE rbField)
Data_Get_Struct(array->field->rbType, ArrayType, arrayType);
Data_Get_Struct(arrayType->rbComponentType, Type, array->componentType);
- array->op = memory_get_op(array->memory, array->componentType);
+ array->op = get_memory_op(array->componentType);
return self;
}
diff --git a/ext/ffi_c/Struct.h b/ext/ffi_c/Struct.h
index ba79818..862345c 100644
--- a/ext/ffi_c/Struct.h
+++ b/ext/ffi_c/Struct.h
@@ -52,6 +52,8 @@ extern "C" {
VALUE (*get)(StructField* field, Struct* s);
void (*put)(StructField* field, Struct* s, VALUE value);
+
+ MemoryOp* memoryOp;
};
struct StructLayout_ {
diff --git a/ext/ffi_c/StructLayout.c b/ext/ffi_c/StructLayout.c
index a9aa796..5af9bd9 100644
--- a/ext/ffi_c/StructLayout.c
+++ b/ext/ffi_c/StructLayout.c
@@ -107,6 +107,7 @@ struct_field_initialize(int argc, VALUE* argv, VALUE self)
field->rbName = (TYPE(rbName) == T_SYMBOL) ? rbName : rb_str_intern(rbName);
field->rbType = rbType;
Data_Get_Struct(field->rbType, Type, field->type);
+ field->memoryOp = get_memory_op(field->type);
return self;
}
@@ -155,34 +156,28 @@ static VALUE
struct_field_get(VALUE self, VALUE pointer)
{
StructField* f;
- MemoryOp* op;
- AbstractMemory* memory = MEMORY(pointer);
Data_Get_Struct(self, StructField, f);
- op = memory_get_op(memory, f->type);
- if (op == NULL) {
- rb_raise(rb_eArgError, "get not supported for %s", rb_obj_classname(self));
+ if (f->memoryOp == NULL) {
+ rb_raise(rb_eArgError, "get not supported for %s", rb_obj_classname(f->rbType));
return Qnil;
}
- return (*op->get)(memory, f->offset);
+ return (*f->memoryOp->get)(MEMORY(pointer), f->offset);
}
static VALUE
struct_field_put(VALUE self, VALUE pointer, VALUE value)
{
StructField* f;
- MemoryOp* op;
- AbstractMemory* memory = MEMORY(pointer);
-
+
Data_Get_Struct(self, StructField, f);
- op = memory_get_op(memory, f->type);
- if (op == NULL) {
- rb_raise(rb_eArgError, "put not supported for %s", rb_obj_classname(self));
+ if (f->memoryOp == NULL) {
+ rb_raise(rb_eArgError, "put not supported for %s", rb_obj_classname(f->rbType));
return self;
}
- (*op->put)(memory, f->offset, value);
+ (*f->memoryOp->put)(MEMORY(pointer), f->offset, value);
return self;
}
@@ -191,33 +186,19 @@ static VALUE
function_field_get(VALUE self, VALUE pointer)
{
StructField* f;
- MemoryOp* op;
- AbstractMemory* memory = MEMORY(pointer);
-
+
Data_Get_Struct(self, StructField, f);
- op = memory->ops->pointer;
- if (op == NULL) {
- rb_raise(rb_eArgError, "get not supported for %s", rb_obj_classname(self));
- return Qnil;
- }
- return rbffi_Function_NewInstance(f->rbType, (*op->get)(memory, f->offset));
+ return rbffi_Function_NewInstance(f->rbType, (*rbffi_AbstractMemoryOps.pointer->get)(MEMORY(pointer), f->offset));
}
static VALUE
function_field_put(VALUE self, VALUE pointer, VALUE proc)
{
StructField* f;
- MemoryOp* op;
- AbstractMemory* memory = MEMORY(pointer);
VALUE value = Qnil;
Data_Get_Struct(self, StructField, f);
- op = memory->ops->pointer;
- if (op == NULL) {
- rb_raise(rb_eArgError, "put not supported for %s", rb_obj_classname(self));
- return self;
- }
if (NIL_P(proc) || rb_obj_is_kind_of(proc, rbffi_FunctionClass)) {
value = proc;
@@ -227,7 +208,7 @@ function_field_put(VALUE self, VALUE pointer, VALUE proc)
rb_raise(rb_eTypeError, "wrong type (expected Proc or Function)");
}
- (*op->put)(memory, f->offset, value);
+ (*rbffi_AbstractMemoryOps.pointer->put)(MEMORY(pointer), f->offset, value);
return self;
}
@@ -452,6 +433,7 @@ struct_layout_free(StructLayout *layout)
xfree(layout);
}
+
void
rbffi_StructLayout_Init(VALUE moduleFFI)
{