Class: FFI::Pointer
- Inherits:
-
AbstractMemoryClass
- Object
- AbstractMemoryClass
- FFI::Pointer
- Defined in:
- lib/ffi/pointer.rb,
ext/ffi_c/Pointer.c
Direct Known Subclasses
AutoPointer, DynamicLibrary::Symbol, Function, MemoryPointer
Constant Summary collapse
- SIZE =
Platform::ADDRESS_SIZE / 8
- NULL =
rbffi_NullPointerSingleton
Class Method Summary collapse
-
.size ⇒ Object
Return the size of a pointer on the current platform, in bytes.
Instance Method Summary collapse
- #+(offset) ⇒ Object
- #==(other) ⇒ Object
- #address ⇒ Object (also: #to_i)
- #initialize(*args) ⇒ Object constructor
- #inspect ⇒ Object
- #null? ⇒ Boolean
- #read_array_of_int(length) ⇒ Object
- #read_array_of_long(length) ⇒ Object
- #read_array_of_pointer(length) ⇒ Object
- #read_array_of_type(type, reader, length) ⇒ Object
- #read_float ⇒ Object
-
#read_int ⇒ Object
Read a C int from the memory pointed to.
-
#read_long ⇒ Object
Read a C long from the memory pointed to.
-
#read_long_long ⇒ Object
Read a C long long from the memory pointed to.
- #read_pointer ⇒ Object
- #read_string(len = nil) ⇒ Object
- #read_string_length(len) ⇒ Object
- #read_string_to_null ⇒ Object
- #write_array_of_int(ary) ⇒ Object
- #write_array_of_long(ary) ⇒ Object
- #write_array_of_pointer(ary) ⇒ Object
- #write_array_of_type(type, writer, ary) ⇒ Object
- #write_float(obj) ⇒ Object
-
#write_int(obj) ⇒ Object
Write
obj
as a C int at the memory pointed to. -
#write_long(obj) ⇒ Object
Write
obj
as a C long at the memory pointed to. -
#write_long_long(obj) ⇒ Object
Write
obj
as a C long long at the memory pointed to. - #write_pointer(ptr) ⇒ Object
- #write_string(str, len = nil) ⇒ Object
- #write_string_length(str, len) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 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 |
# File 'ext/ffi_c/Pointer.c', line 84
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");
}
if (rb_obj_is_kind_of(rbAddress, rbffi_PointerClass)) {
Pointer* orig;
p->parent = rbAddress;
Data_Get_Struct(rbAddress, Pointer, orig);
p->memory = orig->memory;
} else {
p->memory.address = (void*)(uintptr_t) NUM2LL(rbAddress);
p->memory.size = LONG_MAX;
if (p->memory.address == NULL) {
p->memory.access = 0;
}
}
p->memory.typeSize = typeSize;
return self;
}
|
Class Method Details
.size ⇒ Object
Return the size of a pointer on the current platform, in bytes
7 8 9 |
# File 'lib/ffi/pointer.rb', line 7 def self.size SIZE end |
Instance Method Details
#+(offset) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'ext/ffi_c/Pointer.c', line 127
static VALUE
ptr_plus(VALUE self, VALUE offset)
{
AbstractMemory* ptr;
Pointer* p;
VALUE retval;
long off = NUM2LONG(offset);
Data_Get_Struct(self, AbstractMemory, ptr);
checkBounds(ptr, off, 1);
retval = Data_Make_Struct(rbffi_PointerClass, Pointer, ptr_mark, -1, p);
p->memory.address = ptr->address + off;
p->memory.size = ptr->size == LONG_MAX ? LONG_MAX : ptr->size - off;
p->memory.ops = &rbffi_AbstractMemoryOps;
p->memory.access = ptr->access;
p->memory.typeSize = ptr->typeSize;
p->parent = self;
return retval;
}
|
#==(other) ⇒ Object
172 173 174 175 176 177 178 179 180 |
# File 'ext/ffi_c/Pointer.c', line 172
static VALUE
ptr_equals(VALUE self, VALUE other)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ptr->memory.address == POINTER(other)->address ? Qtrue : Qfalse;
}
|
#address ⇒ Object Also known as: to_i
182 183 184 185 186 187 188 189 190 |
# File 'ext/ffi_c/Pointer.c', line 182
static VALUE
ptr_address(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ULL2NUM((uintptr_t) ptr->memory.address);
}
|
#inspect ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'ext/ffi_c/Pointer.c', line 150
static VALUE
ptr_inspect(VALUE self)
{
Pointer* ptr;
char tmp[100];
Data_Get_Struct(self, Pointer, ptr);
snprintf(tmp, sizeof(tmp), "#<FFI::Pointer address=%p>", ptr->memory.address);
return rb_str_new2(tmp);
}
|
#null? ⇒ Boolean
162 163 164 165 166 167 168 169 170 |
# File 'ext/ffi_c/Pointer.c', line 162
static VALUE
ptr_null_p(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ptr->memory.address == NULL ? Qtrue : Qfalse;
}
|
#read_array_of_int(length) ⇒ Object
94 95 96 |
# File 'lib/ffi/pointer.rb', line 94 def read_array_of_int(length) get_array_of_int32(0, length) end |
#read_array_of_long(length) ⇒ Object
102 103 104 |
# File 'lib/ffi/pointer.rb', line 102 def read_array_of_long(length) get_array_of_long(0, length) end |
#read_array_of_pointer(length) ⇒ Object
110 111 112 |
# File 'lib/ffi/pointer.rb', line 110 def read_array_of_pointer(length) read_array_of_type(:pointer, :read_pointer, length) end |
#read_array_of_type(type, reader, length) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ffi/pointer.rb', line 74 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_float ⇒ Object
46 47 48 |
# File 'lib/ffi/pointer.rb', line 46 def read_float get_float32(0) end |
#read_int ⇒ Object
Read a C int from the memory pointed to.
16 17 18 |
# File 'lib/ffi/pointer.rb', line 16 def read_int get_int32(0) end |
#read_long ⇒ Object
Read a C long from the memory pointed to.
26 27 28 |
# File 'lib/ffi/pointer.rb', line 26 def read_long get_long(0) end |
#read_long_long ⇒ Object
Read a C long long from the memory pointed to.
35 36 37 |
# File 'lib/ffi/pointer.rb', line 35 def read_long_long get_int64(0) end |
#read_pointer ⇒ Object
39 40 41 |
# File 'lib/ffi/pointer.rb', line 39 def read_pointer get_pointer(0) end |
#read_string(len = nil) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/ffi/pointer.rb', line 53 def read_string(len=nil) if len get_bytes(0, len) else get_string(0) end end |
#read_string_length(len) ⇒ Object
60 61 62 |
# File 'lib/ffi/pointer.rb', line 60 def read_string_length(len) get_bytes(0, len) end |
#read_string_to_null ⇒ Object
63 64 65 |
# File 'lib/ffi/pointer.rb', line 63 def read_string_to_null get_string(0) end |
#write_array_of_int(ary) ⇒ Object
98 99 100 |
# File 'lib/ffi/pointer.rb', line 98 def write_array_of_int(ary) put_array_of_int32(0, ary) end |
#write_array_of_long(ary) ⇒ Object
106 107 108 |
# File 'lib/ffi/pointer.rb', line 106 def write_array_of_long(ary) put_array_of_long(0, ary) end |
#write_array_of_pointer(ary) ⇒ Object
114 115 116 |
# File 'lib/ffi/pointer.rb', line 114 def write_array_of_pointer(ary) write_array_of_type(:pointer, :write_pointer, ary) end |
#write_array_of_type(type, writer, ary) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/ffi/pointer.rb', line 85 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_float(obj) ⇒ Object
49 50 51 |
# File 'lib/ffi/pointer.rb', line 49 def write_float(obj) put_float32(0, obj) end |
#write_int(obj) ⇒ Object
Write obj
as a C int at the memory pointed to.
11 12 13 |
# File 'lib/ffi/pointer.rb', line 11 def write_int(obj) put_int32(0, obj) end |
#write_long(obj) ⇒ Object
Write obj
as a C long at the memory pointed to.
21 22 23 |
# File 'lib/ffi/pointer.rb', line 21 def write_long(obj) put_long(0, obj) end |
#write_long_long(obj) ⇒ Object
Write obj
as a C long long at the memory pointed to.
30 31 32 |
# File 'lib/ffi/pointer.rb', line 30 def write_long_long(obj) put_int64(0, obj) end |
#write_pointer(ptr) ⇒ Object
42 43 44 |
# File 'lib/ffi/pointer.rb', line 42 def write_pointer(ptr) put_pointer(0, ptr) end |
#write_string(str, len = nil) ⇒ Object
69 70 71 72 73 |
# File 'lib/ffi/pointer.rb', line 69 def write_string(str, len=nil) len = str.size unless len # Write the string data without NUL termination put_bytes(0, str, 0, len) end |
#write_string_length(str, len) ⇒ Object
66 67 68 |
# File 'lib/ffi/pointer.rb', line 66 def write_string_length(str, len) put_bytes(0, str, 0, len) end |