Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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