Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

pointer.c
Go to the documentation of this file.
1 /* -*- C -*-
2 * $Id$
3 */
4
5 #include <ruby/ruby.h>
6 #include <ruby/io.h>
7 #include <ctype.h>
8 #include <fiddle.h>
9
10 VALUE rb_cPointer;
11
12 typedef void (*freefunc_t)(void*);
13
14 struct ptr_data {
15 void *ptr;
16 long size;
17 freefunc_t free;
18 VALUE wrap[2];
19 };
20
21 #define RPTR_DATA(obj) ((struct ptr_data *)(DATA_PTR(obj)))
22
23 static inline freefunc_t
24 get_freefunc(VALUE func, volatile VALUE *wrap)
25 {
26 VALUE addrnum;
27 if (NIL_P(func)) {
28 *wrap = 0;
29 return NULL;
30 }
31 addrnum = rb_Integer(func);
32 *wrap = (addrnum != func) ? func : 0;
33 return (freefunc_t)(VALUE)NUM2PTR(addrnum);
34 }
35
36 static ID id_to_ptr;
37
38 static void
39 fiddle_ptr_mark(void *ptr)
40 {
41 struct ptr_data *data = ptr;
42 if (data->wrap[0]) {
43 rb_gc_mark(data->wrap[0]);
44 }
45 if (data->wrap[1]) {
46 rb_gc_mark(data->wrap[1]);
47 }
48 }
49
50 static void
51 fiddle_ptr_free(void *ptr)
52 {
53 struct ptr_data *data = ptr;
54 if (data->ptr) {
55 if (data->free) {
56 (*(data->free))(data->ptr);
57 }
58 }
59 xfree(ptr);
60 }
61
62 static size_t
63 fiddle_ptr_memsize(const void *ptr)
64 {
65 const struct ptr_data *data = ptr;
66 return data ? sizeof(*data) + data->size : 0;
67 }
68
69 static const rb_data_type_t fiddle_ptr_data_type = {
70 "fiddle/pointer",
71 {fiddle_ptr_mark, fiddle_ptr_free, fiddle_ptr_memsize,},
72 };
73
74 static VALUE
75 rb_fiddle_ptr_new2(VALUE klass, void *ptr, long size, freefunc_t func)
76 {
77 struct ptr_data *data;
78 VALUE val;
79
80 val = TypedData_Make_Struct(klass, struct ptr_data, &fiddle_ptr_data_type, data);
81 data->ptr = ptr;
82 data->free = func;
83 data->size = size;
84 OBJ_TAINT(val);
85
86 return val;
87 }
88
89 static VALUE
90 rb_fiddle_ptr_new(void *ptr, long size, freefunc_t func)
91 {
92 return rb_fiddle_ptr_new2(rb_cPointer, ptr, size, func);
93 }
94
95 static VALUE
96 rb_fiddle_ptr_malloc(long size, freefunc_t func)
97 {
98 void *ptr;
99
100 ptr = ruby_xmalloc((size_t)size);
101 memset(ptr,0,(size_t)size);
102 return rb_fiddle_ptr_new(ptr, size, func);
103 }
104
105 static void *
106 rb_fiddle_ptr2cptr(VALUE val)
107 {
108 struct ptr_data *data;
109 void *ptr;
110
111 if (rb_obj_is_kind_of(val, rb_cPointer)) {
112 TypedData_Get_Struct(val, struct ptr_data, &fiddle_ptr_data_type, data);
113 ptr = data->ptr;
114 }
115 else if (val == Qnil) {
116 ptr = NULL;
117 }
118 else{
119 rb_raise(rb_eTypeError, "Fiddle::Pointer was expected");
120 }
121
122 return ptr;
123 }
124
125 static VALUE
126 rb_fiddle_ptr_s_allocate(VALUE klass)
127 {
128 VALUE obj;
129 struct ptr_data *data;
130
131 obj = TypedData_Make_Struct(klass, struct ptr_data, &fiddle_ptr_data_type, data);
132 data->ptr = 0;
133 data->size = 0;
134 data->free = 0;
135
136 return obj;
137 }
138
139 /*
140 * call-seq:
141 * Fiddle::Pointer.new(address) => fiddle_cptr
142 * new(address, size) => fiddle_cptr
143 * new(address, size, freefunc) => fiddle_cptr
144 *
145 * Create a new pointer to +address+ with an optional +size+ and +freefunc+.
146 *
147 * +freefunc+ will be called when the instance is garbage collected.
148 */
149 static VALUE
150 rb_fiddle_ptr_initialize(int argc, VALUE argv[], VALUE self)
151 {
152 VALUE ptr, sym, size, wrap = 0, funcwrap = 0;
153 struct ptr_data *data;
154 void *p = NULL;
155 freefunc_t f = NULL;
156 long s = 0;
157
158 if (rb_scan_args(argc, argv, "12", &ptr, &size, &sym) >= 1) {
159 VALUE addrnum = rb_Integer(ptr);
160 if (addrnum != ptr) wrap = ptr;
161 p = NUM2PTR(addrnum);
162 }
163 if (argc >= 2) {
164 s = NUM2LONG(size);
165 }
166 if (argc >= 3) {
167 f = get_freefunc(sym, &funcwrap);
168 }
169
170 if (p) {
171 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
172 if (data->ptr && data->free) {
173 /* Free previous memory. Use of inappropriate initialize may cause SEGV. */
174 (*(data->free))(data->ptr);
175 }
176 data->wrap[0] = wrap;
177 data->wrap[1] = funcwrap;
178 data->ptr = p;
179 data->size = s;
180 data->free = f;
181 }
182
183 return Qnil;
184 }
185
186 /*
187 * call-seq:
188 *
189 * Fiddle::Pointer.malloc(size, freefunc = nil) => fiddle pointer instance
190 *
191 * Allocate +size+ bytes of memory and associate it with an optional
192 * +freefunc+ that will be called when the pointer is garbage collected.
193 *
194 * +freefunc+ must be an address pointing to a function or an instance of
195 * Fiddle::Function
196 */
197 static VALUE
198 rb_fiddle_ptr_s_malloc(int argc, VALUE argv[], VALUE klass)
199 {
200 VALUE size, sym, obj, wrap = 0;
201 long s;
202 freefunc_t f;
203
204 switch (rb_scan_args(argc, argv, "11", &size, &sym)) {
205 case 1:
206 s = NUM2LONG(size);
207 f = NULL;
208 break;
209 case 2:
210 s = NUM2LONG(size);
211 f = get_freefunc(sym, &wrap);
212 break;
213 default:
214 rb_bug("rb_fiddle_ptr_s_malloc");
215 }
216
217 obj = rb_fiddle_ptr_malloc(s,f);
218 if (wrap) RPTR_DATA(obj)->wrap[1] = wrap;
219
220 return obj;
221 }
222
223 /*
224 * call-seq: to_i
225 *
226 * Returns the integer memory location of this pointer.
227 */
228 static VALUE
229 rb_fiddle_ptr_to_i(VALUE self)
230 {
231 struct ptr_data *data;
232
233 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
234 return PTR2NUM(data->ptr);
235 }
236
237 /*
238 * call-seq: to_value
239 *
240 * Cast this pointer to a ruby object.
241 */
242 static VALUE
243 rb_fiddle_ptr_to_value(VALUE self)
244 {
245 struct ptr_data *data;
246 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
247 return (VALUE)(data->ptr);
248 }
249
250 /*
251 * call-seq: ptr
252 *
253 * Returns a new Fiddle::Pointer instance that is a dereferenced pointer for
254 * this pointer.
255 *
256 * Analogous to the star operator in C.
257 */
258 static VALUE
259 rb_fiddle_ptr_ptr(VALUE self)
260 {
261 struct ptr_data *data;
262
263 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
264 return rb_fiddle_ptr_new(*((void**)(data->ptr)),0,0);
265 }
266
267 /*
268 * call-seq: ref
269 *
270 * Returns a new Fiddle::Pointer instance that is a reference pointer for this
271 * pointer.
272 *
273 * Analogous to the ampersand operator in C.
274 */
275 static VALUE
276 rb_fiddle_ptr_ref(VALUE self)
277 {
278 struct ptr_data *data;
279
280 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
281 return rb_fiddle_ptr_new(&(data->ptr),0,0);
282 }
283
284 /*
285 * call-seq: null?
286 *
287 * Returns +true+ if this is a null pointer.
288 */
289 static VALUE
290 rb_fiddle_ptr_null_p(VALUE self)
291 {
292 struct ptr_data *data;
293
294 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
295 return data->ptr ? Qfalse : Qtrue;
296 }
297
298 /*
299 * call-seq: free=(function)
300 *
301 * Set the free function for this pointer to +function+ in the given
302 * Fiddle::Function.
303 */
304 static VALUE
305 rb_fiddle_ptr_free_set(VALUE self, VALUE val)
306 {
307 struct ptr_data *data;
308
309 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
310 data->free = get_freefunc(val, &data->wrap[1]);
311
312 return Qnil;
313 }
314
315 /*
316 * call-seq: free => Fiddle::Function
317 *
318 * Get the free function for this pointer.
319 *
320 * Returns a new instance of Fiddle::Function.
321 *
322 * See Fiddle::Function.new
323 */
324 static VALUE
325 rb_fiddle_ptr_free_get(VALUE self)
326 {
327 struct ptr_data *pdata;
328 VALUE address;
329 VALUE arg_types;
330 VALUE ret_type;
331
332 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, pdata);
333
334 if (!pdata->free)
335 return Qnil;
336
337 address = PTR2NUM(pdata->free);
338 ret_type = INT2NUM(TYPE_VOID);
339 arg_types = rb_ary_new();
340 rb_ary_push(arg_types, INT2NUM(TYPE_VOIDP));
341
342 return rb_fiddle_new_function(address, arg_types, ret_type);
343 }
344
345 /*
346 * call-seq:
347 *
348 * ptr.to_s => string
349 * ptr.to_s(len) => string
350 *
351 * Returns the pointer contents as a string.
352 *
353 * When called with no arguments, this method will return the contents until
354 * the first NULL byte.
355 *
356 * When called with +len+, a string of +len+ bytes will be returned.
357 *
358 * See to_str
359 */
360 static VALUE
361 rb_fiddle_ptr_to_s(int argc, VALUE argv[], VALUE self)
362 {
363 struct ptr_data *data;
364 VALUE arg1, val;
365 int len;
366
367 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
368 switch (rb_scan_args(argc, argv, "01", &arg1)) {
369 case 0:
370 val = rb_tainted_str_new2((char*)(data->ptr));
371 break;
372 case 1:
373 len = NUM2INT(arg1);
374 val = rb_tainted_str_new((char*)(data->ptr), len);
375 break;
376 default:
377 rb_bug("rb_fiddle_ptr_to_s");
378 }
379
380 return val;
381 }
382
383 /*
384 * call-seq:
385 *
386 * ptr.to_str => string
387 * ptr.to_str(len) => string
388 *
389 * Returns the pointer contents as a string.
390 *
391 * When called with no arguments, this method will return the contents with the
392 * length of this pointer's +size+.
393 *
394 * When called with +len+, a string of +len+ bytes will be returned.
395 *
396 * See to_s
397 */
398 static VALUE
399 rb_fiddle_ptr_to_str(int argc, VALUE argv[], VALUE self)
400 {
401 struct ptr_data *data;
402 VALUE arg1, val;
403 int len;
404
405 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
406 switch (rb_scan_args(argc, argv, "01", &arg1)) {
407 case 0:
408 val = rb_tainted_str_new((char*)(data->ptr),data->size);
409 break;
410 case 1:
411 len = NUM2INT(arg1);
412 val = rb_tainted_str_new((char*)(data->ptr), len);
413 break;
414 default:
415 rb_bug("rb_fiddle_ptr_to_str");
416 }
417
418 return val;
419 }
420
421 /*
422 * call-seq: inspect
423 *
424 * Returns a string formatted with an easily readable representation of the
425 * internal state of the pointer.
426 */
427 static VALUE
428 rb_fiddle_ptr_inspect(VALUE self)
429 {
430 struct ptr_data *data;
431
432 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
433 return rb_sprintf("#<%"PRIsVALUE":%p ptr=%p size=%ld free=%p>",
434 rb_obj_class(self), data, data->ptr, data->size, data->free);
435 }
436
437 /*
438 * call-seq:
439 * ptr == other => true or false
440 * ptr.eql?(other) => true or false
441 *
442 * Returns true if +other+ wraps the same pointer, otherwise returns
443 * false.
444 */
445 static VALUE
446 rb_fiddle_ptr_eql(VALUE self, VALUE other)
447 {
448 void *ptr1, *ptr2;
449
450 if(!rb_obj_is_kind_of(other, rb_cPointer)) return Qfalse;
451
452 ptr1 = rb_fiddle_ptr2cptr(self);
453 ptr2 = rb_fiddle_ptr2cptr(other);
454
455 return ptr1 == ptr2 ? Qtrue : Qfalse;
456 }
457
458 /*
459 * call-seq:
460 * ptr <=> other => -1, 0, 1, or nil
461 *
462 * Returns -1 if less than, 0 if equal to, 1 if greater than +other+.
463 *
464 * Returns nil if +ptr+ cannot be compared to +other+.
465 */
466 static VALUE
467 rb_fiddle_ptr_cmp(VALUE self, VALUE other)
468 {
469 void *ptr1, *ptr2;
470 SIGNED_VALUE diff;
471
472 if(!rb_obj_is_kind_of(other, rb_cPointer)) return Qnil;
473
474 ptr1 = rb_fiddle_ptr2cptr(self);
475 ptr2 = rb_fiddle_ptr2cptr(other);
476 diff = (SIGNED_VALUE)ptr1 - (SIGNED_VALUE)ptr2;
477 if (!diff) return INT2FIX(0);
478 return diff > 0 ? INT2NUM(1) : INT2NUM(-1);
479 }
480
481 /*
482 * call-seq:
483 * ptr + n => new cptr
484 *
485 * Returns a new pointer instance that has been advanced +n+ bytes.
486 */
487 static VALUE
488 rb_fiddle_ptr_plus(VALUE self, VALUE other)
489 {
490 void *ptr;
491 long num, size;
492
493 ptr = rb_fiddle_ptr2cptr(self);
494 size = RPTR_DATA(self)->size;
495 num = NUM2LONG(other);
496 return rb_fiddle_ptr_new((char *)ptr + num, size - num, 0);
497 }
498
499 /*
500 * call-seq:
501 * ptr - n => new cptr
502 *
503 * Returns a new pointer instance that has been moved back +n+ bytes.
504 */
505 static VALUE
506 rb_fiddle_ptr_minus(VALUE self, VALUE other)
507 {
508 void *ptr;
509 long num, size;
510
511 ptr = rb_fiddle_ptr2cptr(self);
512 size = RPTR_DATA(self)->size;
513 num = NUM2LONG(other);
514 return rb_fiddle_ptr_new((char *)ptr - num, size + num, 0);
515 }
516
517 /*
518 * call-seq:
519 * ptr[index] -> an_integer
520 * ptr[start, length] -> a_string
521 *
522 * Returns integer stored at _index_.
523 *
524 * If _start_ and _length_ are given, a string containing the bytes from
525 * _start_ of _length_ will be returned.
526 */
527 static VALUE
528 rb_fiddle_ptr_aref(int argc, VALUE argv[], VALUE self)
529 {
530 VALUE arg0, arg1;
531 VALUE retval = Qnil;
532 size_t offset, len;
533 struct ptr_data *data;
534
535 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
536 if (!data->ptr) rb_raise(rb_eFiddleError, "NULL pointer dereference");
537 switch( rb_scan_args(argc, argv, "11", &arg0, &arg1) ){
538 case 1:
539 offset = NUM2ULONG(arg0);
540 retval = INT2NUM(*((char *)data->ptr + offset));
541 break;
542 case 2:
543 offset = NUM2ULONG(arg0);
544 len = NUM2ULONG(arg1);
545 retval = rb_tainted_str_new((char *)data->ptr + offset, len);
546 break;
547 default:
548 rb_bug("rb_fiddle_ptr_aref()");
549 }
550 return retval;
551 }
552
553 /*
554 * call-seq:
555 * ptr[index] = int -> int
556 * ptr[start, length] = string or cptr or addr -> string or dl_cptr or addr
557 *
558 * Set the value at +index+ to +int+.
559 *
560 * Or, set the memory at +start+ until +length+ with the contents of +string+,
561 * the memory from +dl_cptr+, or the memory pointed at by the memory address
562 * +addr+.
563 */
564 static VALUE
565 rb_fiddle_ptr_aset(int argc, VALUE argv[], VALUE self)
566 {
567 VALUE arg0, arg1, arg2;
568 VALUE retval = Qnil;
569 size_t offset, len;
570 void *mem;
571 struct ptr_data *data;
572
573 TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
574 if (!data->ptr) rb_raise(rb_eFiddleError, "NULL pointer dereference");
575 switch( rb_scan_args(argc, argv, "21", &arg0, &arg1, &arg2) ){
576 case 2:
577 offset = NUM2ULONG(arg0);
578 ((char*)data->ptr)[offset] = NUM2UINT(arg1);
579 retval = arg1;
580 break;
581 case 3:
582 offset = NUM2ULONG(arg0);
583 len = NUM2ULONG(arg1);
584 if (RB_TYPE_P(arg2, T_STRING)) {
585 mem = StringValuePtr(arg2);
586 }
587 else if( rb_obj_is_kind_of(arg2, rb_cPointer) ){
588 mem = rb_fiddle_ptr2cptr(arg2);
589 }
590 else{
591 mem = NUM2PTR(arg2);
592 }
593 memcpy((char *)data->ptr + offset, mem, len);
594 retval = arg2;
595 break;
596 default:
597 rb_bug("rb_fiddle_ptr_aset()");
598 }
599 return retval;
600 }
601
602 /*
603 * call-seq: size=(size)
604 *
605 * Set the size of this pointer to +size+
606 */
607 static VALUE
608 rb_fiddle_ptr_size_set(VALUE self, VALUE size)
609 {
610 RPTR_DATA(self)->size = NUM2LONG(size);
611 return size;
612 }
613
614 /*
615 * call-seq: size
616 *
617 * Get the size of this pointer.
618 */
619 static VALUE
620 rb_fiddle_ptr_size_get(VALUE self)
621 {
622 return LONG2NUM(RPTR_DATA(self)->size);
623 }
624
625 /*
626 * call-seq:
627 * Fiddle::Pointer[val] => cptr
628 * to_ptr(val) => cptr
629 *
630 * Get the underlying pointer for ruby object +val+ and return it as a
631 * Fiddle::Pointer object.
632 */
633 static VALUE
634 rb_fiddle_ptr_s_to_ptr(VALUE self, VALUE val)
635 {
636 VALUE ptr, wrap = val, vptr;
637
638 if (RTEST(rb_obj_is_kind_of(val, rb_cIO))){
639 rb_io_t *fptr;
640 FILE *fp;
641 GetOpenFile(val, fptr);
642 fp = rb_io_stdio_file(fptr);
643 ptr = rb_fiddle_ptr_new(fp, 0, NULL);
644 }
645 else if (RTEST(rb_obj_is_kind_of(val, rb_cString))){
646 char *str = StringValuePtr(val);
647 ptr = rb_fiddle_ptr_new(str, RSTRING_LEN(val), NULL);
648 }
649 else if ((vptr = rb_check_funcall(val, id_to_ptr, 0, 0)) != Qundef){
650 if (rb_obj_is_kind_of(vptr, rb_cPointer)){
651 ptr = vptr;
652 wrap = 0;
653 }
654 else{
655 rb_raise(rb_eFiddleError, "to_ptr should return a Fiddle::Pointer object");
656 }
657 }
658 else{
659 VALUE num = rb_Integer(val);
660 if (num == val) wrap = 0;
661 ptr = rb_fiddle_ptr_new(NUM2PTR(num), 0, NULL);
662 }
663 OBJ_INFECT(ptr, val);
664 if (wrap) RPTR_DATA(ptr)->wrap[0] = wrap;
665 return ptr;
666 }
667
668 void
669 Init_fiddle_pointer(void)
670 {
671 id_to_ptr = rb_intern("to_ptr");
672
673 /* Document-class: Fiddle::Pointer
674 *
675 * Fiddle::Pointer is a class to handle C pointers
676 *
677 */
678 rb_cPointer = rb_define_class_under(mFiddle, "Pointer", rb_cObject);
679 rb_define_alloc_func(rb_cPointer, rb_fiddle_ptr_s_allocate);
680 rb_define_singleton_method(rb_cPointer, "malloc", rb_fiddle_ptr_s_malloc, -1);
681 rb_define_singleton_method(rb_cPointer, "to_ptr", rb_fiddle_ptr_s_to_ptr, 1);
682 rb_define_singleton_method(rb_cPointer, "[]", rb_fiddle_ptr_s_to_ptr, 1);
683 rb_define_method(rb_cPointer, "initialize", rb_fiddle_ptr_initialize, -1);
684 rb_define_method(rb_cPointer, "free=", rb_fiddle_ptr_free_set, 1);
685 rb_define_method(rb_cPointer, "free", rb_fiddle_ptr_free_get, 0);
686 rb_define_method(rb_cPointer, "to_i", rb_fiddle_ptr_to_i, 0);
687 rb_define_method(rb_cPointer, "to_int", rb_fiddle_ptr_to_i, 0);
688 rb_define_method(rb_cPointer, "to_value", rb_fiddle_ptr_to_value, 0);
689 rb_define_method(rb_cPointer, "ptr", rb_fiddle_ptr_ptr, 0);
690 rb_define_method(rb_cPointer, "+@", rb_fiddle_ptr_ptr, 0);
691 rb_define_method(rb_cPointer, "ref", rb_fiddle_ptr_ref, 0);
692 rb_define_method(rb_cPointer, "-@", rb_fiddle_ptr_ref, 0);
693 rb_define_method(rb_cPointer, "null?", rb_fiddle_ptr_null_p, 0);
694 rb_define_method(rb_cPointer, "to_s", rb_fiddle_ptr_to_s, -1);
695 rb_define_method(rb_cPointer, "to_str", rb_fiddle_ptr_to_str, -1);
696 rb_define_method(rb_cPointer, "inspect", rb_fiddle_ptr_inspect, 0);
697 rb_define_method(rb_cPointer, "<=>", rb_fiddle_ptr_cmp, 1);
698 rb_define_method(rb_cPointer, "==", rb_fiddle_ptr_eql, 1);
699 rb_define_method(rb_cPointer, "eql?", rb_fiddle_ptr_eql, 1);
700 rb_define_method(rb_cPointer, "+", rb_fiddle_ptr_plus, 1);
701 rb_define_method(rb_cPointer, "-", rb_fiddle_ptr_minus, 1);
702 rb_define_method(rb_cPointer, "[]", rb_fiddle_ptr_aref, -1);
703 rb_define_method(rb_cPointer, "[]=", rb_fiddle_ptr_aset, -1);
704 rb_define_method(rb_cPointer, "size", rb_fiddle_ptr_size_get, 0);
705 rb_define_method(rb_cPointer, "size=", rb_fiddle_ptr_size_set, 1);
706
707 /* Document-const: NULL
708 *
709 * A NULL pointer
710 */
711 rb_define_const(mFiddle, "NULL", rb_fiddle_ptr_new(0, 0, 0));
712 }
rb_cString
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1583
rb_fiddle_ptr_ref
static VALUE rb_fiddle_ptr_ref(VALUE self)
Definition: pointer.c:276
ptr_data::free
freefunc_t free
Definition: dl.h:203
TYPE_VOIDP
#define TYPE_VOIDP
Definition: fiddle.h:113
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:327
fiddle.h
mFiddle
VALUE mFiddle
Definition: fiddle.c:3
rb_fiddle_ptr_plus
static VALUE rb_fiddle_ptr_plus(VALUE self, VALUE other)
Definition: pointer.c:488
INT2NUM
#define INT2NUM(x)
Definition: ruby.h:1288
rb_fiddle_ptr_inspect
static VALUE rb_fiddle_ptr_inspect(VALUE self)
Definition: pointer.c:428
rb_fiddle_ptr_to_s
static VALUE rb_fiddle_ptr_to_s(int argc, VALUE argv[], VALUE self)
Definition: pointer.c:361
rb_fiddle_ptr_eql
static VALUE rb_fiddle_ptr_eql(VALUE self, VALUE other)
Definition: pointer.c:446
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:630
NUM2UINT
#define NUM2UINT(x)
Definition: ruby.h:631
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1655
ruby.h
Qtrue
#define Qtrue
Definition: ruby.h:426
rb_io_t
Definition: io.h:61
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
Definition: ruby.h:1041
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:548
rb_fiddle_ptr_ptr
static VALUE rb_fiddle_ptr_ptr(VALUE self)
Definition: pointer.c:259
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:896
rb_fiddle_ptr2cptr
static void * rb_fiddle_ptr2cptr(VALUE val)
Definition: pointer.c:106
NUM2PTR
#define NUM2PTR(x)
Definition: dl.h:169
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:676
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
rb_obj_is_kind_of
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:652
rb_fiddle_ptr_s_malloc
static VALUE rb_fiddle_ptr_s_malloc(int argc, VALUE argv[], VALUE klass)
Definition: pointer.c:198
ptr_data::wrap
VALUE wrap[2]
Definition: dl.h:204
ptr_data::ptr
void * ptr
Definition: dl.h:201
rb_gc_mark
void rb_gc_mark(VALUE ptr)
Definition: gc.c:3604
GetOpenFile
#define GetOpenFile(obj, fp)
Definition: io.h:118
io.h
rb_fiddle_ptr_cmp
static VALUE rb_fiddle_ptr_cmp(VALUE self, VALUE other)
Definition: pointer.c:467
sym
#define sym(x)
Definition: date_core.c:3695
__sFILE
Definition: vsnprintf.c:175
rb_eFiddleError
VALUE rb_eFiddleError
Definition: fiddle.c:4
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1664
val
#define val
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1553
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:495
NIL_P
#define NIL_P(v)
Definition: ruby.h:438
rb_fiddle_ptr_aref
static VALUE rb_fiddle_ptr_aref(int argc, VALUE argv[], VALUE self)
Definition: pointer.c:528
rb_fiddle_ptr_aset
static VALUE rb_fiddle_ptr_aset(int argc, VALUE argv[], VALUE self)
Definition: pointer.c:565
PTR2NUM
#define PTR2NUM(x)
Definition: dl.h:168
rb_define_const
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2225
rb_fiddle_ptr_new2
static VALUE rb_fiddle_ptr_new2(VALUE klass, void *ptr, long size, freefunc_t func)
Definition: pointer.c:75
argc
int argc
Definition: ruby.c:131
Qfalse
#define Qfalse
Definition: ruby.h:425
rb_Integer
VALUE rb_Integer(VALUE)
Definition: object.c:2736
fiddle_ptr_mark
static void fiddle_ptr_mark(void *ptr)
Definition: pointer.c:39
rb_cIO
RUBY_EXTERN VALUE rb_cIO
Definition: ruby.h:1569
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:841
func
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:113
rb_sprintf
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1250
get_freefunc
static freefunc_t get_freefunc(VALUE func, volatile VALUE *wrap)
Definition: pointer.c:24
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1728
PRIsVALUE
#define PRIsVALUE
Definition: ruby.h:137
ptr_data
Definition: dl.h:200
ID
unsigned long ID
Definition: ruby.h:89
Qnil
#define Qnil
Definition: ruby.h:427
OBJ_TAINT
#define OBJ_TAINT(x)
Definition: ruby.h:1177
VALUE
unsigned long VALUE
Definition: ruby.h:88
rb_check_funcall
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:409
rb_tainted_str_new2
#define rb_tainted_str_new2
Definition: intern.h:844
rb_fiddle_ptr_size_set
static VALUE rb_fiddle_ptr_size_set(VALUE self, VALUE size)
Definition: pointer.c:608
freefunc_t
void(* freefunc_t)(void *)
Definition: pointer.c:12
rb_fiddle_ptr_free_get
static VALUE rb_fiddle_ptr_free_get(VALUE self)
Definition: pointer.c:325
LONG2NUM
#define LONG2NUM(x)
Definition: ruby.h:1309
ruby_xmalloc
void * ruby_xmalloc(size_t size)
Definition: gc.c:6156
size
int size
Definition: encoding.c:49
f
#define f
INT2FIX
#define INT2FIX(i)
Definition: ruby.h:231
rb_fiddle_ptr_size_get
static VALUE rb_fiddle_ptr_size_get(VALUE self)
Definition: pointer.c:620
rb_fiddle_new_function
VALUE rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type)
Definition: function.c:42
rb_fiddle_ptr_malloc
static VALUE rb_fiddle_ptr_malloc(long size, freefunc_t func)
Definition: pointer.c:96
TYPE_VOID
#define TYPE_VOID
Definition: fiddle.h:112
NUM2ULONG
#define NUM2ULONG(x)
Definition: ruby.h:609
rb_data_type_struct
Definition: ruby.h:966
rb_fiddle_ptr_null_p
static VALUE rb_fiddle_ptr_null_p(VALUE self)
Definition: pointer.c:290
rb_fiddle_ptr_to_i
static VALUE rb_fiddle_ptr_to_i(VALUE self)
Definition: pointer.c:229
RTEST
#define RTEST(v)
Definition: ruby.h:437
T_STRING
#define T_STRING
Definition: ruby.h:482
rb_fiddle_ptr_free_set
static VALUE rb_fiddle_ptr_free_set(VALUE self, VALUE val)
Definition: pointer.c:305
OBJ_INFECT
#define OBJ_INFECT(x, s)
Definition: ruby.h:1180
diff
static Bigint * diff(Bigint *a, Bigint *b)
Definition: util.c:1466
id_to_ptr
static ID id_to_ptr
Definition: pointer.c:36
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1030
rb_fiddle_ptr_to_value
static VALUE rb_fiddle_ptr_to_value(VALUE self)
Definition: pointer.c:243
rb_fiddle_ptr_new
static VALUE rb_fiddle_ptr_new(void *ptr, long size, freefunc_t func)
Definition: pointer.c:90
rb_fiddle_ptr_s_to_ptr
static VALUE rb_fiddle_ptr_s_to_ptr(VALUE self, VALUE val)
Definition: pointer.c:634
rb_io_stdio_file
FILE * rb_io_stdio_file(rb_io_t *fptr)
Definition: io.c:7247
fiddle_ptr_data_type
static const rb_data_type_t fiddle_ptr_data_type
Definition: pointer.c:69
rb_cPointer
VALUE rb_cPointer
Definition: pointer.c:10
StringValuePtr
#define StringValuePtr(v)
Definition: ruby.h:540
rb_fiddle_ptr_initialize
static VALUE rb_fiddle_ptr_initialize(int argc, VALUE argv[], VALUE self)
Definition: pointer.c:150
RPTR_DATA
#define RPTR_DATA(obj)
Definition: pointer.c:21
rb_fiddle_ptr_s_allocate
static VALUE rb_fiddle_ptr_s_allocate(VALUE klass)
Definition: pointer.c:126
Init_fiddle_pointer
void Init_fiddle_pointer(void)
Definition: pointer.c:669
xfree
void void xfree(void *)
rb_fiddle_ptr_to_str
static VALUE rb_fiddle_ptr_to_str(int argc, VALUE argv[], VALUE self)
Definition: pointer.c:399
rb_tainted_str_new
VALUE rb_tainted_str_new(const char *, long)
Definition: string.c:589
rb_intern
#define rb_intern(str)
NULL
#define NULL
Definition: _sdbm.c:103
Qundef
#define Qundef
Definition: ruby.h:428
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1488
fiddle_ptr_memsize
static size_t fiddle_ptr_memsize(const void *ptr)
Definition: pointer.c:63
ptr_data::size
long size
Definition: dl.h:202
NUM2LONG
#define NUM2LONG(x)
Definition: ruby.h:600
argv
char ** argv
Definition: ruby.c:132
fiddle_ptr_free
static void fiddle_ptr_free(void *ptr)
Definition: pointer.c:51
rb_obj_class
VALUE rb_obj_class(VALUE)
Definition: object.c:226
SIGNED_VALUE
#define SIGNED_VALUE
Definition: ruby.h:90
rb_fiddle_ptr_minus
static VALUE rb_fiddle_ptr_minus(VALUE self, VALUE other)
Definition: pointer.c:506