diff options
author | Wayne Meissner <wmeissner@gmail.com> | 2009-08-11 17:53:06 +1000 |
---|---|---|
committer | Wayne Meissner <wmeissner@gmail.com> | 2009-08-11 17:53:06 +1000 |
commit | 54cf8db5243e6f90d0af620ab647abb883ba1465 (patch) | |
tree | 629cd7dd9226026ad9dcc5ec2d64c0437e184266 /ext/ffi_c/MemoryPointer.c | |
parent | 87185bee7253998599ead4dbca9bda7bf8b3e794 (diff) | |
download | ffi-54cf8db5243e6f90d0af620ab647abb883ba1465.tar.gz |
Pull typeSize up into AbstractMemory, so [] can also be pulled up there.
Move a few other common methods out of Buffer/Pointer into AbstractMemory
Diffstat (limited to 'ext/ffi_c/MemoryPointer.c')
-rw-r--r-- | ext/ffi_c/MemoryPointer.c | 73 |
1 files changed, 47 insertions, 26 deletions
diff --git a/ext/ffi_c/MemoryPointer.c b/ext/ffi_c/MemoryPointer.c index 5faa80c..9eaeb80 100644 --- a/ext/ffi_c/MemoryPointer.c +++ b/ext/ffi_c/MemoryPointer.c @@ -1,3 +1,32 @@ +/* + * Copyright (c) 2008, 2009, Wayne Meissner + * Copyright (c) 2008, Luc Heinrich <luc@honk-honk.com> + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * The name of the author or authors may not be used to endorse or promote + * products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include <stdbool.h> #include <stdint.h> #include <limits.h> @@ -10,7 +39,6 @@ typedef struct MemoryPointer { AbstractMemory memory; char* storage; /* start of malloc area */ - int type_size; bool autorelease; bool allocated; } MemoryPointer; @@ -24,8 +52,6 @@ VALUE rbffi_MemoryPointerClass; #define MEMPTR(obj) ((MemoryPointer *) rbffi_AbstractMemory_Cast(obj, rbffi_MemoryPointerClass)) -static ID plus_id = 0; - VALUE rbffi_MemoryPointer_NewInstance(long size, long count, bool clear) { @@ -66,7 +92,6 @@ memptr_malloc(VALUE self, long size, long count, bool clear) unsigned long msize; Data_Get_Struct(self, MemoryPointer, p); - p->type_size = size; msize = size * count; @@ -75,43 +100,38 @@ memptr_malloc(VALUE self, long size, long count, bool clear) rb_raise(rb_eNoMemError, "Failed to allocate memory size=%ld bytes", msize); } p->autorelease = true; + p->memory.typeSize = size; p->memory.size = msize; /* ensure the memory is aligned on at least a 8 byte boundary */ p->memory.address = (char *) (((uintptr_t) p->storage + 0x7) & (uintptr_t) ~0x7UL);; p->allocated = true; + if (clear && p->memory.size > 0) { memset(p->memory.address, 0, p->memory.size); } + return self; } static VALUE memptr_inspect(VALUE self) { - MemoryPointer* ptr = MEMPTR(self); + MemoryPointer* ptr; char tmp[100]; - snprintf(tmp, sizeof(tmp), "#<MemoryPointer address=%p size=%lu>", ptr->memory.address, ptr->memory.size); - return rb_str_new2(tmp); -} -static VALUE -memptr_type_size(VALUE self) -{ - return INT2FIX(MEMPTR(self)->type_size); -} + Data_Get_Struct(self, MemoryPointer, ptr); -static VALUE -memptr_aref(VALUE self, VALUE which) -{ - MemoryPointer* ptr = MEMPTR(self); - VALUE offset = INT2NUM(ptr->type_size * NUM2INT(which)); - return rb_funcall2(self, plus_id, 1, &offset); + snprintf(tmp, sizeof(tmp), "#<FFI::MemoryPointer address=%p size=%lu>", ptr->memory.address, ptr->memory.size); + return rb_str_new2(tmp); } static VALUE memptr_free(VALUE self) { - MemoryPointer* ptr = MEMPTR(self); + MemoryPointer* ptr; + + Data_Get_Struct(self, MemoryPointer, ptr); + if (ptr->allocated) { if (ptr->storage != NULL) { free(ptr->storage); @@ -119,14 +139,19 @@ memptr_free(VALUE self) } ptr->allocated = false; } + return self; } static VALUE memptr_autorelease(VALUE self, VALUE autorelease) { - MEMPTR(self)->autorelease = autorelease == Qtrue; - return self; + MemoryPointer* ptr; + + Data_Get_Struct(self, MemoryPointer, ptr); + ptr->autorelease = autorelease == Qtrue; + + return autorelease; } static void @@ -150,8 +175,4 @@ rbffi_MemoryPointer_Init(VALUE moduleFFI) rb_define_method(rbffi_MemoryPointerClass, "inspect", memptr_inspect, 0); rb_define_method(rbffi_MemoryPointerClass, "autorelease=", memptr_autorelease, 1); rb_define_method(rbffi_MemoryPointerClass, "free", memptr_free, 0); - rb_define_method(rbffi_MemoryPointerClass, "type_size", memptr_type_size, 0); - rb_define_method(rbffi_MemoryPointerClass, "[]", memptr_aref, 1); - - plus_id = rb_intern("+"); } |