Class: FFI::AbstractMemory
- Inherits:
-
Object
- Object
- FFI::AbstractMemory
- Defined in:
- ext/ffi_c/AbstractMemory.c,
ext/ffi_c/AbstractMemory.c
Overview
AbstractMemory is the base class for many memory management classes such as Buffer.
This class has a lot of methods to work with integers :
-
put_intsize(offset, value)
-
get_intsize(offset)
-
put_uintsize(offset, value)
-
get_uintsize(offset)
-
writeuintsize(value)
-
read_intsize
-
write_uintsize(value)
-
read_uintsize
-
put_array_of_intsize(offset, ary)
-
get_array_of_intsize(offset, length)
-
put_array_of_uintsize(offset, ary)
-
get_array_of_uintsize(offset, length)
-
write_array_of_intsize(ary)
-
read_array_of_intsize(length)
-
write_array_of_uintsize(ary)
-
read_array_of_uintsize(length)
where size is 8, 16, 32 or 64. Same methods exist for long type.
Aliases exist : char for int8, short for int16, int for int32 and long_long for int64.
Others methods are listed below.
Instance Method Summary (collapse)
-
- (Object) [](idx)
Memory read accessor.
- - (Object) __copy_from__
-
- (self) clear
Set the memory to all-zero.
-
- (Array<Float>) get_array_of_float32(offset, length)
(also: #get_array_of_float)
Get 32-bit floats in memory from offset offset (alias: #get_array_of_float).
-
- (Array<Float>) get_array_of_float64(offset, length)
(also: #get_array_of_double)
Get 64-bit floats (doubles) in memory from offset offset (alias: #get_array_of_double).
-
- (Array<Pointer>) get_array_of_pointer(offset, length)
Get an array of Pointer of length length from offset.
-
- (Array<String>) get_array_of_string(offset, count = nil)
Return an array of strings contained in memory.
-
- (String) get_bytes(offset, length)
Return string contained in memory.
-
- (Float) get_float32(offset)
(also: #get_float)
Get a 32-bit float from memory at offset offset (alias: #get_float).
-
- (Float) get_float64(offset)
(also: #get_double)
Get a 64-bit float (double) from memory at offset offset (alias: #get_double).
-
- (Pointer) get_pointer(offset)
Get a Pointer to the memory from offset.
-
- (String) get_string(offset, length = nil)
Return string contained in memory.
-
- (self) put_array_of_float32(offset, ary)
(also: #put_array_of_float)
Put values from ary as 32-bit floats in memory from offset offset (alias: #put_array_of_float).
-
- (self) put_array_of_float64(offset, ary)
(also: #put_array_of_double)
Put values from ary as 64-bit floats (doubles) in memory from offset offset (alias: #put_array_of_double).
-
- (self) put_array_of_pointer(offset, ary)
Put an array of Pointer into memory from offset.
-
- (self) put_bytes(offset, str, index = 0, length = nil)
Put a string in memory.
-
- (self) put_float32offset
(also: #put_float)
Put value as a 32-bit float in memory at offset offset (alias: #put_float).
-
- (self) put_float64(offset, value)
(also: #put_double)
Put value as a 64-bit float (double) in memory at offset offset (alias: #put_double).
-
- (self) put_pointer(offset, value)
Put value in memory from offset..
-
- (self) put_string(offset, str)
Put a string in memory.
-
- (Array<Float>) read_array_of_double(length)
Read 64-bit floats (doubles) from memory.
-
- (Array<Float>) read_array_of_float(length)
Read 32-bit floats from memory.
-
- (Array<Pointer>) read_array_of_pointer(length)
Read an array of Pointer of length length.
-
- (String) read_bytes(length)
equivalent to :.
-
- (Float) read_double
Read a 64-bit float (double) from memory.
-
- (Float) read_float
Read a 32-bit float from memory.
-
- (Pointer) read_pointer
Get a Pointer to the memory from base address.
-
- (Numeric) size
(also: #size)
Return memory size in bytes (alias: #total).
-
- (Numeric) type_size
Get the memory's type size.
-
- (self) write_array_of_double(ary)
Write values from ary as 64-bit floats (doubles) in memory.
-
- (self) write_array_of_float(ary)
Write values from ary as 32-bit floats in memory.
-
- (self) write_array_of_pointer(ary)
Write an array of Pointer into memory from offset.
-
- (self) write_bytes(str, index = 0, length = nil)
equivalent to :.
-
- (self) write_double(value)
Write value as a 64-bit float (double) in memory.
-
- (self) write_float(value)
Write value as a 32-bit float in memory.
-
- (self) write_pointer(value)
Write value in memory.
Instance Method Details
- (Object) [](idx)
Memory read accessor.
575 576 577 578 579 580 581 582 583 584 585 586 |
# File 'ext/ffi_c/AbstractMemory.c', line 575
static VALUE
memory_aref(VALUE self, VALUE idx)
{
AbstractMemory* ptr;
VALUE rbOffset = Qnil;
Data_Get_Struct(self, AbstractMemory, ptr);
rbOffset = ULONG2NUM(NUM2ULONG(idx) * ptr->typeSize);
return rb_funcall2(self, id_plus, 1, &rbOffset);
}
|
- (Object) __copy_from__
594 595 596 597 598 599 600 601 602 603 604 |
# File 'ext/ffi_c/AbstractMemory.c', line 594
static VALUE
memory_copy_from(VALUE self, VALUE rbsrc, VALUE rblen)
{
AbstractMemory* dst;
Data_Get_Struct(self, AbstractMemory, dst);
memcpy(dst->address, rbffi_AbstractMemory_Cast(rbsrc, rbffi_AbstractMemoryClass)->address, NUM2INT(rblen));
return self;
}
|
- (self) clear
Set the memory to all-zero.
290 291 292 293 294 295 296 |
# File 'ext/ffi_c/AbstractMemory.c', line 290
static VALUE
memory_clear(VALUE self)
{
AbstractMemory* ptr = MEMORY(self);
memset(ptr->address, 0, ptr->size);
return self;
}
|
- (Array<Float>) get_array_of_float32(offset, length) Also known as: get_array_of_float
Get 32-bit floats in memory from offset offset (alias: #get_array_of_float).
- (Array<Float>) get_array_of_float64(offset, length) Also known as: get_array_of_double
Get 64-bit floats (doubles) in memory from offset offset (alias: #get_array_of_double).
- (Array<Pointer>) get_array_of_pointer(offset, length)
Get an array of Pointer of length length from offset.
- (Array<String>) get_array_of_string(offset, count = nil)
Return an array of strings contained in memory.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'ext/ffi_c/AbstractMemory.c', line 350
static VALUE
memory_get_array_of_string(int argc, VALUE* argv, VALUE self)
{
VALUE offset = Qnil, countnum = Qnil, retVal = Qnil;
AbstractMemory* ptr;
long off;
int count;
rb_scan_args(argc, argv, "11", &offset, &countnum);
off = NUM2LONG(offset);
count = (countnum == Qnil ? 0 : NUM2INT(countnum));
retVal = rb_ary_new2(count);
Data_Get_Struct(self, AbstractMemory, ptr);
checkRead(ptr);
if (countnum != Qnil) {
int i;
checkBounds(ptr, off, count * sizeof (char*));
for (i = 0; i < count; ++i) {
const char* strptr = *((const char**) (ptr->address + off) + i);
rb_ary_push(retVal, (strptr == NULL ? Qnil : rb_tainted_str_new2(strptr)));
}
} else {
checkBounds(ptr, off, sizeof (char*));
for ( ; off < ptr->size - (long) sizeof (void *); off += (long) sizeof (void *)) {
const char* strptr = *(const char**) (ptr->address + off);
if (strptr == NULL) {
break;
}
rb_ary_push(retVal, rb_tainted_str_new2(strptr));
}
}
return retVal;
}
|
- (String) get_bytes(offset, length)
Return string contained in memory.
455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'ext/ffi_c/AbstractMemory.c', line 455
static VALUE
memory_get_bytes(VALUE self, VALUE offset, VALUE length)
{
AbstractMemory* ptr = MEMORY(self);
long off, len;
off = NUM2LONG(offset);
len = NUM2LONG(length);
checkRead(ptr);
checkBounds(ptr, off, len);
return rb_tainted_str_new((char *) ptr->address + off, len);
}
|
- (Float) get_float32(offset) Also known as: get_float
Get a 32-bit float from memory at offset offset (alias: #get_float).
- (Float) get_float64(offset) Also known as: get_double
Get a 64-bit float (double) from memory at offset offset (alias: #get_double).
- (String) get_string(offset, length = nil)
Return string contained in memory.
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'ext/ffi_c/AbstractMemory.c', line 322
static VALUE
memory_get_string(int argc, VALUE* argv, VALUE self)
{
VALUE length = Qnil, offset = Qnil;
AbstractMemory* ptr = MEMORY(self);
long off, len;
char* end;
int nargs = rb_scan_args(argc, argv, "11", &offset, &length);
off = NUM2LONG(offset);
len = nargs > 1 && length != Qnil ? NUM2LONG(length) : (ptr->size - off);
checkRead(ptr);
checkBounds(ptr, off, len);
end = memchr(ptr->address + off, 0, len);
return rb_tainted_str_new((char *) ptr->address + off,
(end != NULL ? end - ptr->address - off : len));
}
|
- (self) put_array_of_float32(offset, ary) Also known as: put_array_of_float
Put values from ary as 32-bit floats in memory from offset offset (alias: #put_array_of_float).
- (self) put_array_of_float64(offset, ary) Also known as: put_array_of_double
Put values from ary as 64-bit floats (doubles) in memory from offset offset (alias: #put_array_of_double).
- (self) put_array_of_pointer(offset, ary)
Put an array of Pointer into memory from offset.
- (self) put_bytes(offset, str, index = 0, length = nil)
Put a string in memory.
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'ext/ffi_c/AbstractMemory.c', line 483
static VALUE
memory_put_bytes(int argc, VALUE* argv, VALUE self)
{
AbstractMemory* ptr = MEMORY(self);
VALUE offset = Qnil, str = Qnil, rbIndex = Qnil, rbLength = Qnil;
long off, len, idx;
int nargs = rb_scan_args(argc, argv, "22", &offset, &str, &rbIndex, &rbLength);
Check_Type(str, T_STRING);
off = NUM2LONG(offset);
idx = nargs > 2 ? NUM2LONG(rbIndex) : 0;
if (idx < 0) {
rb_raise(rb_eRangeError, "index canot be less than zero");
return Qnil;
}
len = nargs > 3 ? NUM2LONG(rbLength) : (RSTRING_LEN(str) - idx);
if ((idx + len) > RSTRING_LEN(str)) {
rb_raise(rb_eRangeError, "index+length is greater than size of string");
return Qnil;
}
checkWrite(ptr);
checkBounds(ptr, off, len);
if (rb_safe_level() >= 1 && OBJ_TAINTED(str)) {
rb_raise(rb_eSecurityError, "Writing unsafe string to memory");
return Qnil;
}
memcpy(ptr->address + off, RSTRING_PTR(str) + idx, len);
return self;
}
|
- (self) put_float32offset Also known as: put_float
Put value as a 32-bit float in memory at offset offset (alias: #put_float).
- (self) put_float64(offset, value) Also known as: put_double
Put value as a 64-bit float (double) in memory at offset offset (alias: #put_double).
- (self) put_pointer(offset, value)
Put value in memory from offset..
- (self) put_string(offset, str)
Put a string in memory.
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'ext/ffi_c/AbstractMemory.c', line 422
static VALUE
memory_put_string(VALUE self, VALUE offset, VALUE str)
{
AbstractMemory* ptr = MEMORY(self);
long off, len;
Check_Type(str, T_STRING);
off = NUM2LONG(offset);
len = RSTRING_LEN(str);
checkWrite(ptr);
checkBounds(ptr, off, len + 1);
if (rb_safe_level() >= 1 && OBJ_TAINTED(str)) {
rb_raise(rb_eSecurityError, "Writing unsafe string to memory");
return Qnil;
}
memcpy(ptr->address + off, RSTRING_PTR(str), len);
*((char *) ptr->address + off + len) = '\0';
return self;
}
|
- (Array<Float>) read_array_of_double(length)
Read 64-bit floats (doubles) from memory.
Same as:
memory.get_array_of_double(0, ary)
- (Array<Float>) read_array_of_float(length)
Read 32-bit floats from memory.
Same as:
memory.get_array_of_float(0, ary)
- (Array<Pointer>) read_array_of_pointer(length)
- (String) read_bytes(length)
equivalent to :
memory.get_bytes(0, length)
524 525 526 527 528 |
# File 'ext/ffi_c/AbstractMemory.c', line 524
static VALUE
memory_read_bytes(VALUE self, VALUE length)
{
return memory_get_bytes(self, INT2FIX(0), length);
}
|
- (Float) read_double
Read a 64-bit float (double) from memory.
Same as:
memory.get_double(0)
- (Float) read_float
Read a 32-bit float from memory.
Same as:
memory.get_float(0)
- (Pointer) read_pointer
- (Numeric) size Also known as: size
Return memory size in bytes (alias: #total)
303 304 305 306 307 308 309 310 311 |
# File 'ext/ffi_c/AbstractMemory.c', line 303
static VALUE
memory_size(VALUE self)
{
AbstractMemory* ptr;
Data_Get_Struct(self, AbstractMemory, ptr);
return LONG2NUM(ptr->size);
}
|
- (Numeric) type_size
Get the memory's type size.
558 559 560 561 562 563 564 565 566 |
# File 'ext/ffi_c/AbstractMemory.c', line 558
static VALUE
memory_type_size(VALUE self)
{
AbstractMemory* ptr;
Data_Get_Struct(self, AbstractMemory, ptr);
return INT2NUM(ptr->typeSize);
}
|
- (self) write_array_of_double(ary)
Write values from ary as 64-bit floats (doubles) in memory.
Same as:
memory.put_array_of_double(0, ary)
- (self) write_array_of_float(ary)
Write values from ary as 32-bit floats in memory.
Same as:
memory.put_array_of_float(0, ary)
- (self) write_array_of_pointer(ary)
- (self) write_bytes(str, index = 0, length = nil)
equivalent to :
memory.put_bytes(0, str, index, length)
539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'ext/ffi_c/AbstractMemory.c', line 539
static VALUE
memory_write_bytes(int argc, VALUE* argv, VALUE self)
{
VALUE* wargv = ALLOCA_N(VALUE, argc + 1);
int i;
wargv[0] = INT2FIX(0);
for (i = 0; i < argc; i++) {
wargv[i + 1] = argv[i];
}
return memory_put_bytes(argc + 1, wargv, self);
}
|
- (self) write_double(value)
Write value as a 64-bit float (double) in memory.
Same as:
memory.put_double(0, value)
- (self) write_float(value)
Write value as a 32-bit float in memory.
Same as:
memory.put_float(0, value)
- (self) write_pointer(value)
Write value in memory.
Equivalent to:
memory.put_pointer(0, value)