Skip to content

Commit

Permalink
More Buffer api
Browse files Browse the repository at this point in the history
  • Loading branch information
vp-of-awesome committed Sep 18, 2008
1 parent 379e9dd commit 2592f27
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
8 changes: 5 additions & 3 deletions ext/Buffer.c
Expand Up @@ -24,8 +24,8 @@ buffer_allocate(VALUE self, VALUE size, VALUE count, VALUE clear)

p = ALLOC(Buffer);
memset(p, 0, sizeof(*p));
p->memory.size = NUM2LONG(size) * NUM2LONG(count);
p->memory.address = malloc(p->memory.size);
p->memory.size = NUM2LONG(size) * (count == Qnil ? 1 : NUM2LONG(count));
p->memory.address = p->memory.size > 0 ? malloc(p->memory.size) : NULL;
p->parent = Qnil;

if (p->memory.address == NULL) {
Expand Down Expand Up @@ -86,7 +86,9 @@ rb_FFI_Buffer_Init()
{
VALUE moduleFFI = rb_define_module("FFI");
rb_FFI_Buffer_class = classBuffer = rb_define_class_under(moduleFFI, "Buffer", rb_FFI_AbstractMemory_class);
rb_define_singleton_method(classBuffer, "__allocate", buffer_allocate, 3);
rb_define_singleton_method(classBuffer, "__alloc_inout", buffer_allocate, 3);
rb_define_singleton_method(classBuffer, "__alloc_out", buffer_allocate, 3);
rb_define_singleton_method(classBuffer, "__alloc_in", buffer_allocate, 3);
rb_define_method(classBuffer, "inspect", buffer_inspect, 0);
rb_define_method(classBuffer, "+", buffer_plus, 1);
}
Expand Down
6 changes: 3 additions & 3 deletions ext/MemoryPointer.c
Expand Up @@ -41,15 +41,15 @@ memptr_allocate(VALUE self, VALUE size, VALUE count, VALUE clear)
p = ALLOC(MemoryPointer);
memset(p, 0, sizeof(*p));
p->autorelease = true;
p->memory.size = NUM2ULONG(size) * NUM2ULONG(count);
p->memory.address = malloc(p->memory.size);
p->memory.size = NUM2LONG(size) * (count == Qnil ? 1 : NUM2LONG(count));
p->memory.address = p->memory.size > 0 ? malloc(p->memory.size) : NULL;
p->parent = Qnil;
p->allocated = true;

if (p->memory.address == NULL) {
int size = p->memory.size;
xfree(p);
rb_raise(rb_eNoMemError, "Failed to allocate memory size=%u bytes", size);
rb_raise(rb_eNoMemError, "Failed to allocate memory size=%ld bytes", size);
}
if (TYPE(clear) == T_TRUE) {
memset(p->memory.address, 0, p->memory.size);
Expand Down
25 changes: 25 additions & 0 deletions lib/ffi/buffer.rb
@@ -0,0 +1,25 @@
module FFI
class Buffer
def self.__calc_size(type)
if type.kind_of? Fixnum
type
elsif type.kind_of? Symbol
FFI.type_size(type)
else
type.size
end
end
def self.new(size, count=nil, clear=true)
self.__alloc_inout(self.__calc_size(size), count, clear)
end
def self.alloc_in(size, count=nil, clear=true)
self.__alloc_in(self.__calc_size(size), count, clear)
end
def self.alloc_out(size, count=nil, clear=true)
self.__alloc_out(self.__calc_size(size), count, clear)
end
def self.alloc_inout(size, count=nil, clear=true)
self.__alloc_inout(self.__calc_size(size), count, clear)
end
end
end
1 change: 1 addition & 0 deletions lib/ffi/ffi.rb
Expand Up @@ -68,6 +68,7 @@ def initialize(function, library)

require 'ffi/platform'
require 'ffi/memorypointer'
require 'ffi/buffer'
require 'ffi/struct'
require 'ffi/callback'

Expand Down
2 changes: 1 addition & 1 deletion lib/ffi/memorypointer.rb
Expand Up @@ -30,7 +30,7 @@ def self.new(type, count=nil, clear=true)
else
type.size
end
ptr = self.__allocate(size, count ? count : 1, clear)
ptr = self.__allocate(size, count, clear)
ptr.type_size = size
if block_given?
begin
Expand Down

0 comments on commit 2592f27

Please sign in to comment.