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
15 changes: 5 additions & 10 deletions ext/ffi_c/AbstractMemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -693,22 +693,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
Expand Down
4 changes: 2 additions & 2 deletions ext/ffi_c/AbstractMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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

Expand Down
20 changes: 10 additions & 10 deletions ext/ffi_c/Buffer.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2008-2010 Wayne Meissner
* Copyright (C) 2009 Aman Gupta <[email protected]>
*
*
* Copyright (c) 2008-2013, Ruby FFI project contributors
* All rights reserved.
*
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -105,7 +105,7 @@ buffer_release(void *data)
xfree(ptr->data.storage);
ptr->data.storage = NULL;
}

xfree(ptr);
}

Expand Down Expand Up @@ -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];
Expand All @@ -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);
}
Expand All @@ -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);

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi_c/MemoryPointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi_c/Pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ext/ffi_c/Variadic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down