ffi 0.4.0-x86-mswin32 → 0.5.0-x86-mswin32

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ffi might be problematic. Click here for more details.

Files changed (57) hide show
  1. data/README.rdoc +15 -15
  2. data/Rakefile +57 -8
  3. data/ext/ffi_c/AbstractMemory.c +101 -24
  4. data/ext/ffi_c/AbstractMemory.h +98 -6
  5. data/ext/ffi_c/ArrayType.c +129 -0
  6. data/ext/ffi_c/ArrayType.h +58 -0
  7. data/ext/ffi_c/AutoPointer.c +1 -0
  8. data/ext/ffi_c/Buffer.c +59 -43
  9. data/ext/ffi_c/Call.c +853 -0
  10. data/ext/ffi_c/Call.h +86 -0
  11. data/ext/ffi_c/ClosurePool.c +302 -0
  12. data/ext/ffi_c/ClosurePool.h +29 -0
  13. data/ext/ffi_c/DynamicLibrary.c +3 -0
  14. data/ext/ffi_c/Function.c +478 -0
  15. data/ext/ffi_c/Function.h +80 -0
  16. data/ext/ffi_c/FunctionInfo.c +221 -0
  17. data/ext/ffi_c/LastError.c +30 -6
  18. data/ext/ffi_c/MemoryPointer.c +50 -28
  19. data/ext/ffi_c/MethodHandle.c +346 -0
  20. data/ext/ffi_c/MethodHandle.h +53 -0
  21. data/ext/ffi_c/Pointer.c +73 -13
  22. data/ext/ffi_c/Pointer.h +31 -7
  23. data/ext/ffi_c/Struct.c +517 -224
  24. data/ext/ffi_c/Struct.h +60 -6
  25. data/ext/ffi_c/StructByValue.c +140 -0
  26. data/ext/ffi_c/StructByValue.h +53 -0
  27. data/ext/ffi_c/StructLayout.c +450 -0
  28. data/ext/ffi_c/Type.c +121 -22
  29. data/ext/ffi_c/Type.h +37 -8
  30. data/ext/ffi_c/Types.c +46 -61
  31. data/ext/ffi_c/Types.h +38 -7
  32. data/ext/ffi_c/Variadic.c +260 -0
  33. data/ext/ffi_c/compat.h +53 -3
  34. data/ext/ffi_c/extconf.rb +6 -7
  35. data/ext/ffi_c/ffi.c +45 -39
  36. data/ext/ffi_c/libffi.darwin.mk +2 -2
  37. data/ext/ffi_c/rbffi.h +3 -0
  38. data/lib/1.8/ffi_c.so +0 -0
  39. data/lib/1.9/ffi_c.so +0 -0
  40. data/lib/ffi/ffi.rb +7 -4
  41. data/lib/ffi/library.rb +34 -59
  42. data/lib/ffi/platform.rb +14 -4
  43. data/lib/ffi/struct.rb +110 -281
  44. data/lib/ffi/union.rb +4 -9
  45. data/lib/ffi/variadic.rb +1 -6
  46. data/spec/ffi/buffer_spec.rb +6 -0
  47. data/spec/ffi/callback_spec.rb +34 -3
  48. data/spec/ffi/function_spec.rb +73 -0
  49. data/spec/ffi/library_spec.rb +56 -52
  50. data/spec/ffi/pointer_spec.rb +3 -3
  51. data/spec/ffi/struct_callback_spec.rb +26 -3
  52. data/spec/ffi/struct_spec.rb +56 -3
  53. metadata +34 -13
  54. data/ext/ffi_c/Callback.c +0 -374
  55. data/ext/ffi_c/Callback.h +0 -47
  56. data/ext/ffi_c/Invoker.c +0 -962
  57. data/ext/ffi_c/NullPointer.c +0 -143
@@ -0,0 +1,80 @@
1
+ /*
2
+ * Copyright (c) 2009, Wayne Meissner
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * * Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * * Redistributions in binary form must reproduce the above copyright notice
11
+ * this list of conditions and the following disclaimer in the documentation
12
+ * and/or other materials provided with the distribution.
13
+ * * The name of the author or authors may not be used to endorse or promote
14
+ * products derived from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #ifndef RBFFI_FUNCTION_H
29
+ #define RBFFI_FUNCTION_H
30
+
31
+ #ifdef __cplusplus
32
+ extern "C" {
33
+ #endif
34
+
35
+ #include <stdbool.h>
36
+ #include <ffi.h>
37
+
38
+ typedef struct FunctionType_ FunctionType;
39
+
40
+ #include "Type.h"
41
+ #include "Call.h"
42
+ #include "ClosurePool.h"
43
+
44
+ struct FunctionType_ {
45
+ Type type; // The native type of a FunctionInfo object
46
+ VALUE rbReturnType;
47
+ VALUE rbParameterTypes;
48
+
49
+ Type* returnType;
50
+ Type** parameterTypes;
51
+ NativeType* nativeParameterTypes;
52
+ ffi_type* ffiReturnType;
53
+ ffi_type** ffiParameterTypes;
54
+ ffi_cif ffi_cif;
55
+ Invoker invoke;
56
+ ClosurePool* closurePool;
57
+ int parameterCount;
58
+ int flags;
59
+ ffi_abi abi;
60
+ int callbackCount;
61
+ VALUE* callbackParameters;
62
+ VALUE rbEnums;
63
+ bool ignoreErrno;
64
+ bool blocking;
65
+ bool hasStruct;
66
+ };
67
+
68
+ extern VALUE rbffi_FunctionTypeClass, rbffi_FunctionClass;
69
+
70
+ void rbffi_Function_Init(VALUE moduleFFI);
71
+ VALUE rbffi_Function_NewInstance(VALUE functionInfo, VALUE proc);
72
+ VALUE rbffi_Function_ForProc(VALUE cbInfo, VALUE proc);
73
+ void rbffi_FunctionInfo_Init(VALUE moduleFFI);
74
+
75
+ #ifdef __cplusplus
76
+ }
77
+ #endif
78
+
79
+ #endif /* RBFFI_FUNCTION_H */
80
+
@@ -0,0 +1,221 @@
1
+ /*
2
+ * Copyright (c) 2009, Wayne Meissner
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions are met:
7
+ *
8
+ * * Redistributions of source code must retain the above copyright notice, this
9
+ * list of conditions and the following disclaimer.
10
+ * * Redistributions in binary form must reproduce the above copyright notice
11
+ * this list of conditions and the following disclaimer in the documentation
12
+ * and/or other materials provided with the distribution.
13
+ * * The name of the author or authors may not be used to endorse or promote
14
+ * products derived from this software without specific prior written permission.
15
+ *
16
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #include <sys/param.h>
29
+ #include <sys/types.h>
30
+ #include <stdio.h>
31
+ #include <stdint.h>
32
+ #include <stdbool.h>
33
+ #include <errno.h>
34
+ #include <ruby.h>
35
+
36
+ #include <ffi.h>
37
+ #include "rbffi.h"
38
+ #include "compat.h"
39
+
40
+ #include "AbstractMemory.h"
41
+ #include "Types.h"
42
+ #include "Type.h"
43
+ #include "StructByValue.h"
44
+ #include "Function.h"
45
+
46
+ static VALUE fntype_allocate(VALUE klass);
47
+ static VALUE fntype_initialize(int argc, VALUE* argv, VALUE self);
48
+ static void fntype_mark(FunctionType*);
49
+ static void fntype_free(FunctionType *);
50
+
51
+ VALUE rbffi_FunctionTypeClass = Qnil;
52
+
53
+ static VALUE
54
+ fntype_allocate(VALUE klass)
55
+ {
56
+ FunctionType* fnInfo;
57
+ VALUE obj = Data_Make_Struct(klass, FunctionType, fntype_mark, fntype_free, fnInfo);
58
+
59
+ fnInfo->type.ffiType = &ffi_type_pointer;
60
+ fnInfo->type.nativeType = NATIVE_FUNCTION;
61
+ fnInfo->rbReturnType = Qnil;
62
+ fnInfo->rbParameterTypes = Qnil;
63
+ fnInfo->rbEnums = Qnil;
64
+ fnInfo->invoke = rbffi_CallFunction;
65
+ fnInfo->closurePool = NULL;
66
+
67
+ return obj;
68
+ }
69
+
70
+ static void
71
+ fntype_mark(FunctionType* fnInfo)
72
+ {
73
+ rb_gc_mark(fnInfo->rbReturnType);
74
+ rb_gc_mark(fnInfo->rbParameterTypes);
75
+ rb_gc_mark(fnInfo->rbEnums);
76
+ if (fnInfo->callbackCount > 0 && fnInfo->callbackParameters != NULL) {
77
+ rb_gc_mark_locations(&fnInfo->callbackParameters[0], &fnInfo->callbackParameters[fnInfo->callbackCount]);
78
+ }
79
+ }
80
+
81
+ static void
82
+ fntype_free(FunctionType* fnInfo)
83
+ {
84
+ xfree(fnInfo->parameterTypes);
85
+ xfree(fnInfo->ffiParameterTypes);
86
+ xfree(fnInfo->nativeParameterTypes);
87
+ xfree(fnInfo->callbackParameters);
88
+ if (fnInfo->closurePool != NULL) {
89
+ rbffi_ClosurePool_Free(fnInfo->closurePool);
90
+ }
91
+ xfree(fnInfo);
92
+ }
93
+
94
+ static VALUE
95
+ fntype_initialize(int argc, VALUE* argv, VALUE self)
96
+ {
97
+ FunctionType *fnInfo;
98
+ ffi_status status;
99
+ VALUE rbReturnType = Qnil, rbParamTypes = Qnil, rbOptions = Qnil;
100
+ VALUE rbEnums = Qnil, rbConvention = Qnil, rbBlocking = Qnil;
101
+ int i, nargs;
102
+
103
+ nargs = rb_scan_args(argc, argv, "21", &rbReturnType, &rbParamTypes, &rbOptions);
104
+ if (nargs >= 3 && rbOptions != Qnil) {
105
+ rbConvention = rb_hash_aref(rbOptions, ID2SYM(rb_intern("convention")));
106
+ rbEnums = rb_hash_aref(rbOptions, ID2SYM(rb_intern("enums")));
107
+ rbBlocking = rb_hash_aref(rbOptions, ID2SYM(rb_intern("blocking")));
108
+ }
109
+
110
+ Check_Type(rbParamTypes, T_ARRAY);
111
+
112
+ Data_Get_Struct(self, FunctionType, fnInfo);
113
+ fnInfo->parameterCount = RARRAY_LEN(rbParamTypes);
114
+ fnInfo->parameterTypes = xcalloc(fnInfo->parameterCount, sizeof(*fnInfo->parameterTypes));
115
+ fnInfo->ffiParameterTypes = xcalloc(fnInfo->parameterCount, sizeof(ffi_type *));
116
+ fnInfo->nativeParameterTypes = xcalloc(fnInfo->parameterCount, sizeof(*fnInfo->nativeParameterTypes));
117
+ fnInfo->rbParameterTypes = rb_ary_new2(fnInfo->parameterCount);
118
+ fnInfo->rbEnums = rbEnums;
119
+ fnInfo->blocking = RTEST(rbBlocking);
120
+ fnInfo->hasStruct = false;
121
+
122
+ for (i = 0; i < fnInfo->parameterCount; ++i) {
123
+ VALUE entry = rb_ary_entry(rbParamTypes, i);
124
+ VALUE type = rbffi_Type_Lookup(entry);
125
+
126
+ if (!RTEST(type)) {
127
+ VALUE typeName = rb_funcall2(entry, rb_intern("inspect"), 0, NULL);
128
+ rb_raise(rb_eTypeError, "Invalid parameter type (%s)", RSTRING_PTR(typeName));
129
+ }
130
+
131
+ if (rb_obj_is_kind_of(type, rbffi_FunctionTypeClass)) {
132
+ REALLOC_N(fnInfo->callbackParameters, VALUE, fnInfo->callbackCount + 1);
133
+ fnInfo->callbackParameters[fnInfo->callbackCount++] = type;
134
+ }
135
+
136
+ if (rb_obj_is_kind_of(type, rbffi_StructByValueClass)) {
137
+ fnInfo->hasStruct = true;
138
+ }
139
+
140
+ rb_ary_push(fnInfo->rbParameterTypes, type);
141
+ Data_Get_Struct(type, Type, fnInfo->parameterTypes[i]);
142
+ fnInfo->ffiParameterTypes[i] = fnInfo->parameterTypes[i]->ffiType;
143
+ fnInfo->nativeParameterTypes[i] = fnInfo->parameterTypes[i]->nativeType;
144
+ }
145
+
146
+ fnInfo->rbReturnType = rbffi_Type_Lookup(rbReturnType);
147
+ if (!RTEST(fnInfo->rbReturnType)) {
148
+ VALUE typeName = rb_funcall2(rbReturnType, rb_intern("inspect"), 0, NULL);
149
+ rb_raise(rb_eTypeError, "Invalid return type (%s)", RSTRING_PTR(typeName));
150
+ }
151
+
152
+ if (rb_obj_is_kind_of(fnInfo->rbReturnType, rbffi_StructByValueClass)) {
153
+ fnInfo->hasStruct = true;
154
+ }
155
+
156
+ Data_Get_Struct(fnInfo->rbReturnType, Type, fnInfo->returnType);
157
+ fnInfo->ffiReturnType = fnInfo->returnType->ffiType;
158
+
159
+
160
+ #if defined(_WIN32) || defined(__WIN32__)
161
+ VALUE rbConventionStr = rb_funcall2(rbConvention, rb_intern("to_s"), 0, NULL);
162
+ fnInfo->abi = (rbConvention != Qnil && strcmp(StringValueCStr(rbConventionStr), "stdcall") == 0)
163
+ ? FFI_STDCALL : FFI_DEFAULT_ABI;
164
+ #else
165
+ fnInfo->abi = FFI_DEFAULT_ABI;
166
+ #endif
167
+
168
+ status = ffi_prep_cif(&fnInfo->ffi_cif, fnInfo->abi, fnInfo->parameterCount,
169
+ fnInfo->ffiReturnType, fnInfo->ffiParameterTypes);
170
+ switch (status) {
171
+ case FFI_BAD_ABI:
172
+ rb_raise(rb_eArgError, "Invalid ABI specified");
173
+ case FFI_BAD_TYPEDEF:
174
+ rb_raise(rb_eArgError, "Invalid argument type specified");
175
+ case FFI_OK:
176
+ break;
177
+ default:
178
+ rb_raise(rb_eArgError, "Unknown FFI error");
179
+ }
180
+
181
+ fnInfo->invoke = rbffi_GetInvoker(fnInfo);
182
+
183
+ return self;
184
+ }
185
+
186
+ static VALUE
187
+ fntype_result_type(VALUE self)
188
+ {
189
+ FunctionType* ft;
190
+
191
+ Data_Get_Struct(self, FunctionType, ft);
192
+
193
+ return ft->rbReturnType;
194
+ }
195
+
196
+ static VALUE
197
+ fntype_param_types(VALUE self)
198
+ {
199
+ FunctionType* ft;
200
+
201
+ Data_Get_Struct(self, FunctionType, ft);
202
+
203
+ return rb_ary_dup(ft->rbParameterTypes);
204
+ }
205
+
206
+ void
207
+ rbffi_FunctionInfo_Init(VALUE moduleFFI)
208
+ {
209
+ rbffi_FunctionTypeClass = rb_define_class_under(moduleFFI, "FunctionType", rbffi_TypeClass);
210
+ rb_global_variable(&rbffi_FunctionTypeClass);
211
+ rb_define_const(moduleFFI, "CallbackInfo", rbffi_FunctionTypeClass);
212
+ rb_define_const(moduleFFI, "FunctionInfo", rbffi_FunctionTypeClass);
213
+ rb_define_const(rbffi_TypeClass, "Function", rbffi_FunctionTypeClass);
214
+
215
+ rb_define_alloc_func(rbffi_FunctionTypeClass, fntype_allocate);
216
+ rb_define_method(rbffi_FunctionTypeClass, "initialize", fntype_initialize, -1);
217
+ rb_define_method(rbffi_FunctionTypeClass, "result_type", fntype_result_type, 0);
218
+ rb_define_method(rbffi_FunctionTypeClass, "param_types", fntype_param_types, 0);
219
+
220
+ }
221
+
@@ -1,3 +1,31 @@
1
+ /*
2
+ * Copyright (c) 2008, 2009, Wayne Meissner
3
+ *
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice, this
10
+ * list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright notice
12
+ * this list of conditions and the following disclaimer in the documentation
13
+ * and/or other materials provided with the distribution.
14
+ * * The name of the author or authors may not be used to endorse or promote
15
+ * products derived from this software without specific prior written permission.
16
+ *
17
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+ */
28
+
1
29
  #include <sys/param.h>
2
30
  #include <sys/types.h>
3
31
  #include <stdio.h>
@@ -10,10 +38,7 @@
10
38
 
11
39
  #if defined(HAVE_NATIVETHREAD) && !defined(_WIN32) && !defined(__WIN32__)
12
40
  # include <pthread.h>
13
- #endif
14
-
15
- #if defined(HAVE_NATIVETHREAD) && !defined(_WIN32) && !defined(__WIN32__)
16
- # define USE_PTHREAD_LOCAL
41
+ # define USE_PTHREAD_LOCAL
17
42
  #endif
18
43
 
19
44
  typedef struct ThreadData {
@@ -31,9 +56,8 @@ static inline ThreadData* thread_data_get(void);
31
56
  static ThreadData*
32
57
  thread_data_init(void)
33
58
  {
34
- ThreadData* td = ALLOC_N(ThreadData, 1);
59
+ ThreadData* td = xcalloc(1, sizeof(ThreadData));
35
60
 
36
- memset(td, 0, sizeof(*td));
37
61
  pthread_setspecific(threadDataKey, td);
38
62
 
39
63
  return td;
@@ -1,3 +1,32 @@
1
+ /*
2
+ * Copyright (c) 2008, 2009, Wayne Meissner
3
+ * Copyright (c) 2008, Luc Heinrich <luc@honk-honk.com>
4
+ *
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * * Redistributions of source code must retain the above copyright notice, this
11
+ * list of conditions and the following disclaimer.
12
+ * * Redistributions in binary form must reproduce the above copyright notice
13
+ * this list of conditions and the following disclaimer in the documentation
14
+ * and/or other materials provided with the distribution.
15
+ * * The name of the author or authors may not be used to endorse or promote
16
+ * products derived from this software without specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ */
29
+
1
30
  #include <stdbool.h>
2
31
  #include <stdint.h>
3
32
  #include <limits.h>
@@ -10,7 +39,6 @@
10
39
  typedef struct MemoryPointer {
11
40
  AbstractMemory memory;
12
41
  char* storage; /* start of malloc area */
13
- int type_size;
14
42
  bool autorelease;
15
43
  bool allocated;
16
44
  } MemoryPointer;
@@ -24,8 +52,6 @@ VALUE rbffi_MemoryPointerClass;
24
52
 
25
53
  #define MEMPTR(obj) ((MemoryPointer *) rbffi_AbstractMemory_Cast(obj, rbffi_MemoryPointerClass))
26
54
 
27
- static ID plus_id = 0;
28
-
29
55
  VALUE
30
56
  rbffi_MemoryPointer_NewInstance(long size, long count, bool clear)
31
57
  {
@@ -38,6 +64,7 @@ memptr_allocate(VALUE klass)
38
64
  MemoryPointer* p;
39
65
  VALUE obj = Data_Make_Struct(klass, MemoryPointer, NULL, memptr_release, p);
40
66
  p->memory.ops = &rbffi_AbstractMemoryOps;
67
+ p->memory.access = MEM_RD | MEM_WR;
41
68
 
42
69
  return obj;
43
70
  }
@@ -65,52 +92,46 @@ memptr_malloc(VALUE self, long size, long count, bool clear)
65
92
  unsigned long msize;
66
93
 
67
94
  Data_Get_Struct(self, MemoryPointer, p);
68
- p->type_size = size;
69
95
 
70
96
  msize = size * count;
71
97
 
72
- p->storage = malloc(msize + 7);
98
+ p->storage = xmalloc(msize + 7);
73
99
  if (p->storage == NULL) {
74
100
  rb_raise(rb_eNoMemError, "Failed to allocate memory size=%ld bytes", msize);
75
101
  }
76
102
  p->autorelease = true;
103
+ p->memory.typeSize = size;
77
104
  p->memory.size = msize;
78
105
  /* ensure the memory is aligned on at least a 8 byte boundary */
79
106
  p->memory.address = (char *) (((uintptr_t) p->storage + 0x7) & (uintptr_t) ~0x7UL);;
80
107
  p->allocated = true;
108
+
81
109
  if (clear && p->memory.size > 0) {
82
110
  memset(p->memory.address, 0, p->memory.size);
83
111
  }
112
+
84
113
  return self;
85
114
  }
86
115
 
87
116
  static VALUE
88
117
  memptr_inspect(VALUE self)
89
118
  {
90
- MemoryPointer* ptr = MEMPTR(self);
119
+ MemoryPointer* ptr;
91
120
  char tmp[100];
92
- snprintf(tmp, sizeof(tmp), "#<MemoryPointer address=%p size=%lu>", ptr->memory.address, ptr->memory.size);
93
- return rb_str_new2(tmp);
94
- }
95
121
 
96
- static VALUE
97
- memptr_type_size(VALUE self)
98
- {
99
- return INT2FIX(MEMPTR(self)->type_size);
100
- }
122
+ Data_Get_Struct(self, MemoryPointer, ptr);
101
123
 
102
- static VALUE
103
- memptr_aref(VALUE self, VALUE which)
104
- {
105
- MemoryPointer* ptr = MEMPTR(self);
106
- VALUE offset = INT2NUM(ptr->type_size * NUM2INT(which));
107
- return rb_funcall2(self, plus_id, 1, &offset);
124
+ snprintf(tmp, sizeof(tmp), "#<FFI::MemoryPointer address=%p size=%lu>", ptr->memory.address, ptr->memory.size);
125
+ return rb_str_new2(tmp);
108
126
  }
109
127
 
110
128
  static VALUE
111
129
  memptr_free(VALUE self)
112
130
  {
113
- MemoryPointer* ptr = MEMPTR(self);
131
+ MemoryPointer* ptr;
132
+
133
+ Data_Get_Struct(self, MemoryPointer, ptr);
134
+
114
135
  if (ptr->allocated) {
115
136
  if (ptr->storage != NULL) {
116
137
  free(ptr->storage);
@@ -118,21 +139,26 @@ memptr_free(VALUE self)
118
139
  }
119
140
  ptr->allocated = false;
120
141
  }
142
+
121
143
  return self;
122
144
  }
123
145
 
124
146
  static VALUE
125
147
  memptr_autorelease(VALUE self, VALUE autorelease)
126
148
  {
127
- MEMPTR(self)->autorelease = autorelease == Qtrue;
128
- return self;
149
+ MemoryPointer* ptr;
150
+
151
+ Data_Get_Struct(self, MemoryPointer, ptr);
152
+ ptr->autorelease = autorelease == Qtrue;
153
+
154
+ return autorelease;
129
155
  }
130
156
 
131
157
  static void
132
158
  memptr_release(MemoryPointer* ptr)
133
159
  {
134
160
  if (ptr->autorelease && ptr->allocated && ptr->storage != NULL) {
135
- free(ptr->storage);
161
+ xfree(ptr->storage);
136
162
  ptr->storage = NULL;
137
163
  }
138
164
  xfree(ptr);
@@ -149,8 +175,4 @@ rbffi_MemoryPointer_Init(VALUE moduleFFI)
149
175
  rb_define_method(rbffi_MemoryPointerClass, "inspect", memptr_inspect, 0);
150
176
  rb_define_method(rbffi_MemoryPointerClass, "autorelease=", memptr_autorelease, 1);
151
177
  rb_define_method(rbffi_MemoryPointerClass, "free", memptr_free, 0);
152
- rb_define_method(rbffi_MemoryPointerClass, "type_size", memptr_type_size, 0);
153
- rb_define_method(rbffi_MemoryPointerClass, "[]", memptr_aref, 1);
154
-
155
- plus_id = rb_intern("+");
156
178
  }