Class: FFI::Pointer
- Inherits:
-
AbstractMemory
- Object
- AbstractMemory
- FFI::Pointer
- Defined in:
- ext/ffi_c/Pointer.c,
lib/ffi/pointer.rb,
ext/ffi_c/Pointer.c
Overview
Pointer class is used to manage C pointers with ease. A Pointer object is defined by his #address (as a C pointer). It permits additions with an integer for pointer arithmetic.
Autorelease
A pointer object may autorelease his contents when freed (by default). This behaviour may be changed with #autorelease= method.
Direct Known Subclasses
AutoPointer, DynamicLibrary::Symbol, Function, MemoryPointer
Constant Summary collapse
- SIZE =
Pointer size
Platform::ADDRESS_SIZE / 8
- NULL =
NULL pointer
rbffi_NullPointerSingleton
Class Method Summary collapse
-
.size ⇒ Numeric
Return the size of a pointer on the current platform, in bytes.
Instance Method Summary collapse
-
#+(offset) ⇒ Pointer
Return a new Pointer from an existing pointer and an
offset
. -
#==(other) ⇒ Object
Check equality between
self
andother
. -
#address ⇒ Numeric
(also: #to_i)
Return
self
‘s base address (alias: #to_i). -
#autorelease=(autorelease) ⇒ Boolean
Set
autorelease
attribute. -
#autorelease? ⇒ Boolean
Get
autorelease
attribute. -
#free ⇒ self
Free memory pointed by
self
. -
#initialize(*args) ⇒ self
constructor
A new instance of Pointer.
-
#initialize_copy(other) ⇒ self
DO NOT CALL THIS METHOD.
-
#inspect ⇒ String
Inspect pointer object.
-
#null? ⇒ Boolean
Return
true
ifself
is a NULL pointer. -
#order(*args) ⇒ Object
Get or set
self
‘s endianness. -
#read(type) ⇒ Object
Read pointer’s contents as
type
. -
#read_array_of_type(type, reader, length) ⇒ Array
Read an array of
type
of lengthlength
. -
#read_string(len = nil) ⇒ String
Read pointer’s contents as a string, or the first
len
bytes of the equivalent string iflen
is notnil
. -
#read_string_length(len) ⇒ String
Read the first
len
bytes of pointer’s contents as a string. -
#read_string_to_null ⇒ String
Read pointer’s contents as a string.
-
#slice(offset, length) ⇒ Pointer
Return a new Pointer from an existing one.
- #to_ptr ⇒ self
-
#inspect ⇒ String
Inspect pointer object.
- #type_size ⇒ Object
-
#write(type, value) ⇒ nil
Write
value
of typetype
to pointer’s content. -
#write_array_of_type(type, writer, ary) ⇒ self
Write
ary
in pointer’s contents astype
. -
#write_string(str, len = nil) ⇒ self
Write
str
in pointer’s contents, or firstlen
bytes iflen
is notnil
. -
#write_string_length(str, len) ⇒ self
Write
len
first bytes ofstr
in pointer’s contents.
Methods inherited from AbstractMemory
#[], #__copy_from__, #clear, #get, #get_array_of_float32, #get_array_of_float64, #get_array_of_pointer, #get_array_of_string, #get_bytes, #get_float32, #get_float64, #get_pointer, #get_string, #put, #put_array_of_float32, #put_array_of_float64, #put_array_of_pointer, #put_bytes, #put_float32, #put_float64, #put_pointer, #put_string, #read_array_of_double, #read_array_of_float, #read_array_of_pointer, #read_bytes, #read_double, #read_float, #read_pointer, #total, #write_array_of_double, #write_array_of_float, #write_array_of_pointer, #write_bytes, #write_double, #write_float, #write_pointer
Constructor Details
#initialize(pointer) ⇒ self #initialize(type, address) ⇒ self
A new instance of Pointer.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'ext/ffi_c/Pointer.c', line 96
static VALUE
ptr_initialize(int argc, VALUE* argv, VALUE self)
{
Pointer* p;
VALUE rbType = Qnil, rbAddress = Qnil;
int typeSize = 1;
Data_Get_Struct(self, Pointer, p);
switch (rb_scan_args(argc, argv, "11", &rbType, &rbAddress)) {
case 1:
rbAddress = rbType;
typeSize = 1;
break;
case 2:
typeSize = rbffi_type_size(rbType);
break;
default:
rb_raise(rb_eArgError, "Invalid arguments");
}
switch (TYPE(rbAddress)) {
case T_FIXNUM:
case T_BIGNUM:
p->memory.address = (void*) (uintptr_t) NUM2LL(rbAddress);
p->memory.size = LONG_MAX;
if (p->memory.address == NULL) {
p->memory.flags = 0;
}
break;
default:
if (rb_obj_is_kind_of(rbAddress, rbffi_PointerClass)) {
Pointer* orig;
p->rbParent = rbAddress;
Data_Get_Struct(rbAddress, Pointer, orig);
p->memory = orig->memory;
} else {
rb_raise(rb_eTypeError, "wrong argument type, expected Integer or FFI::Pointer");
}
break;
}
p->memory.typeSize = typeSize;
return self;
}
|
Class Method Details
.size ⇒ Numeric
Return the size of a pointer on the current platform, in bytes
42 43 44 |
# File 'lib/ffi/pointer.rb', line 42 def self.size SIZE end |
Instance Method Details
#+(offset) ⇒ Pointer
Return a new FFI::Pointer from an existing pointer and an offset
.
224 225 226 227 228 229 230 231 232 233 |
# File 'ext/ffi_c/Pointer.c', line 224
static VALUE
ptr_plus(VALUE self, VALUE offset)
{
AbstractMemory* ptr;
long off = NUM2LONG(offset);
Data_Get_Struct(self, AbstractMemory, ptr);
return slice(self, off, ptr->size == LONG_MAX ? LONG_MAX : ptr->size - off);
}
|
#==(other) ⇒ Object
Check equality between self
and other
. Equality is tested on #address.
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'ext/ffi_c/Pointer.c', line 294
static VALUE
ptr_equals(VALUE self, VALUE other)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
if (NIL_P(other)) {
return ptr->memory.address == NULL ? Qtrue : Qfalse;
}
return ptr->memory.address == POINTER(other)->address ? Qtrue : Qfalse;
}
|
#address ⇒ Numeric Also known as: to_i
Return self
‘s base address (alias: #to_i).
313 314 315 316 317 318 319 320 321 |
# File 'ext/ffi_c/Pointer.c', line 313
static VALUE
ptr_address(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ULL2NUM((uintptr_t) ptr->memory.address);
}
|
#autorelease=(autorelease) ⇒ Boolean
Set autorelease
attribute. See also Autorelease section.
421 422 423 424 425 426 427 428 429 430 |
# File 'ext/ffi_c/Pointer.c', line 421
static VALUE
ptr_autorelease(VALUE self, VALUE autorelease)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
ptr->autorelease = autorelease == Qtrue;
return autorelease;
}
|
#autorelease? ⇒ Boolean
Get autorelease
attribute. See also Autorelease section.
437 438 439 440 441 442 443 444 445 |
# File 'ext/ffi_c/Pointer.c', line 437
static VALUE
ptr_autorelease_p(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ptr->autorelease ? Qtrue : Qfalse;
}
|
#free ⇒ self
Free memory pointed by self
.
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'ext/ffi_c/Pointer.c', line 382
static VALUE
ptr_free(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
if (ptr->allocated) {
if (ptr->storage != NULL) {
xfree(ptr->storage);
ptr->storage = NULL;
}
ptr->allocated = false;
} else {
VALUE caller = rb_funcall(rb_funcall(Qnil, rb_intern("caller"), 0), rb_intern("first"), 0);
rb_warn("calling free on non allocated pointer %s from %s", RSTRING_PTR(ptr_inspect(self)), RSTRING_PTR(rb_str_to_str(caller)));
}
return self;
}
|
#initialize_copy(other) ⇒ self
DO NOT CALL THIS METHOD.
This method is internally used by #dup and #clone. Memory content is copied from other
.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'ext/ffi_c/Pointer.c', line 155
static VALUE
ptr_initialize_copy(VALUE self, VALUE other)
{
AbstractMemory* src;
Pointer* dst;
Data_Get_Struct(self, Pointer, dst);
src = POINTER(other);
if (src->size == LONG_MAX) {
rb_raise(rb_eRuntimeError, "cannot duplicate unbounded memory area");
return Qnil;
}
if ((dst->memory.flags & (MEM_RD | MEM_WR)) != (MEM_RD | MEM_WR)) {
rb_raise(rb_eRuntimeError, "cannot duplicate unreadable/unwritable memory area");
return Qnil;
}
if (dst->storage != NULL) {
xfree(dst->storage);
dst->storage = NULL;
}
dst->storage = xmalloc(src->size + 7);
if (dst->storage == NULL) {
rb_raise(rb_eNoMemError, "failed to allocate memory size=%lu bytes", src->size);
return Qnil;
}
dst->allocated = true;
dst->autorelease = true;
dst->memory.address = (void *) (((uintptr_t) dst->storage + 0x7) & (uintptr_t) ~0x7UL);
dst->memory.size = src->size;
dst->memory.typeSize = src->typeSize;
/* finally, copy the actual memory contents */
memcpy(dst->memory.address, src->address, src->size);
return self;
}
|
#inspect ⇒ String
Inspect pointer object.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'ext/ffi_c/Pointer.c', line 254
static VALUE
ptr_inspect(VALUE self)
{
char buf[100];
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
if (ptr->memory.size != LONG_MAX) {
snprintf(buf, sizeof(buf), "#<%s address=%p size=%lu>",
rb_obj_classname(self), ptr->memory.address, ptr->memory.size);
} else {
snprintf(buf, sizeof(buf), "#<%s address=%p>", rb_obj_classname(self), ptr->memory.address);
}
return rb_str_new2(buf);
}
|
#null? ⇒ Boolean
Return true
if self
is a NULL pointer.
278 279 280 281 282 283 284 285 286 |
# File 'ext/ffi_c/Pointer.c', line 278
static VALUE
ptr_null_p(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ptr->memory.address == NULL ? Qtrue : Qfalse;
}
|
#order ⇒ :big, :little #order(order) ⇒ self
Get or set self
‘s endianness
338 339 340 341 342 343 344 345 346 347 348 349 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 |
# File 'ext/ffi_c/Pointer.c', line 338
static VALUE
ptr_order(int argc, VALUE* argv, VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
if (argc == 0) {
int order = (ptr->memory.flags & MEM_SWAP) == 0 ? BYTE_ORDER : SWAPPED_ORDER;
return order == BIG_ENDIAN ? ID2SYM(rb_intern("big")) : ID2SYM(rb_intern("little"));
} else {
VALUE rbOrder = Qnil;
int order = BYTE_ORDER;
if (rb_scan_args(argc, argv, "1", &rbOrder) < 1) {
rb_raise(rb_eArgError, "need byte order");
}
if (SYMBOL_P(rbOrder)) {
ID id = SYM2ID(rbOrder);
if (id == rb_intern("little")) {
order = LITTLE_ENDIAN;
} else if (id == rb_intern("big") || id == rb_intern("network")) {
order = BIG_ENDIAN;
}
}
if (order != BYTE_ORDER) {
Pointer* p2;
VALUE retval = slice(self, 0, ptr->memory.size);
Data_Get_Struct(retval, Pointer, p2);
p2->memory.flags |= MEM_SWAP;
return retval;
}
return self;
}
}
|
#read(type) ⇒ Object
Read pointer’s contents as type
Same as:
ptr.get(type, 0)
146 147 148 |
# File 'lib/ffi/pointer.rb', line 146 def read(type) get(type, 0) end |
#read_array_of_type(type, reader, length) ⇒ Array
Read an array of type
of length length
.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ffi/pointer.rb', line 107 def read_array_of_type(type, reader, length) ary = [] size = FFI.type_size(type) tmp = self length.times { |j| ary << tmp.send(reader) tmp += size unless j == length-1 # avoid OOB } ary end |
#read_string(len = nil) ⇒ String
Read pointer’s contents as a string, or the first len
bytes of the equivalent string if len
is not nil
.
50 51 52 53 54 55 56 57 |
# File 'lib/ffi/pointer.rb', line 50 def read_string(len=nil) if len return '' if len == 0 get_bytes(0, len) else get_string(0) end end |
#read_string_length(len) ⇒ String
Read the first len
bytes of pointer’s contents as a string.
Same as:
ptr.read_string(len) # with len not nil
65 66 67 |
# File 'lib/ffi/pointer.rb', line 65 def read_string_length(len) get_bytes(0, len) end |
#read_string_to_null ⇒ String
Read pointer’s contents as a string.
Same as:
ptr.read_string # with no len
74 75 76 |
# File 'lib/ffi/pointer.rb', line 74 def read_string_to_null get_string(0) end |
#slice(offset, length) ⇒ Pointer
Return a new FFI::Pointer from an existing one. This pointer points on same contents from offset
for a length length
.
243 244 245 246 247 |
# File 'ext/ffi_c/Pointer.c', line 243
static VALUE
ptr_slice(VALUE self, VALUE rbOffset, VALUE rbLength)
{
return slice(self, NUM2LONG(rbOffset), NUM2LONG(rbLength));
}
|
#to_ptr ⇒ self
136 137 138 |
# File 'lib/ffi/pointer.rb', line 136 def to_ptr self end |
#inspect ⇒ String
Inspect pointer object.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'ext/ffi_c/Pointer.c', line 254
static VALUE
ptr_inspect(VALUE self)
{
char buf[100];
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
if (ptr->memory.size != LONG_MAX) {
snprintf(buf, sizeof(buf), "#<%s address=%p size=%lu>",
rb_obj_classname(self), ptr->memory.address, ptr->memory.size);
} else {
snprintf(buf, sizeof(buf), "#<%s address=%p>", rb_obj_classname(self), ptr->memory.address);
}
return rb_str_new2(buf);
}
|
#type_size ⇒ Object
405 406 407 408 409 410 411 412 413 |
# File 'ext/ffi_c/Pointer.c', line 405
static VALUE
ptr_type_size(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return INT2NUM(ptr->memory.typeSize);
}
|
#write(type, value) ⇒ nil
Write value
of type type
to pointer’s content
Same as:
ptr.put(type, 0)
157 158 159 |
# File 'lib/ffi/pointer.rb', line 157 def write(type, value) put(type, 0, value) end |
#write_array_of_type(type, writer, ary) ⇒ self
Write ary
in pointer’s contents as type
.
125 126 127 128 129 130 131 132 133 |
# File 'lib/ffi/pointer.rb', line 125 def write_array_of_type(type, writer, ary) size = FFI.type_size(type) tmp = self ary.each_with_index {|i, j| tmp.send(writer, i) tmp += size unless j == ary.length-1 # avoid OOB } self end |
#write_string(str, len = nil) ⇒ self
Write str
in pointer’s contents, or first len
bytes if len
is not nil
.
94 95 96 97 98 |
# File 'lib/ffi/pointer.rb', line 94 def write_string(str, len=nil) len = str.bytesize unless len # Write the string data without NUL termination put_bytes(0, str, 0, len) end |
#write_string_length(str, len) ⇒ self
Write len
first bytes of str
in pointer’s contents.
Same as:
ptr.write_string(str, len) # with len not nil
85 86 87 |
# File 'lib/ffi/pointer.rb', line 85 def write_string_length(str, len) put_bytes(0, str, 0, len) end |