Skip to content

Commit

Permalink
Merge pull request #1053 from larskanis/fix-1052
Browse files Browse the repository at this point in the history
Avoid loop initial declarations for compat with Centos-7
  • Loading branch information
larskanis committed Sep 26, 2023
2 parents 4a2df1a + 8be6833 commit 683e18b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
6 changes: 4 additions & 2 deletions ext/ffi_c/AbstractMemory.c
Expand Up @@ -350,14 +350,15 @@ memory_get(VALUE self, VALUE type_name, VALUE offset)
AbstractMemory* ptr;
VALUE nType;
Type *type;
MemoryOp *op;

nType = rbffi_Type_Lookup(type_name);
if(NIL_P(nType)) goto undefined_type;

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

MemoryOp *op = get_memory_op(type);
op = get_memory_op(type);
if(op == NULL) goto undefined_type;

return op->get(ptr, NUM2LONG(offset));
Expand All @@ -382,14 +383,15 @@ memory_put(VALUE self, VALUE type_name, VALUE offset, VALUE value)
AbstractMemory* ptr;
VALUE nType;
Type *type;
MemoryOp *op;

nType = rbffi_Type_Lookup(type_name);
if(NIL_P(nType)) goto undefined_type;

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

MemoryOp *op = get_memory_op(type);
op = get_memory_op(type);
if(op == NULL) goto undefined_type;

op->put(ptr, NUM2LONG(offset), value);
Expand Down
9 changes: 6 additions & 3 deletions ext/ffi_c/Call.c
Expand Up @@ -427,8 +427,10 @@ getPointer(VALUE value, int type)
} else if (type == T_DATA && rb_obj_is_kind_of(value, rbffi_StructClass)) {

Struct* s;
AbstractMemory* memory;

TypedData_Get_Struct(value, Struct, &rbffi_struct_data_type, s);
AbstractMemory* memory = s->pointer;
memory = s->pointer;
return memory != NULL ? memory->address : NULL;

} else if (type == T_STRING) {
Expand Down Expand Up @@ -464,7 +466,9 @@ rbffi_GetInvoker(FunctionType *fnInfo)
static void*
callback_param(VALUE proc, VALUE cbInfo)
{
VALUE callback ;
VALUE callback;
AbstractMemory *mem;

if (unlikely(proc == Qnil)) {
return NULL ;
}
Expand All @@ -479,7 +483,6 @@ callback_param(VALUE proc, VALUE cbInfo)
callback = rbffi_Function_ForProc(cbInfo, proc);
RB_GC_GUARD(callback);

AbstractMemory *mem;
TypedData_Get_Struct(callback, AbstractMemory, &rbffi_abstract_memory_data_type, mem);
return mem->address;
}
Expand Down
6 changes: 4 additions & 2 deletions ext/ffi_c/Function.c
Expand Up @@ -953,10 +953,10 @@ invoke_callback(VALUE data)

} else if (rb_obj_is_kind_of(rbReturnValue, rb_cProc) || rb_respond_to(rbReturnValue, id_call)) {
VALUE function;
AbstractMemory* memory;

function = rbffi_Function_ForProc(rbReturnType, rbReturnValue);

AbstractMemory* memory;
TypedData_Get_Struct(function, AbstractMemory, &rbffi_abstract_memory_data_type, memory);

*((void **) retval) = memory->address;
Expand All @@ -968,8 +968,10 @@ invoke_callback(VALUE data)
case NATIVE_STRUCT:
if (TYPE(rbReturnValue) == T_DATA && rb_obj_is_kind_of(rbReturnValue, rbffi_StructClass)) {
Struct* s;
AbstractMemory* memory;

TypedData_Get_Struct(rbReturnValue, Struct, &rbffi_struct_data_type, s);
AbstractMemory* memory = s->pointer;
memory = s->pointer;

if (memory->address != NULL) {
memcpy(retval, memory->address, returnType->ffiType->size);
Expand Down
6 changes: 4 additions & 2 deletions ext/ffi_c/FunctionInfo.c
Expand Up @@ -97,7 +97,8 @@ fntype_mark(void *data)
rb_gc_mark_movable(fnInfo->rbParameterTypes);
rb_gc_mark_movable(fnInfo->rbEnums);
if (fnInfo->callbackCount > 0 && fnInfo->callbackParameters != NULL) {
for (size_t index = 0; index < fnInfo->callbackCount; index++) {
size_t index;
for (index = 0; index < fnInfo->callbackCount; index++) {
rb_gc_mark_movable(fnInfo->callbackParameters[index]);
}
}
Expand All @@ -111,7 +112,8 @@ fntype_compact(void *data)
ffi_gc_location(fnInfo->rbParameterTypes);
ffi_gc_location(fnInfo->rbEnums);
if (fnInfo->callbackCount > 0 && fnInfo->callbackParameters != NULL) {
for (size_t index = 0; index < fnInfo->callbackCount; index++) {
size_t index;
for (index = 0; index < fnInfo->callbackCount; index++) {
ffi_gc_location(fnInfo->callbackParameters[index]);
}
}
Expand Down
10 changes: 7 additions & 3 deletions ext/ffi_c/Struct.c
Expand Up @@ -189,9 +189,11 @@ struct_initialize_copy(VALUE self, VALUE other)
}

if (src->layout->referenceFieldCount > 0) {
size_t index;

dst->rbReferences = ALLOC_N(VALUE, dst->layout->referenceFieldCount);
memcpy(dst->rbReferences, src->rbReferences, dst->layout->referenceFieldCount * sizeof(VALUE));
for (size_t index = 0; index < dst->layout->referenceFieldCount; index++) {
for ( index = 0; index < dst->layout->referenceFieldCount; index++) {
RB_OBJ_WRITTEN(self, Qundef, &dst->rbReferences[index]);
}
}
Expand Down Expand Up @@ -268,7 +270,8 @@ struct_mark(void *data)
rb_gc_mark_movable(s->rbPointer);
rb_gc_mark_movable(s->rbLayout);
if (s->rbReferences != NULL) {
for (size_t index = 0; index < s->layout->referenceFieldCount; index++) {
size_t index;
for (index = 0; index < s->layout->referenceFieldCount; index++) {
rb_gc_mark_movable(s->rbReferences[index]);
}
}
Expand All @@ -281,7 +284,8 @@ struct_compact(void *data)
ffi_gc_location(s->rbPointer);
ffi_gc_location(s->rbLayout);
if (s->rbReferences != NULL) {
for (size_t index = 0; index < s->layout->referenceFieldCount; index++) {
size_t index;
for (index = 0; index < s->layout->referenceFieldCount; index++) {
ffi_gc_location(s->rbReferences[index]);
}
}
Expand Down

0 comments on commit 683e18b

Please sign in to comment.