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
4 changes: 2 additions & 2 deletions ext/ffi_c/AbstractMemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ memory_get(VALUE self, VALUE type_name, VALUE offset)
if(NIL_P(nType)) goto undefined_type;

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

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

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

MemoryOp *op = get_memory_op(type);
if(op == NULL) goto undefined_type;
Expand Down
34 changes: 24 additions & 10 deletions ext/ffi_c/ArrayType.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@

static VALUE array_type_s_allocate(VALUE klass);
static VALUE array_type_initialize(VALUE self, VALUE rbComponentType, VALUE rbLength);
static void array_type_mark(ArrayType *);
static void array_type_free(ArrayType *);
static void array_type_mark(void *);
static void array_type_free(void *);

const rb_data_type_t rbffi_array_type_data_type = { /* extern */
.wrap_struct_name = "FFI::ArrayType",
.function = {
.dmark = array_type_mark,
.dfree = array_type_free,
.dsize = NULL,
},
.parent = &rbffi_type_data_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY
};


VALUE rbffi_ArrayTypeClass = Qnil;

Expand All @@ -44,7 +56,7 @@ array_type_s_allocate(VALUE klass)
ArrayType* array;
VALUE obj;

obj = Data_Make_Struct(klass, ArrayType, array_type_mark, array_type_free, array);
obj = TypedData_Make_Struct(klass, ArrayType, &rbffi_array_type_data_type, array);

array->base.nativeType = NATIVE_ARRAY;
array->base.ffiType = xcalloc(1, sizeof(*array->base.ffiType));
Expand All @@ -57,14 +69,16 @@ array_type_s_allocate(VALUE klass)
}

static void
array_type_mark(ArrayType *array)
array_type_mark(void *data)
{
ArrayType *array = (ArrayType *)data;
rb_gc_mark(array->rbComponentType);
}

static void
array_type_free(ArrayType *array)
array_type_free(void *data)
{
ArrayType *array = (ArrayType *)data;
xfree(array->base.ffiType);
xfree(array->ffiTypes);
xfree(array);
Expand All @@ -84,12 +98,12 @@ array_type_initialize(VALUE self, VALUE rbComponentType, VALUE rbLength)
ArrayType* array;
int i;

Data_Get_Struct(self, ArrayType, array);
TypedData_Get_Struct(self, ArrayType, &rbffi_array_type_data_type, array);

array->length = NUM2UINT(rbLength);
array->rbComponentType = rbComponentType;
Data_Get_Struct(rbComponentType, Type, array->componentType);
TypedData_Get_Struct(rbComponentType, Type, &rbffi_type_data_type, array->componentType);

array->ffiTypes = xcalloc(array->length + 1, sizeof(*array->ffiTypes));
array->base.ffiType->elements = array->ffiTypes;
array->base.ffiType->size = array->componentType->ffiType->size * array->length;
Expand All @@ -112,7 +126,7 @@ array_type_length(VALUE self)
{
ArrayType* array;

Data_Get_Struct(self, ArrayType, array);
TypedData_Get_Struct(self, ArrayType, &rbffi_array_type_data_type, array);

return UINT2NUM(array->length);
}
Expand All @@ -127,7 +141,7 @@ array_type_element_type(VALUE self)
{
ArrayType* array;

Data_Get_Struct(self, ArrayType, array);
TypedData_Get_Struct(self, ArrayType, &rbffi_array_type_data_type, array);

return array->rbComponentType;
}
Expand Down
1 change: 1 addition & 0 deletions ext/ffi_c/ArrayType.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef struct ArrayType_ {
} ArrayType;

extern void rbffi_ArrayType_Init(VALUE moduleFFI);
extern const rb_data_type_t rbffi_array_type_data_type;
extern VALUE rbffi_ArrayTypeClass;


Expand Down
2 changes: 1 addition & 1 deletion ext/ffi_c/Function.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function_init(VALUE self, VALUE rbFunctionInfo, VALUE rbProc)

fn->rbFunctionInfo = rbFunctionInfo;

Data_Get_Struct(fn->rbFunctionInfo, FunctionType, fn->info);
TypedData_Get_Struct(fn->rbFunctionInfo, FunctionType, &rbffi_fntype_data_type, fn->info);

if (rb_obj_is_kind_of(rbProc, rbffi_PointerClass)) {
Pointer* orig;
Expand Down
1 change: 1 addition & 0 deletions ext/ffi_c/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct FunctionType_ {
bool hasStruct;
};

extern const rb_data_type_t rbffi_fntype_data_type;
extern VALUE rbffi_FunctionTypeClass, rbffi_FunctionClass;

void rbffi_Function_Init(VALUE moduleFFI);
Expand Down
33 changes: 23 additions & 10 deletions ext/ffi_c/FunctionInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,27 @@

static VALUE fntype_allocate(VALUE klass);
static VALUE fntype_initialize(int argc, VALUE* argv, VALUE self);
static void fntype_mark(FunctionType*);
static void fntype_free(FunctionType *);
static void fntype_mark(void *);
static void fntype_free(void *);

const rb_data_type_t rbffi_fntype_data_type = { /* extern */
.wrap_struct_name = "FFI::FunctionType",
.function = {
.dmark = fntype_mark,
.dfree = fntype_free,
.dsize = NULL,
},
.parent = &rbffi_type_data_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY
};

VALUE rbffi_FunctionTypeClass = Qnil;

static VALUE
fntype_allocate(VALUE klass)
{
FunctionType* fnInfo;
VALUE obj = Data_Make_Struct(klass, FunctionType, fntype_mark, fntype_free, fnInfo);
VALUE obj = TypedData_Make_Struct(klass, FunctionType, &rbffi_fntype_data_type, fnInfo);

fnInfo->type.ffiType = &ffi_type_pointer;
fnInfo->type.nativeType = NATIVE_FUNCTION;
Expand All @@ -74,8 +85,9 @@ fntype_allocate(VALUE klass)
}

static void
fntype_mark(FunctionType* fnInfo)
fntype_mark(void *data)
{
FunctionType *fnInfo = (FunctionType *)data;
rb_gc_mark(fnInfo->rbReturnType);
rb_gc_mark(fnInfo->rbParameterTypes);
rb_gc_mark(fnInfo->rbEnums);
Expand All @@ -85,8 +97,9 @@ fntype_mark(FunctionType* fnInfo)
}

static void
fntype_free(FunctionType* fnInfo)
fntype_free(void *data)
{
FunctionType *fnInfo = (FunctionType *)data;
xfree(fnInfo->parameterTypes);
xfree(fnInfo->ffiParameterTypes);
xfree(fnInfo->nativeParameterTypes);
Expand Down Expand Up @@ -129,7 +142,7 @@ fntype_initialize(int argc, VALUE* argv, VALUE self)

Check_Type(rbParamTypes, T_ARRAY);

Data_Get_Struct(self, FunctionType, fnInfo);
TypedData_Get_Struct(self, FunctionType, &rbffi_fntype_data_type, fnInfo);
fnInfo->parameterCount = (int) RARRAY_LEN(rbParamTypes);
fnInfo->parameterTypes = xcalloc(fnInfo->parameterCount, sizeof(*fnInfo->parameterTypes));
fnInfo->ffiParameterTypes = xcalloc(fnInfo->parameterCount, sizeof(ffi_type *));
Expand Down Expand Up @@ -158,7 +171,7 @@ fntype_initialize(int argc, VALUE* argv, VALUE self)
}

rb_ary_push(fnInfo->rbParameterTypes, type);
Data_Get_Struct(type, Type, fnInfo->parameterTypes[i]);
TypedData_Get_Struct(type, Type, &rbffi_type_data_type, fnInfo->parameterTypes[i]);
fnInfo->ffiParameterTypes[i] = fnInfo->parameterTypes[i]->ffiType;
fnInfo->nativeParameterTypes[i] = fnInfo->parameterTypes[i]->nativeType;
}
Expand All @@ -173,7 +186,7 @@ fntype_initialize(int argc, VALUE* argv, VALUE self)
fnInfo->hasStruct = true;
}

Data_Get_Struct(fnInfo->rbReturnType, Type, fnInfo->returnType);
TypedData_Get_Struct(fnInfo->rbReturnType, Type, &rbffi_type_data_type, fnInfo->returnType);
fnInfo->ffiReturnType = fnInfo->returnType->ffiType;

#if defined(X86_WIN32)
Expand Down Expand Up @@ -212,7 +225,7 @@ fntype_result_type(VALUE self)
{
FunctionType* ft;

Data_Get_Struct(self, FunctionType, ft);
TypedData_Get_Struct(self, FunctionType, &rbffi_fntype_data_type, ft);

return ft->rbReturnType;
}
Expand All @@ -227,7 +240,7 @@ fntype_param_types(VALUE self)
{
FunctionType* ft;

Data_Get_Struct(self, FunctionType, ft);
TypedData_Get_Struct(self, FunctionType, &rbffi_fntype_data_type, ft);

return rb_ary_dup(ft->rbParameterTypes);
}
Expand Down
37 changes: 24 additions & 13 deletions ext/ffi_c/MappedType.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,29 @@

static VALUE mapped_allocate(VALUE);
static VALUE mapped_initialize(VALUE, VALUE);
static void mapped_mark(MappedType *);
static void mapped_mark(void *);
static ID id_native_type, id_to_native, id_from_native;

VALUE rbffi_MappedTypeClass = Qnil;

static const rb_data_type_t mapped_type_data_type = {
.wrap_struct_name = "FFI::Type::Mapped",
.function = {
.dmark = mapped_mark,
.dfree = RUBY_TYPED_DEFAULT_FREE,
.dsize = NULL,
},
.parent = &rbffi_type_data_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY
};


static VALUE
mapped_allocate(VALUE klass)
{
MappedType* m;

VALUE obj = Data_Make_Struct(klass, MappedType, mapped_mark, -1, m);
VALUE obj = TypedData_Make_Struct(klass, MappedType, &mapped_type_data_type, m);

m->rbConverter = Qnil;
m->rbType = Qnil;
Expand Down Expand Up @@ -81,23 +93,24 @@ mapped_initialize(VALUE self, VALUE rbConverter)
if (!rb_respond_to(rbConverter, id_from_native)) {
rb_raise(rb_eNoMethodError, "from_native method not implemented");
}
Data_Get_Struct(self, MappedType, m);

TypedData_Get_Struct(self, MappedType, &mapped_type_data_type, m);
m->rbType = rb_funcall2(rbConverter, id_native_type, 0, NULL);
if (!(rb_obj_is_kind_of(m->rbType, rbffi_TypeClass))) {
rb_raise(rb_eTypeError, "native_type did not return instance of FFI::Type");
}

m->rbConverter = rbConverter;
Data_Get_Struct(m->rbType, Type, m->type);
TypedData_Get_Struct(m->rbType, Type, &rbffi_type_data_type, m->type);
m->base.ffiType = m->type->ffiType;

return self;
}

static void
mapped_mark(MappedType* m)
mapped_mark(void* data)
{
MappedType* m = (MappedType*)data;
rb_gc_mark(m->rbType);
rb_gc_mark(m->rbConverter);
}
Expand All @@ -111,7 +124,7 @@ static VALUE
mapped_native_type(VALUE self)
{
MappedType*m = NULL;
Data_Get_Struct(self, MappedType, m);
TypedData_Get_Struct(self, MappedType, &mapped_type_data_type, m);

return m->rbType;
}
Expand All @@ -124,9 +137,8 @@ static VALUE
mapped_to_native(int argc, VALUE* argv, VALUE self)
{
MappedType*m = NULL;

Data_Get_Struct(self, MappedType, m);

TypedData_Get_Struct(self, MappedType, &mapped_type_data_type, m);

return rb_funcall2(m->rbConverter, id_to_native, argc, argv);
}

Expand All @@ -138,8 +150,7 @@ static VALUE
mapped_from_native(int argc, VALUE* argv, VALUE self)
{
MappedType*m = NULL;

Data_Get_Struct(self, MappedType, m);
TypedData_Get_Struct(self, MappedType, &mapped_type_data_type, m);

return rb_funcall2(m->rbConverter, id_from_native, argc, argv);
}
Expand Down
1 change: 0 additions & 1 deletion ext/ffi_c/MappedType.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ void rbffi_MappedType_Init(VALUE moduleFFI);

extern VALUE rbffi_MappedTypeClass;


#ifdef __cplusplus
}
#endif
Expand Down
16 changes: 8 additions & 8 deletions ext/ffi_c/Struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ struct_initialize(int argc, VALUE* argv, VALUE self)
rb_raise(rb_eRuntimeError, "Invalid Struct layout");
}

Data_Get_Struct(s->rbLayout, StructLayout, s->layout);
TypedData_Get_Struct(s->rbLayout, StructLayout, &rbffi_struct_layout_data_type, s->layout);

if (rbPointer != Qnil) {
s->pointer = MEMORY(rbPointer);
Expand Down Expand Up @@ -203,7 +203,7 @@ struct_layout(VALUE self)

if (s->layout == NULL) {
s->rbLayout = struct_class_layout(CLASS_OF(self));
Data_Get_Struct(s->rbLayout, StructLayout, s->layout);
TypedData_Get_Struct(s->rbLayout, StructLayout, &rbffi_struct_layout_data_type, s->layout);
}

return s->layout;
Expand Down Expand Up @@ -291,7 +291,7 @@ struct_field(Struct* s, VALUE fieldName)
}
/* Write the retrieved coder to the cache */
p_ce->fieldName = fieldName;
p_ce->field = (StructField *) DATA_PTR(rbField);
TypedData_Get_Struct(rbField, StructField, &rbffi_struct_field_data_type, p_ce->field);
}

return p_ce->field;
Expand Down Expand Up @@ -432,7 +432,7 @@ struct_set_layout(VALUE self, VALUE layout)
return Qnil;
}

Data_Get_Struct(layout, StructLayout, s->layout);
TypedData_Get_Struct(layout, StructLayout, &rbffi_struct_layout_data_type, s->layout);
rb_ivar_set(self, id_layout_ivar, layout);

return self;
Expand Down Expand Up @@ -526,9 +526,9 @@ inline_array_initialize(VALUE self, VALUE rbMemory, VALUE rbField)
array->rbField = rbField;

Data_Get_Struct(rbMemory, AbstractMemory, array->memory);
Data_Get_Struct(rbField, StructField, array->field);
Data_Get_Struct(array->field->rbType, ArrayType, array->arrayType);
Data_Get_Struct(array->arrayType->rbComponentType, Type, array->componentType);
TypedData_Get_Struct(rbField, StructField, &rbffi_struct_field_data_type, array->field);
TypedData_Get_Struct(array->field->rbType, ArrayType, &rbffi_array_type_data_type, array->arrayType);
TypedData_Get_Struct(array->arrayType->rbComponentType, Type, &rbffi_type_data_type, array->componentType);

array->op = get_memory_op(array->componentType);
if (array->op == NULL && array->componentType->nativeType == NATIVE_MAPPED) {
Expand Down Expand Up @@ -641,7 +641,7 @@ inline_array_aset(VALUE self, VALUE rbIndex, VALUE rbValue)

} else {
ArrayType* arrayType;
Data_Get_Struct(array->field->rbType, ArrayType, arrayType);
TypedData_Get_Struct(array->field->rbType, ArrayType, &rbffi_array_type_data_type, arrayType);

rb_raise(rb_eArgError, "set not supported for %s", rb_obj_classname(arrayType->rbComponentType));
return Qnil;
Expand Down
3 changes: 3 additions & 0 deletions ext/ffi_c/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ extern "C" {

extern void rbffi_Struct_Init(VALUE ffiModule);
extern void rbffi_StructLayout_Init(VALUE ffiModule);
extern const rb_data_type_t rbffi_struct_layout_data_type;
extern const rb_data_type_t rbffi_struct_field_data_type;

typedef struct StructField_ StructField;
typedef struct StructLayout_ StructLayout;
typedef struct Struct_ Struct;
Expand Down
Loading