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

Constants inherited from AbstractMemory

AbstractMemory::LONG_MAX

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, #freeze, #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, #size_limit?, #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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/ffi_c/MemoryPointer.c', line 89

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:



181
182
183
184
185
186
187
188
189
190
# File 'ext/ffi_c/MemoryPointer.c', line 181

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;
}