123456789_123456789_123456789_123456789_123456789_

Class: FFI::MemoryPointer

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: FFI::Pointer
Defined in: 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

AbstractMemory - Inherited

LONG_MAX

Pointer - Inherited

NULL, SIZE

Class Method Summary

Pointer - Inherited

.new

A new instance of Pointer.

.size

Return the size of a pointer on the current platform, in bytes.

Instance Attribute Summary

Pointer - Inherited

AbstractMemory - Inherited

#size_limit?

Return true if self has a size limit.

Instance Method Summary

Pointer - Inherited

#+, #==,
#address

Alias for Pointer#to_i.

#free,
#initialize_copy

This method is internally used by #dup and #clone.

#inspect

Alias for Pointer#to_s.

#order

Get or set self‘s endianness.

#read

Read pointer’s contents as type

#read_array_of_type

Read an array of type of length length.

#read_string

Read pointer’s contents as a string, or the first len bytes of the equivalent string if len is not nil.

#read_string_length

Read the first len bytes of pointer’s contents as a string.

#read_string_to_null

Read pointer’s contents as a string.

#slice, #to_i, #to_ptr, #to_s, #type_size,
#write

Write value of type type to pointer’s content.

#write_array_of_type

Write ary in pointer’s contents as type.

#write_string

Write str in pointer’s contents, or first len bytes if len is not nil.

#write_string_length

Write len first bytes of str in pointer’s contents.

AbstractMemory - Inherited

Constructor Details

.new(size, count = 1, clear = true) .new(instance)

[ GitHub ]

  
# 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) Create(a {MemoryPointer} with {s} inside.)

[ GitHub ]

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