Class: FFI::MemoryPointer

Inherits:
Pointer show all
Defined in:
ext/ffi_c/MemoryPointer.c,
ext/ffi_c/MemoryPointer.c

Overview

A MemoryPointer is a specific Pointer. It points to a memory composed of cells. All cells have the same size.

Examples:

Create a new MemoryPointer

mp = FFI::MemoryPointer.new(:long, 16)   # Create a pointer on a memory of 16 long ints.

Create a new MemoryPointer from a String

mp1 = FFI::MemoryPointer.from_string("this is a string")
# same as:
mp2 = FFI::MemoryPointer.new(:char,16)
mp2.put_string("this is a string")

Constant Summary

Constants inherited from Pointer

Pointer::NULL, Pointer::SIZE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pointer

#+, #==, #address, #autorelease=, #autorelease?, #free, #initialize_copy, #inspect, #null?, #order, #read, #read_array_of_type, #read_string, #read_string_length, #read_string_to_null, size, #slice, #to_ptr, #to_s, #type_size, #write, #write_array_of_type, #write_string, #write_string_length

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, #type_size, #write_array_of_double, #write_array_of_float, #write_array_of_pointer, #write_bytes, #write_double, #write_float, #write_pointer

Constructor Details

#initialize(size, count = 1, clear = true) ⇒ self

A new instance of FFI::MemoryPointer.

Parameters:

  • size (Fixnum, Bignum, Symbol, FFI::Type)

    size of a memory cell (in bytes, or type whom size will be used)

  • count (Numeric)

    number of cells in memory

  • clear (Boolean)

    set memory to all-zero if true


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'ext/ffi_c/MemoryPointer.c', line 80

static VALUE
memptr_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE size = Qnil, count = Qnil, clear = Qnil;
    int nargs = rb_scan_args(argc, argv, "12", &size, &count, &clear);

    memptr_malloc(self, rbffi_type_size(size), nargs > 1 ? NUM2LONG(count) : 1,
        RTEST(clear) || clear == Qnil);

    if (rb_block_given_p()) {
        return rb_ensure(rb_yield, self, memptr_free, self);
    }

    return self;
}

Class Method Details

.from_string(s) ⇒ MemoryPointer

Create a FFI::MemoryPointer with s inside.

Parameters:

  • s (String)

    string

Returns:


159
160
161
162
163
164
165
166
167
168
# File 'ext/ffi_c/MemoryPointer.c', line 159

static VALUE
memptr_s_from_string(VALUE klass, VALUE to_str)
{
    VALUE s = StringValue(to_str);
    VALUE args[] = { INT2FIX(1), LONG2NUM(RSTRING_LEN(s) + 1), Qfalse };
    VALUE obj = rb_class_new_instance(3, args, klass);
    rb_funcall(obj, rb_intern("put_string"), 2, INT2FIX(0), s);

    return obj;
}