1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 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 333 334 335 336 337 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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/*
* Copyright (c) 2008, 2009, Wayne Meissner
*
* All rights reserved.
*
* This file is part of ruby-ffi.
*
* This code is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with this work. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
#include <ruby.h>
#include "rbffi.h"
#include "endian.h"
#include "AbstractMemory.h"
#include "Pointer.h"
#define POINTER(obj) rbffi_AbstractMemory_Cast((obj), rbffi_PointerClass)
VALUE rbffi_PointerClass = Qnil;
VALUE rbffi_NullPointerSingleton = Qnil;
static void ptr_release(Pointer* ptr);
static void ptr_mark(Pointer* ptr);
VALUE
rbffi_Pointer_NewInstance(void* addr)
{
Pointer* p;
VALUE obj;
if (addr == NULL) {
return rbffi_NullPointerSingleton;
}
obj = Data_Make_Struct(rbffi_PointerClass, Pointer, NULL, -1, p);
p->memory.address = addr;
p->memory.size = LONG_MAX;
p->memory.flags = (addr == NULL) ? 0 : (MEM_RD | MEM_WR);
p->memory.typeSize = 1;
p->rbParent = Qnil;
return obj;
}
static VALUE
ptr_allocate(VALUE klass)
{
Pointer* p;
VALUE obj;
obj = Data_Make_Struct(klass, Pointer, ptr_mark, ptr_release, p);
p->rbParent = Qnil;
p->memory.flags = MEM_RD | MEM_WR;
return obj;
}
/*
* @overload initialize(pointer)
* @param [Pointer] pointer another pointer to initialize from
* Create a new pointer from another {Pointer}.
* @overload initialize(type, address)
* @param [Type] type type for pointer
* @param [Integer] address base address for pointer
* Create a new pointer from a {Type} and a base adresse
* @return [self]
* A new instance of Pointer.
*/
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;
}
/*
* call-seq: ptr.initialize_copy(other)
* @param [Pointer] other source for cloning or dupping
* @return [self]
* @raise {RuntimeError} if +other+ is an unbounded memory area, or is unreable/unwritable
* @raise {NoMemError} if failed to allocate memory for new object
* DO NOT CALL THIS METHOD.
*
* This method is internally used by #dup and #clone. Memory contents is copied from +other+.
*/
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;
}
static VALUE
slice(VALUE self, long offset, long size)
{
AbstractMemory* ptr;
Pointer* p;
VALUE retval;
Data_Get_Struct(self, AbstractMemory, ptr);
checkBounds(ptr, offset, size == LONG_MAX ? 1 : size);
retval = Data_Make_Struct(rbffi_PointerClass, Pointer, ptr_mark, -1, p);
p->memory.address = ptr->address + offset;
p->memory.size = size;
p->memory.flags = ptr->flags;
p->memory.typeSize = ptr->typeSize;
p->rbParent = self;
return retval;
}
/*
* Document-method: +
* call-seq: ptr + offset
* @param [Numeric] offset
* @return [Pointer]
* Return a new {Pointer} from an existing pointer and an +offset+.
*/
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);
}
/*
* call-seq: ptr.slice(offset, length)
* @param [Numeric] offset
* @param [Numeric] length
* @return [Pointer]
* Return a new {Pointer} from an existing one. This pointer points on same contents
* from +offset+ for a length +length+.
*/
static VALUE
ptr_slice(VALUE self, VALUE rbOffset, VALUE rbLength)
{
return slice(self, NUM2LONG(rbOffset), NUM2LONG(rbLength));
}
/*
* call-seq: ptr.inspect
* @return [String]
* Inspect pointer object.
*/
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);
}
/*
* Document-method: null?
* call-seq: ptr.null?
* @return [Boolean]
* Return +true+ if +self+ is a {NULL} pointer.
*/
static VALUE
ptr_null_p(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ptr->memory.address == NULL ? Qtrue : Qfalse;
}
/*
* Document-method: ==
* call-seq: ptr == other
* @param [Pointer] other
* Check equality between +self+ and +other+. Equality is tested on {#address}.
*/
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;
}
/*
* call-seq: ptr.address
* @return [Numeric] pointer's base address
* Return +self+'s base address (alias: #to_i).
*/
static VALUE
ptr_address(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ULL2NUM((uintptr_t) ptr->memory.address);
}
#if BYTE_ORDER == LITTLE_ENDIAN
# define SWAPPED_ORDER BIG_ENDIAN
#else
# define SWAPPED_ORDER LITTLE_ENDIAN
#endif
/*
* Get or set +self+'s endianness
* @overload ptr.order
* @return [:big, :little] endianness of +self+
* @overload ptr.order(order)
* @param [Symbol] order endianness to set (+:little+, +:big+ or +:network+). +:big+ and +:network+
* are synonymous.
* @return [self]
*/
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;
}
}
/*
* call-seq: ptr.free
* @return [self]
* Free memory pointed by +self+.
*/
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;
}
return self;
}
/*
* call-seq: ptr.autorelease = autorelease
* @param [Boolean] autorelease
* @return [Boolean] +autorelease+
* Set +autorelease+ attribute. See also Autorelease section.
*/
static VALUE
ptr_autorelease(VALUE self, VALUE autorelease)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
ptr->autorelease = autorelease == Qtrue;
return autorelease;
}
/*
* call-seq: ptr.autorelease?
* @return [Boolean]
* Get +autorelease+ attribute. See also Autorelease section.
*/
static VALUE
ptr_autorelease_p(VALUE self)
{
Pointer* ptr;
Data_Get_Struct(self, Pointer, ptr);
return ptr->autorelease ? Qtrue : Qfalse;
}
static void
ptr_release(Pointer* ptr)
{
if (ptr->autorelease && ptr->allocated && ptr->storage != NULL) {
xfree(ptr->storage);
ptr->storage = NULL;
}
xfree(ptr);
}
static void
ptr_mark(Pointer* ptr)
{
rb_gc_mark(ptr->rbParent);
}
void
rbffi_Pointer_Init(VALUE moduleFFI)
{
VALUE rbNullAddress = ULL2NUM(0);
/*
* Document-class: FFI::Pointer < FFI::AbstractMemory
* 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.
*/
rbffi_PointerClass = rb_define_class_under(moduleFFI, "Pointer", rbffi_AbstractMemoryClass);
/*
* Document-variable: Pointer
*/
rb_global_variable(&rbffi_PointerClass);
rb_define_alloc_func(rbffi_PointerClass, ptr_allocate);
rb_define_method(rbffi_PointerClass, "initialize", ptr_initialize, -1);
rb_define_method(rbffi_PointerClass, "initialize_copy", ptr_initialize_copy, 1);
rb_define_method(rbffi_PointerClass, "inspect", ptr_inspect, 0);
rb_define_method(rbffi_PointerClass, "to_s", ptr_inspect, 0);
rb_define_method(rbffi_PointerClass, "+", ptr_plus, 1);
rb_define_method(rbffi_PointerClass, "slice", ptr_slice, 2);
rb_define_method(rbffi_PointerClass, "null?", ptr_null_p, 0);
rb_define_method(rbffi_PointerClass, "address", ptr_address, 0);
rb_define_alias(rbffi_PointerClass, "to_i", "address");
rb_define_method(rbffi_PointerClass, "==", ptr_equals, 1);
rb_define_method(rbffi_PointerClass, "order", ptr_order, -1);
rb_define_method(rbffi_PointerClass, "autorelease=", ptr_autorelease, 1);
rb_define_method(rbffi_PointerClass, "autorelease?", ptr_autorelease_p, 0);
rb_define_method(rbffi_PointerClass, "free", ptr_free, 0);
rbffi_NullPointerSingleton = rb_class_new_instance(1, &rbNullAddress, rbffi_PointerClass);
/*
* NULL pointer
*/
rb_define_const(rbffi_PointerClass, "NULL", rbffi_NullPointerSingleton);
}
|