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
- #slice(rbOffset, rbLength) ⇒ 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
82 83 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 125 126 127 128 129 |
# File 'ext/ffi_c/Pointer.c', line 82
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.access = 0;
}
break;
default:
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 {
rb_raise(rb_eTypeError, "wrong argument type, expected Integer or FFI::Pointer");
}
break;
}
p->memory.typeSize = typeSize;
return self;
}
|
Class Method Details
.size ⇒ Object
Return the size of a pointer on the current platform, in bytes
35 36 37 |
# File 'lib/ffi/pointer.rb', line 35 def self.size SIZE end |
Instance Method Details
#+(offset) ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'ext/ffi_c/Pointer.c', line 153
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
192 193 194 195 196 197 198 199 200 |
# File 'ext/ffi_c/Pointer.c', line 192
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
202 203 204 205 206 207 208 209 210 |
# File 'ext/ffi_c/Pointer.c', line 202
static VALUE
ptr_address(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ULL2NUM((uintptr_t) ptr->memory.address);
}
|
#inspect ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 |
# File 'ext/ffi_c/Pointer.c', line 170
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
182 183 184 185 186 187 188 189 190 |
# File 'ext/ffi_c/Pointer.c', line 182
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
122 123 124 |
# File 'lib/ffi/pointer.rb', line 122 def read_array_of_int(length) get_array_of_int32(0, length) end |
#read_array_of_long(length) ⇒ Object
130 131 132 |
# File 'lib/ffi/pointer.rb', line 130 def read_array_of_long(length) get_array_of_long(0, length) end |
#read_array_of_pointer(length) ⇒ Object
138 139 140 |
# File 'lib/ffi/pointer.rb', line 138 def read_array_of_pointer(length) read_array_of_type(:pointer, :read_pointer, length) end |
#read_array_of_type(type, reader, length) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ffi/pointer.rb', line 102 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
74 75 76 |
# File 'lib/ffi/pointer.rb', line 74 def read_float get_float32(0) end |
#read_int ⇒ Object
Read a C int from the memory pointed to.
44 45 46 |
# File 'lib/ffi/pointer.rb', line 44 def read_int get_int32(0) end |
#read_long ⇒ Object
Read a C long from the memory pointed to.
54 55 56 |
# File 'lib/ffi/pointer.rb', line 54 def read_long get_long(0) end |
#read_long_long ⇒ Object
Read a C long long from the memory pointed to.
63 64 65 |
# File 'lib/ffi/pointer.rb', line 63 def read_long_long get_int64(0) end |
#read_pointer ⇒ Object
67 68 69 |
# File 'lib/ffi/pointer.rb', line 67 def read_pointer get_pointer(0) end |
#read_string(len = nil) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/ffi/pointer.rb', line 81 def read_string(len=nil) if len get_bytes(0, len) else get_string(0) end end |
#read_string_length(len) ⇒ Object
88 89 90 |
# File 'lib/ffi/pointer.rb', line 88 def read_string_length(len) get_bytes(0, len) end |
#read_string_to_null ⇒ Object
91 92 93 |
# File 'lib/ffi/pointer.rb', line 91 def read_string_to_null get_string(0) end |
#slice(rbOffset, rbLength) ⇒ Object
164 165 166 167 168 |
# File 'ext/ffi_c/Pointer.c', line 164
static VALUE
ptr_slice(VALUE self, VALUE rbOffset, VALUE rbLength)
{
return slice(self, NUM2LONG(rbOffset), NUM2LONG(rbLength));
}
|
#write_array_of_int(ary) ⇒ Object
126 127 128 |
# File 'lib/ffi/pointer.rb', line 126 def write_array_of_int(ary) put_array_of_int32(0, ary) end |
#write_array_of_long(ary) ⇒ Object
134 135 136 |
# File 'lib/ffi/pointer.rb', line 134 def write_array_of_long(ary) put_array_of_long(0, ary) end |
#write_array_of_pointer(ary) ⇒ Object
142 143 144 |
# File 'lib/ffi/pointer.rb', line 142 def write_array_of_pointer(ary) write_array_of_type(:pointer, :write_pointer, ary) end |
#write_array_of_type(type, writer, ary) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/ffi/pointer.rb', line 113 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
77 78 79 |
# File 'lib/ffi/pointer.rb', line 77 def write_float(obj) put_float32(0, obj) end |
#write_int(obj) ⇒ Object
Write obj
as a C int at the memory pointed to.
39 40 41 |
# File 'lib/ffi/pointer.rb', line 39 def write_int(obj) put_int32(0, obj) end |
#write_long(obj) ⇒ Object
Write obj
as a C long at the memory pointed to.
49 50 51 |
# File 'lib/ffi/pointer.rb', line 49 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.
58 59 60 |
# File 'lib/ffi/pointer.rb', line 58 def write_long_long(obj) put_int64(0, obj) end |
#write_pointer(ptr) ⇒ Object
70 71 72 |
# File 'lib/ffi/pointer.rb', line 70 def write_pointer(ptr) put_pointer(0, ptr) end |
#write_string(str, len = nil) ⇒ Object
97 98 99 100 101 |
# File 'lib/ffi/pointer.rb', line 97 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
94 95 96 |
# File 'lib/ffi/pointer.rb', line 94 def write_string_length(str, len) put_bytes(0, str, 0, len) end |