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
14 changes: 12 additions & 2 deletions ext/ffi_c/AbstractMemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
# define RB_OBJ_STRING(obj) StringValueCStr(obj)
#endif

static size_t memsize(const void *data);
static inline char* memory_address(VALUE self);
VALUE rbffi_AbstractMemoryClass = Qnil;
static VALUE NullPointerErrorClass = Qnil;
Expand All @@ -65,9 +66,11 @@ const rb_data_type_t rbffi_abstract_memory_data_type = { /* extern */
.function = {
.dmark = NULL,
.dfree = RUBY_TYPED_DEFAULT_FREE,
.dsize = NULL,
.dsize = memsize,
},
.flags = RUBY_TYPED_FREE_IMMEDIATELY
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
// macro to update VALUE references, as to trigger write barriers.
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};

static VALUE
Expand All @@ -80,6 +83,13 @@ memory_allocate(VALUE klass)

return obj;
}

static size_t
memsize(const void *data)
{
return sizeof(AbstractMemory);
}

#define VAL(x, swap) (unlikely(((memory->flags & MEM_SWAP) != 0)) ? swap((x)) : (x))

#define NUM_OP(name, type, toNative, fromNative, swap) \
Expand Down
35 changes: 29 additions & 6 deletions ext/ffi_c/Buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,33 @@ static VALUE buffer_initialize(int argc, VALUE* argv, VALUE self);
static void buffer_release(void *data);
static void buffer_mark(void *data);
static VALUE buffer_free(VALUE self);
static size_t allocated_buffer_memsize(const void *data);
static size_t buffer_memsize(const void *data);

static const rb_data_type_t buffer_data_type = {
.wrap_struct_name = "FFI::Buffer",
.function = {
.dmark = buffer_mark,
.dfree = RUBY_TYPED_DEFAULT_FREE,
.dsize = NULL,
.dsize = buffer_memsize,
},
.parent = &rbffi_abstract_memory_data_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
// macro to update VALUE references, as to trigger write barriers.
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};

static const rb_data_type_t allocated_buffer_data_type = {
.wrap_struct_name = "FFI::Buffer(allocated)",
.function = {
.dmark = NULL,
.dfree = buffer_release,
.dsize = NULL,
.dsize = allocated_buffer_memsize,
},
.parent = &buffer_data_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
// macro to update VALUE references, as to trigger write barriers.
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};


Expand All @@ -85,7 +91,7 @@ buffer_allocate(VALUE klass)
VALUE obj;

obj = TypedData_Make_Struct(klass, Buffer, &allocated_buffer_data_type, buffer);
buffer->data.rbParent = Qnil;
RB_OBJ_WRITE(obj, &buffer->data.rbParent, Qnil);
buffer->memory.flags = MEM_RD | MEM_WR;

return obj;
Expand Down Expand Up @@ -204,7 +210,7 @@ slice(VALUE self, long offset, long len)
result->memory.size = len;
result->memory.flags = ptr->memory.flags;
result->memory.typeSize = ptr->memory.typeSize;
result->data.rbParent = self;
RB_OBJ_WRITE(obj, &result->data.rbParent, self);

return obj;
}
Expand Down Expand Up @@ -334,6 +340,23 @@ buffer_mark(void *data)
rb_gc_mark(ptr->data.rbParent);
}

static size_t
buffer_memsize(const void *data)
{
return sizeof(Buffer);
}

static size_t
allocated_buffer_memsize(const void *data)
{
const Buffer *ptr = (const Buffer *)data;
size_t memsize = sizeof(Buffer);
if ((ptr->memory.flags & MEM_EMBED) == 0 && ptr->data.storage != NULL) {
memsize += ptr->memory.size;
}
return memsize;
}

void
rbffi_Buffer_Init(VALUE moduleFFI)
{
Expand Down
20 changes: 17 additions & 3 deletions ext/ffi_c/MemoryPointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

static VALUE memptr_allocate(VALUE klass);
static void memptr_release(void *data);
static size_t memptr_memsize(const void *data);
static VALUE memptr_malloc(VALUE self, long size, long count, bool clear);
static VALUE memptr_free(VALUE self);

Expand All @@ -58,18 +59,20 @@ static const rb_data_type_t memory_pointer_data_type = {
.function = {
.dmark = NULL,
.dfree = memptr_release,
.dsize = NULL,
.dsize = memptr_memsize,
},
.parent = &rbffi_pointer_data_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
// macro to update VALUE references, as to trigger write barriers.
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};

static VALUE
memptr_allocate(VALUE klass)
{
Pointer* p;
VALUE obj = TypedData_Make_Struct(klass, Pointer, &memory_pointer_data_type, p);
p->rbParent = Qnil;
RB_OBJ_WRITE(obj, &p->rbParent, Qnil);
p->memory.flags = MEM_RD | MEM_WR;

return obj;
Expand Down Expand Up @@ -157,6 +160,17 @@ memptr_release(void *data)
xfree(ptr);
}

static size_t
memptr_memsize(const void *data)
{
const Pointer *ptr = (const Pointer *)data;
size_t memsize = sizeof(Pointer);
if (ptr->allocated) {
memsize += ptr->memory.size;
}
return memsize;
}

/*
* call-seq: from_string(s)
* @param [String] s string
Expand Down
26 changes: 20 additions & 6 deletions ext/ffi_c/Pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ VALUE rbffi_NullPointerSingleton = Qnil;

static void ptr_release(void *data);
static void ptr_mark(void *data);
static size_t ptr_memsize(const void *data);

const rb_data_type_t rbffi_pointer_data_type = { /* extern */
.wrap_struct_name = "FFI::Pointer",
.function = {
.dmark = ptr_mark,
.dfree = ptr_release,
.dsize = NULL,
.dsize = ptr_memsize,
},
.parent = &rbffi_abstract_memory_data_type,
.flags = RUBY_TYPED_FREE_IMMEDIATELY
// IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
// macro to update VALUE references, as to trigger write barriers.
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};

VALUE
Expand All @@ -70,7 +73,7 @@ rbffi_Pointer_NewInstance(void* addr)
p->memory.size = LONG_MAX;
p->memory.flags = (addr == NULL) ? 0 : (MEM_RD | MEM_WR);
p->memory.typeSize = 1;
p->rbParent = Qnil;
RB_OBJ_WRITE(obj, &p->rbParent, Qnil);

return obj;
}
Expand All @@ -82,7 +85,7 @@ ptr_allocate(VALUE klass)
VALUE obj;

obj = TypedData_Make_Struct(klass, Pointer, &rbffi_pointer_data_type, p);
p->rbParent = Qnil;
RB_OBJ_WRITE(obj, &p->rbParent, Qnil);
p->memory.flags = MEM_RD | MEM_WR;

return obj;
Expand Down Expand Up @@ -134,7 +137,7 @@ ptr_initialize(int argc, VALUE* argv, VALUE self)
if (rb_obj_is_kind_of(rbAddress, rbffi_PointerClass)) {
Pointer* orig;

p->rbParent = rbAddress;
RB_OBJ_WRITE(self, &p->rbParent, rbAddress);
TypedData_Get_Struct(rbAddress, Pointer, &rbffi_pointer_data_type, orig);
p->memory = orig->memory;
} else {
Expand Down Expand Up @@ -215,7 +218,7 @@ slice(VALUE self, long offset, long size)
p->memory.size = size;
p->memory.flags = ptr->flags;
p->memory.typeSize = ptr->typeSize;
p->rbParent = self;
RB_OBJ_WRITE(retval, &p->rbParent, self);

return retval;
}
Expand Down Expand Up @@ -471,6 +474,17 @@ ptr_mark(void *data)
rb_gc_mark(ptr->rbParent);
}

static size_t
ptr_memsize(const void *data)
{
const Pointer *ptr = (const Pointer *)data;
size_t memsize = sizeof(Pointer);
if (ptr->allocated) {
memsize += ptr->memory.size;
}
return memsize;
}

void
rbffi_Pointer_Init(VALUE moduleFFI)
{
Expand Down
10 changes: 10 additions & 0 deletions spec/ffi/buffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,13 @@
expect(block_executed).to be true
end
end

describe "Buffer#memsize_of" do
it "has a memsize function", skip: RUBY_ENGINE != "ruby" do
base_size = ObjectSpace.memsize_of(Object.new)

buf = FFI::Buffer.new 14
size = ObjectSpace.memsize_of(buf)
expect(size).to be > base_size
end
end
8 changes: 8 additions & 0 deletions spec/ffi/pointer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,13 @@ def self.release(ptr); end
expect(mptr[1].read_uint).to eq(0xcafebabe)
end
end

it "has a memsize function", skip: RUBY_ENGINE != "ruby" do
base_size = ObjectSpace.memsize_of(Object.new)

pointer = FFI::Pointer.new(:int, 0xdeadbeef)
size = ObjectSpace.memsize_of(pointer)
expect(size).to be > base_size
end
end

8 changes: 8 additions & 0 deletions spec/ffi/rbx/memory_pointer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,12 @@
end
expect(block_executed).to be true
end

it "has a memsize function", skip: RUBY_ENGINE != "ruby" do
base_size = ObjectSpace.memsize_of(Object.new)

pointer = FFI::MemoryPointer.from_string("FFI is Awesome")
size = ObjectSpace.memsize_of(pointer)
expect(size).to be > base_size
end
end