Class: Fiddle::Pointer
- Inherits:
-
Object
- Object
- Fiddle::Pointer
- Defined in:
- pointer.c,
pointer.c
Overview
Fiddle::Pointer is a class to handle C pointers
Direct Known Subclasses
Class Method Summary collapse
-
.[](val) ⇒ Object
Get the underlying pointer for ruby object
val
and return it as a Fiddle::Pointer object. -
.malloc(argv[], klass) ⇒ Object
Examples.
-
.Fiddle::Pointer.read(address, len) ⇒ String
Or read the memory at address
address
with lengthlen
and return a string with that memory. -
.to_ptr(val) ⇒ Object
Get the underlying pointer for ruby object
val
and return it as a Fiddle::Pointer object. -
.Fiddle::Pointer.write(address, str) ⇒ Object
Write bytes in
str
to the location pointed to byaddress
.
Instance Method Summary collapse
-
#+(n) ⇒ Object
Returns a new pointer instance that has been advanced
n
bytes. -
#ptr ⇒ Object
Returns a new Fiddle::Pointer instance that is a dereferenced pointer for this pointer.
-
#-(n) ⇒ Object
Returns a new pointer instance that has been moved back
n
bytes. -
#ref ⇒ Object
Returns a new Fiddle::Pointer instance that is a reference pointer for this pointer.
-
#<=>(other) ⇒ -1, ...
Returns -1 if less than, 0 if equal to, 1 if greater than
other
. -
#==(other) ⇒ Object
Returns true if
other
wraps the same pointer, otherwise returns false. -
#[](argv[], self) ⇒ Object
Returns integer stored at index.
-
#[]=(argv[], self) ⇒ Object
Set the value at
index
toint
. -
#call_free ⇒ nil
Call the free function for this pointer.
-
#eql?(other) ⇒ Object
Returns true if
other
wraps the same pointer, otherwise returns false. -
#free ⇒ Fiddle::Function
Get the free function for this pointer.
-
#free=(function) ⇒ Object
Set the free function for this pointer to
function
in the given Fiddle::Function. -
#freed? ⇒ Boolean
Returns if the free function for this pointer has been called.
-
#initialize(argv[], self) ⇒ Object
constructor
Create a new pointer to
address
with an optionalsize
andfreefunc
. -
#inspect ⇒ Object
Returns a string formatted with an easily readable representation of the internal state of the pointer.
-
#null? ⇒ Boolean
Returns
true
if this is a null pointer. -
#ptr ⇒ Object
Returns a new Fiddle::Pointer instance that is a dereferenced pointer for this pointer.
-
#ref ⇒ Object
Returns a new Fiddle::Pointer instance that is a reference pointer for this pointer.
-
#size ⇒ Object
Get the size of this pointer.
-
#size=(size) ⇒ Object
Set the size of this pointer to
size
. -
#to_i ⇒ Object
Returns the integer memory location of this pointer.
-
#to_i ⇒ Object
Returns the integer memory location of this pointer.
-
#to_s(argv[], self) ⇒ Object
Returns the pointer contents as a string.
-
#to_str(argv[], self) ⇒ Object
Returns the pointer contents as a string.
-
#to_value ⇒ Object
Cast this pointer to a ruby object.
Constructor Details
#Fiddle::Pointer.new(address) ⇒ Object #new(address, size) ⇒ Object #new(address, size, freefunc) ⇒ Object
Create a new pointer to address
with an optional size
and freefunc
.
freefunc
will be called when the instance is garbage collected.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'pointer.c', line 216
static VALUE
rb_fiddle_ptr_initialize(int argc, VALUE argv[], VALUE self)
{
VALUE ptr, sym, size, wrap = 0, funcwrap = 0;
struct ptr_data *data;
void *p = NULL;
freefunc_t f = NULL;
long s = 0;
if (rb_scan_args(argc, argv, "12", &ptr, &size, &sym) >= 1) {
VALUE addrnum = rb_Integer(ptr);
if (addrnum != ptr) wrap = ptr;
p = NUM2PTR(addrnum);
}
if (argc >= 2) {
s = NUM2LONG(size);
}
if (argc >= 3) {
f = get_freefunc(sym, &funcwrap);
}
if (p) {
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
if (data->ptr && data->free) {
/* Free previous memory. Use of inappropriate initialize may cause SEGV. */
(*(data->free))(data->ptr);
}
RB_OBJ_WRITE(self, &data->wrap[0], wrap);
RB_OBJ_WRITE(self, &data->wrap[1], funcwrap);
data->ptr = p;
data->size = s;
data->free = f;
}
return Qnil;
}
|
Class Method Details
.Fiddle::Pointer ⇒ Object .to_ptr(val) ⇒ Object
Get the underlying pointer for ruby object val
and return it as a Fiddle::Pointer object.
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 |
# File 'pointer.c', line 772
static VALUE
rb_fiddle_ptr_s_to_ptr(VALUE self, VALUE val)
{
VALUE ptr, wrap = val, vptr;
if (RTEST(rb_obj_is_kind_of(val, rb_cIO))){
rb_io_t *fptr;
FILE *fp;
GetOpenFile(val, fptr);
fp = rb_io_stdio_file(fptr);
ptr = rb_fiddle_ptr_new(fp, 0, NULL);
}
else if (RTEST(rb_obj_is_kind_of(val, rb_cString))){
char *str = StringValuePtr(val);
wrap = val;
ptr = rb_fiddle_ptr_new(str, RSTRING_LEN(val), NULL);
}
else if ((vptr = rb_check_funcall(val, id_to_ptr, 0, 0)) != Qundef){
if (rb_obj_is_kind_of(vptr, rb_cPointer)){
ptr = vptr;
wrap = 0;
}
else{
rb_raise(rb_eFiddleDLError, "to_ptr should return a Fiddle::Pointer object");
}
}
else{
VALUE num = rb_Integer(val);
if (num == val) wrap = 0;
ptr = rb_fiddle_ptr_new(NUM2PTR(num), 0, NULL);
}
if (wrap) RB_OBJ_WRITE(ptr, &RPTR_DATA(ptr)->wrap[0], wrap);
return ptr;
}
|
.Fiddle::Pointer.malloc(size, freefunc = nil) ⇒ Object .Fiddle::Pointer.malloc(size, freefunc) {|pointer| ... } ⇒ Object
Examples
# Automatically freeing the pointer when the block is exited - recommended
Fiddle::Pointer.malloc(size, Fiddle::RUBY_FREE) do |pointer|
...
end
# Manually freeing but relying on the garbage collector otherwise
pointer = Fiddle::Pointer.malloc(size, Fiddle::RUBY_FREE)
...
pointer.call_free
# Relying on the garbage collector - may lead to unlimited memory allocated before freeing any, but safe
pointer = Fiddle::Pointer.malloc(size, Fiddle::RUBY_FREE)
...
# Only manually freeing
pointer = Fiddle::Pointer.malloc(size)
begin
...
ensure
Fiddle.free pointer
end
# No free function and no call to free - the native memory will leak if the pointer is garbage collected
pointer = Fiddle::Pointer.malloc(size)
...
Allocate size
bytes of memory and associate it with an optional freefunc
.
If a block is supplied, the pointer will be yielded to the block instead of being returned, and the return value of the block will be returned. A freefunc
must be supplied if a block is.
If a freefunc
is supplied it will be called once, when the pointer is garbage collected or when the block is left if a block is supplied or when the user calls call_free
, whichever happens first. freefunc
must be an address pointing to a function or an instance of Fiddle::Function
.
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'pointer.c', line 301
static VALUE
rb_fiddle_ptr_s_malloc(int argc, VALUE argv[], VALUE klass)
{
VALUE size, sym, obj, wrap = 0;
long s;
freefunc_t f;
switch (rb_scan_args(argc, argv, "11", &size, &sym)) {
case 1:
s = NUM2LONG(size);
f = NULL;
break;
case 2:
s = NUM2LONG(size);
f = get_freefunc(sym, &wrap);
break;
default:
rb_bug("rb_fiddle_ptr_s_malloc");
}
obj = rb_fiddle_ptr_malloc(klass, s,f);
if (wrap) RB_OBJ_WRITE(obj, &RPTR_DATA(obj)->wrap[1], wrap);
if (rb_block_given_p()) {
if (!f) {
rb_raise(rb_eArgError, "a free function must be supplied to Fiddle::Pointer.malloc when it is called with a block");
}
return rb_ensure(rb_yield, obj, rb_fiddle_ptr_call_free, obj);
} else {
return obj;
}
}
|
.Fiddle::Pointer.read(address, len) ⇒ String
Or read the memory at address address
with length len
and return a string with that memory
815 816 817 818 819 |
# File 'pointer.c', line 815
static VALUE
rb_fiddle_ptr_read_mem(VALUE klass, VALUE address, VALUE len)
{
return rb_str_new((char *)NUM2PTR(address), NUM2ULONG(len));
}
|
.Fiddle::Pointer ⇒ Object .to_ptr(val) ⇒ Object
Get the underlying pointer for ruby object val
and return it as a Fiddle::Pointer object.
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 |
# File 'pointer.c', line 772
static VALUE
rb_fiddle_ptr_s_to_ptr(VALUE self, VALUE val)
{
VALUE ptr, wrap = val, vptr;
if (RTEST(rb_obj_is_kind_of(val, rb_cIO))){
rb_io_t *fptr;
FILE *fp;
GetOpenFile(val, fptr);
fp = rb_io_stdio_file(fptr);
ptr = rb_fiddle_ptr_new(fp, 0, NULL);
}
else if (RTEST(rb_obj_is_kind_of(val, rb_cString))){
char *str = StringValuePtr(val);
wrap = val;
ptr = rb_fiddle_ptr_new(str, RSTRING_LEN(val), NULL);
}
else if ((vptr = rb_check_funcall(val, id_to_ptr, 0, 0)) != Qundef){
if (rb_obj_is_kind_of(vptr, rb_cPointer)){
ptr = vptr;
wrap = 0;
}
else{
rb_raise(rb_eFiddleDLError, "to_ptr should return a Fiddle::Pointer object");
}
}
else{
VALUE num = rb_Integer(val);
if (num == val) wrap = 0;
ptr = rb_fiddle_ptr_new(NUM2PTR(num), 0, NULL);
}
if (wrap) RB_OBJ_WRITE(ptr, &RPTR_DATA(ptr)->wrap[0], wrap);
return ptr;
}
|
.Fiddle::Pointer.write(address, str) ⇒ Object
Write bytes in str
to the location pointed to by address
.
827 828 829 830 831 832 |
# File 'pointer.c', line 827
static VALUE
rb_fiddle_ptr_write_mem(VALUE klass, VALUE addr, VALUE str)
{
memcpy(NUM2PTR(addr), StringValuePtr(str), RSTRING_LEN(str));
return str;
}
|
Instance Method Details
#+(n) ⇒ Object
Returns a new pointer instance that has been advanced n
bytes.
626 627 628 629 630 631 632 633 634 635 636 |
# File 'pointer.c', line 626
static VALUE
rb_fiddle_ptr_plus(VALUE self, VALUE other)
{
void *ptr;
long num, size;
ptr = rb_fiddle_ptr2cptr(self);
size = RPTR_DATA(self)->size;
num = NUM2LONG(other);
return rb_fiddle_ptr_new((char *)ptr + num, size - num, 0);
}
|
#ptr ⇒ Object
Returns a new Fiddle::Pointer instance that is a dereferenced pointer for this pointer.
Analogous to the star operator in C.
369 370 371 372 373 374 375 376 |
# File 'pointer.c', line 369
static VALUE
rb_fiddle_ptr_ptr(VALUE self)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
return rb_fiddle_ptr_new(*((void**)(data->ptr)),0,0);
}
|
#-(n) ⇒ Object
Returns a new pointer instance that has been moved back n
bytes.
644 645 646 647 648 649 650 651 652 653 654 |
# File 'pointer.c', line 644
static VALUE
rb_fiddle_ptr_minus(VALUE self, VALUE other)
{
void *ptr;
long num, size;
ptr = rb_fiddle_ptr2cptr(self);
size = RPTR_DATA(self)->size;
num = NUM2LONG(other);
return rb_fiddle_ptr_new((char *)ptr - num, size + num, 0);
}
|
#ref ⇒ Object
Returns a new Fiddle::Pointer instance that is a reference pointer for this pointer.
Analogous to the ampersand operator in C.
386 387 388 389 390 391 392 393 |
# File 'pointer.c', line 386
static VALUE
rb_fiddle_ptr_ref(VALUE self)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
return rb_fiddle_ptr_new(&(data->ptr),0,0);
}
|
#<=>(other) ⇒ -1, ...
Returns -1 if less than, 0 if equal to, 1 if greater than other
.
Returns nil if ptr
cannot be compared to other
.
605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
# File 'pointer.c', line 605
static VALUE
rb_fiddle_ptr_cmp(VALUE self, VALUE other)
{
void *ptr1, *ptr2;
SIGNED_VALUE diff;
if(!rb_obj_is_kind_of(other, rb_cPointer)) return Qnil;
ptr1 = rb_fiddle_ptr2cptr(self);
ptr2 = rb_fiddle_ptr2cptr(other);
diff = (SIGNED_VALUE)ptr1 - (SIGNED_VALUE)ptr2;
if (!diff) return INT2FIX(0);
return diff > 0 ? INT2NUM(1) : INT2NUM(-1);
}
|
#==(other) ⇒ Boolean #eql?(other) ⇒ Boolean
Returns true if other
wraps the same pointer, otherwise returns false.
584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'pointer.c', line 584
static VALUE
rb_fiddle_ptr_eql(VALUE self, VALUE other)
{
void *ptr1, *ptr2;
if(!rb_obj_is_kind_of(other, rb_cPointer)) return Qfalse;
ptr1 = rb_fiddle_ptr2cptr(self);
ptr2 = rb_fiddle_ptr2cptr(other);
return ptr1 == ptr2 ? Qtrue : Qfalse;
}
|
#[](index) ⇒ Integer #[](start, length) ⇒ String
Returns integer stored at index.
If start and length are given, a string containing the bytes from start of length will be returned.
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
# File 'pointer.c', line 666
static VALUE
rb_fiddle_ptr_aref(int argc, VALUE argv[], VALUE self)
{
VALUE arg0, arg1;
VALUE retval = Qnil;
size_t offset, len;
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
if (!data->ptr) rb_raise(rb_eFiddleDLError, "NULL pointer dereference");
switch( rb_scan_args(argc, argv, "11", &arg0, &arg1) ){
case 1:
offset = NUM2ULONG(arg0);
retval = INT2NUM(*((char *)data->ptr + offset));
break;
case 2:
offset = NUM2ULONG(arg0);
len = NUM2ULONG(arg1);
retval = rb_str_new((char *)data->ptr + offset, len);
break;
default:
rb_bug("rb_fiddle_ptr_aref()");
}
return retval;
}
|
#[]=(index) ⇒ Integer #[]=(start, length) ⇒ String
Set the value at index
to int
.
Or, set the memory at start
until length
with the contents of string
, the memory from dl_cptr
, or the memory pointed at by the memory address addr
.
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 |
# File 'pointer.c', line 703
static VALUE
rb_fiddle_ptr_aset(int argc, VALUE argv[], VALUE self)
{
VALUE arg0, arg1, arg2;
VALUE retval = Qnil;
size_t offset, len;
void *mem;
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
if (!data->ptr) rb_raise(rb_eFiddleDLError, "NULL pointer dereference");
switch( rb_scan_args(argc, argv, "21", &arg0, &arg1, &arg2) ){
case 2:
offset = NUM2ULONG(arg0);
((char*)data->ptr)[offset] = NUM2UINT(arg1);
retval = arg1;
break;
case 3:
offset = NUM2ULONG(arg0);
len = NUM2ULONG(arg1);
if (RB_TYPE_P(arg2, T_STRING)) {
mem = StringValuePtr(arg2);
}
else if( rb_obj_is_kind_of(arg2, rb_cPointer) ){
mem = rb_fiddle_ptr2cptr(arg2);
}
else{
mem = NUM2PTR(arg2);
}
memcpy((char *)data->ptr + offset, mem, len);
retval = arg2;
break;
default:
rb_bug("rb_fiddle_ptr_aset()");
}
return retval;
}
|
#call_free ⇒ nil
Call the free function for this pointer. Calling more than once will do nothing. Does nothing if there is no free function attached.
462 463 464 465 466 467 468 469 |
# File 'pointer.c', line 462
static VALUE
rb_fiddle_ptr_call_free(VALUE self)
{
struct ptr_data *pdata;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, pdata);
fiddle_ptr_free_ptr(pdata);
return Qnil;
}
|
#==(other) ⇒ Boolean #eql?(other) ⇒ Boolean
Returns true if other
wraps the same pointer, otherwise returns false.
584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'pointer.c', line 584
static VALUE
rb_fiddle_ptr_eql(VALUE self, VALUE other)
{
void *ptr1, *ptr2;
if(!rb_obj_is_kind_of(other, rb_cPointer)) return Qfalse;
ptr1 = rb_fiddle_ptr2cptr(self);
ptr2 = rb_fiddle_ptr2cptr(other);
return ptr1 == ptr2 ? Qtrue : Qfalse;
}
|
#free ⇒ Fiddle::Function
Get the free function for this pointer.
Returns a new instance of Fiddle::Function.
See Fiddle::Function.new
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'pointer.c', line 435
static VALUE
rb_fiddle_ptr_free_get(VALUE self)
{
struct ptr_data *pdata;
VALUE address;
VALUE arg_types;
VALUE ret_type;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, pdata);
if (!pdata->free)
return Qnil;
address = PTR2NUM(pdata->free);
ret_type = INT2NUM(TYPE_VOID);
arg_types = rb_ary_new();
rb_ary_push(arg_types, INT2NUM(TYPE_VOIDP));
return rb_fiddle_new_function(address, arg_types, ret_type);
}
|
#free=(function) ⇒ Object
Set the free function for this pointer to function
in the given Fiddle::Function.
415 416 417 418 419 420 421 422 423 424 |
# File 'pointer.c', line 415
static VALUE
rb_fiddle_ptr_free_set(VALUE self, VALUE val)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
data->free = get_freefunc(val, &data->wrap[1]);
return Qnil;
}
|
#freed? ⇒ Boolean
Returns if the free function for this pointer has been called.
476 477 478 479 480 481 482 |
# File 'pointer.c', line 476
static VALUE
rb_fiddle_ptr_freed_p(VALUE self)
{
struct ptr_data *pdata;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, pdata);
return pdata->freed ? Qtrue : Qfalse;
}
|
#inspect ⇒ Object
Returns a string formatted with an easily readable representation of the internal state of the pointer.
566 567 568 569 570 571 572 573 574 |
# File 'pointer.c', line 566
static VALUE
rb_fiddle_ptr_inspect(VALUE self)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
return rb_sprintf("#<%"PRIsVALUE":%p ptr=%p size=%ld free=%p>",
RB_OBJ_CLASSNAME(self), (void *)data, data->ptr, data->size, (void *)data->free);
}
|
#null? ⇒ Boolean
Returns true
if this is a null pointer.
400 401 402 403 404 405 406 407 |
# File 'pointer.c', line 400
static VALUE
rb_fiddle_ptr_null_p(VALUE self)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
return data->ptr ? Qfalse : Qtrue;
}
|
#ptr ⇒ Object
Returns a new Fiddle::Pointer instance that is a dereferenced pointer for this pointer.
Analogous to the star operator in C.
369 370 371 372 373 374 375 376 |
# File 'pointer.c', line 369
static VALUE
rb_fiddle_ptr_ptr(VALUE self)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
return rb_fiddle_ptr_new(*((void**)(data->ptr)),0,0);
}
|
#ref ⇒ Object
Returns a new Fiddle::Pointer instance that is a reference pointer for this pointer.
Analogous to the ampersand operator in C.
386 387 388 389 390 391 392 393 |
# File 'pointer.c', line 386
static VALUE
rb_fiddle_ptr_ref(VALUE self)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
return rb_fiddle_ptr_new(&(data->ptr),0,0);
}
|
#size ⇒ Object
Get the size of this pointer.
758 759 760 761 762 |
# File 'pointer.c', line 758
static VALUE
rb_fiddle_ptr_size_get(VALUE self)
{
return LONG2NUM(RPTR_DATA(self)->size);
}
|
#size=(size) ⇒ Object
Set the size of this pointer to size
746 747 748 749 750 751 |
# File 'pointer.c', line 746
static VALUE
rb_fiddle_ptr_size_set(VALUE self, VALUE size)
{
RPTR_DATA(self)->size = NUM2LONG(size);
return size;
}
|
#to_i ⇒ Object
Returns the integer memory location of this pointer.
339 340 341 342 343 344 345 346 |
# File 'pointer.c', line 339
static VALUE
rb_fiddle_ptr_to_i(VALUE self)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
return PTR2NUM(data->ptr);
}
|
#to_i ⇒ Object
Returns the integer memory location of this pointer.
339 340 341 342 343 344 345 346 |
# File 'pointer.c', line 339
static VALUE
rb_fiddle_ptr_to_i(VALUE self)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
return PTR2NUM(data->ptr);
}
|
#to_s ⇒ String #to_s(len) ⇒ String
Returns the pointer contents as a string.
When called with no arguments, this method will return the contents until the first NULL byte.
When called with len
, a string of len
bytes will be returned.
See to_str
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'pointer.c', line 499
static VALUE
rb_fiddle_ptr_to_s(int argc, VALUE argv[], VALUE self)
{
struct ptr_data *data;
VALUE arg1, val;
int len;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
switch (rb_scan_args(argc, argv, "01", &arg1)) {
case 0:
val = rb_str_new2((char*)(data->ptr));
break;
case 1:
len = NUM2INT(arg1);
val = rb_str_new((char*)(data->ptr), len);
break;
default:
rb_bug("rb_fiddle_ptr_to_s");
}
return val;
}
|
#to_str ⇒ String #to_str(len) ⇒ String
Returns the pointer contents as a string.
When called with no arguments, this method will return the contents with the length of this pointer’s size
.
When called with len
, a string of len
bytes will be returned.
See to_s
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
# File 'pointer.c', line 537
static VALUE
rb_fiddle_ptr_to_str(int argc, VALUE argv[], VALUE self)
{
struct ptr_data *data;
VALUE arg1, val;
int len;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
switch (rb_scan_args(argc, argv, "01", &arg1)) {
case 0:
val = rb_str_new((char*)(data->ptr),data->size);
break;
case 1:
len = NUM2INT(arg1);
val = rb_str_new((char*)(data->ptr), len);
break;
default:
rb_bug("rb_fiddle_ptr_to_str");
}
return val;
}
|
#to_value ⇒ Object
Cast this pointer to a ruby object.
353 354 355 356 357 358 359 |
# File 'pointer.c', line 353
static VALUE
rb_fiddle_ptr_to_value(VALUE self)
{
struct ptr_data *data;
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
return (VALUE)(data->ptr);
}
|