### Home Classes Methods

### In Files

- ext/ffi_c/ffi.c

### Parent

Object

### Methods

- #[]
- #__copy_from__
- #clear
- #get_array_of_double
- #get_array_of_float
- #get_array_of_float32
- #get_array_of_float64
- #get_array_of_pointer
- #get_array_of_string
- #get_bytes
- #get_double
- #get_float
- #get_float32
- #get_float64
- #get_pointer
- #get_string
- #put_" #name, "put_" #old); \ rb_define_alias(classMemory, "get_" #name, "get_" #old); \ rb_define_alias(classMemory, "put_u" #name, "put_u" #old); \ rb_define_alias(classMemory, "get_u" #name, "get_u" #old); \ rb_define_alias(classMemory, "write_" #name, "write_" #old); \ rb_define_alias(classMemory, "read_" #name, "read_" #old); \ rb_define_alias(classMemory, "write_u" #name, "write_u" #old); \ rb_define_alias(classMemory, "read_u" #name, "read_u" #old); \ rb_define_alias(classMemory, "put_array_of_" #name, "put_array_of_" #old); \ rb_define_alias(classMemory, "get_array_of_" #name, "get_array_of_" #old); \ rb_define_alias(classMemory, "put_array_of_u" #name, "put_array_of_u" #old); \ rb_define_alias(classMemory, "get_array_of_u" #name, "get_array_of_u" #old); \ rb_define_alias(classMemory, "write_array_of_" #name, "write_array_of_" #old); \ rb_define_alias(classMemory, "read_array_of_" #name, "read_array_of_" #old); \ rb_define_alias(classMemory, "write_array_of_u" #name, "write_array_of_u" #old); \ rb_define_alias(classMemory, "read_array_of_u" #name, "read_array_of_u" #old); ALIAS(char, int8); ALIAS(short, int16); ALIAS(int, int32); ALIAS(long_long, int64); /* * Document-method: put_float32 * call-seq: memory.put_float32offset, value) * @param [Numeric] offset * @param [Numeric] value * @return [self] * Put +value+ as a 32-bit float in memory at offset +offset+ (alias: #put_float). */ rb_define_method(classMemory, "put_float32", memory_put_float32, 2); /* * Document-method: get_float32 * call-seq: memory.get_float32(offset) * @param [Numeric] offset * @return [Float] * Get a 32-bit float from memory at offset +offset+ (alias: #get_float). */ rb_define_method(classMemory, "get_float32", memory_get_float32, 1); rb_define_alias(classMemory, "put_float
- #put_array_of_double
- #put_array_of_float
- #put_array_of_float32
- #put_array_of_float64
- #put_array_of_pointer
- #put_bytes
- #put_double
- #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
- #total
- #type_size
- #write_array_of_double
- #write_array_of_float
- #write_array_of_pointer
- #write_bytes
- #write_double
- #write_float
- #write_pointer

### Files

- Makefile
- ChangeLog
- LICENSE
- README
- compile
- configure
- depcomp
- stamp-vti
- install-sh
- libtool-version
- mdate-sh
- missing

### Class/Module Index

Quicksearch

- FFI
- FFI::AbstractMemory
- FFI::ArrayType
- FFI::AutoPointer
- FFI::AutoPointer::CallableReleaser
- FFI::AutoPointer::DefaultReleaser
- FFI::AutoPointer::Releaser
- FFI::Buffer
- FFI::ConstGenerator
- FFI::ConstGenerator::Constant
- FFI::DynamicLibrary
- FFI::DynamicLibrary::Symbol
- FFI::Enum
- FFI::Enums
- FFI::FunctionType
- FFI::Generator
- FFI::Generator::Task
- FFI::IO
- FFI::LastError
- FFI::Library
- FFI::ManagedStruct
- FFI::NotFoundError
- FFI::NullPointerError
- FFI::Platform
- FFI::PlatformError
- FFI::Pointer
- FFI::StrPtrConverter
- FFI::Struct
- FFI::Struct::ManagedStructConverter
- FFI::StructByReference
- FFI::StructByValue
- FFI::StructGenerator
- FFI::StructGenerator::Field
- FFI::StructLayout
- FFI::StructLayout::Array
- FFI::StructLayout::Enum
- FFI::StructLayout::Field
- FFI::StructLayout::Function
- FFI::StructLayout::InnerStruct
- FFI::StructLayout::Mapped
- FFI::StructLayout::Number
- FFI::StructLayout::Pointer
- FFI::StructLayout::String
- FFI::StructLayoutBuilder
- FFI::TypesGenerator
- FFI::Union
- FFI::VariadicInvoker

No matching classes.

# FFI::AbstractMemory

{AbstractMemory} is the base class for many memory management classes such as {Buffer}.

This class has a lot of methods to work with integers :

- put_intsize(offset, value)

- get_intsize(offset)

- put_uintsize(offset, value)

- get_uintsize(offset)

- writeuintsize(value)

- read_intsize

- write_uintsize(value)

- read_uintsize

- put_array_of_intsize(offset, ary)

- get_array_of_intsize(offset, length)

- put_array_of_uintsize(offset, ary)

- get_array_of_uintsize(offset, length)

- write_array_of_intsize(ary)

- read_array_of_intsize(length)

- write_array_of_uintsize(ary)

- read_array_of_uintsize(length)

where size is 8, 16, 32 or 64. Same methods exist for long type.

Aliases exist : char for int8, short for int16, int for int32 and long_long for int64.

Others methods are listed below.

### Public Instance Methods

memory[idx] click to toggle source
@param [Numeric] idx index to access in memory
@return
Memory read accessor.

    static VALUE
    memory_aref(VALUE self, VALUE idx)
    {
        AbstractMemory* ptr;
        VALUE rbOffset = Qnil;

        Data_Get_Struct(self, AbstractMemory, ptr);

        rbOffset = ULONG2NUM(NUM2ULONG(idx) * ptr->typeSize);

        return rb_funcall2(self, id_plus, 1, &rbOffset);
    }

__copy_from__(p1, p2) click to toggle source

    static VALUE
    memory_copy_from(VALUE self, VALUE rbsrc, VALUE rblen)
    {
        AbstractMemory* dst;

        Data_Get_Struct(self, AbstractMemory, dst);

        memcpy(dst->address, rbffi_AbstractMemory_Cast(rbsrc, rbffi_AbstractMemoryClass)->address, NUM2INT(rblen));

        return self;
    }

clear click to toggle source
Set the memory to all-zero.
@return [self]

    static VALUE
    memory_clear(VALUE self)
    {
        AbstractMemory* ptr = MEMORY(self);
        memset(ptr->address, 0, ptr->size);
        return self;
    }

get_array_of_double(p1, p2) click to toggle source
Alias for: get_array_of_float64
get_array_of_float(p1, p2) click to toggle source
Alias for: get_array_of_float32
get_array_of_float32(offset, length) click to toggle source
@param [Numeric] offset
@param [Numeric] length number of Float to get
@return [Array<Float>]
Get 32-bit floats in memory from offset +offset+ (alias: #get_array_of_float).
Also aliased as: get_array_of_float
get_array_of_float64(offset, length) click to toggle source
@param [Numeric] offset
@param [Numeric] length number of Float to get
@return [Array<Float>]
Get 64-bit floats (doubles) in memory from offset +offset+ (alias: #get_array_of_double).
Also aliased as: get_array_of_double
get_array_of_pointer(offset, length) click to toggle source
@param [Numeric] offset
@param [Numeric] length
@return [Array<Pointer>]
Get an array of {Pointer} of length +length+ from +offset+.
get_array_of_string(offset, count=nil) click to toggle source
Return an array of strings contained in memory.
@param [Numeric] offset point in memory to start from
@param [Numeric] count number of strings to get. If nil, return all strings
@return [Array<String>]
@raise {IndexError} if +offset+ is too great
@raise {NullPointerError} if memory not initialized

    static VALUE
    memory_get_array_of_string(int argc, VALUE* argv, VALUE self)
    {
        VALUE offset = Qnil, countnum = Qnil, retVal = Qnil;
        AbstractMemory* ptr;
        long off;
        int count;

        rb_scan_args(argc, argv, "11", &offset, &countnum);
        off = NUM2LONG(offset);
        count = (countnum == Qnil ? 0 : NUM2INT(countnum));
        retVal = rb_ary_new2(count);

        Data_Get_Struct(self, AbstractMemory, ptr);
        checkRead(ptr);

        if (countnum != Qnil) {
            int i;

            checkBounds(ptr, off, count * sizeof (char*));

            for (i = 0; i < count; ++i) {
                const char* strptr = *((const char**) (ptr->address + off) + i);
                rb_ary_push(retVal, (strptr == NULL ? Qnil : rb_tainted_str_new2(strptr)));
            }

        } else {
            checkBounds(ptr, off, sizeof (char*));
            for ( ; off < ptr->size - (long) sizeof (void *); off += (long) sizeof (void *)) {
                const char* strptr = *(const char**) (ptr->address + off);
                if (strptr == NULL) {
                    break;
                }
                rb_ary_push(retVal, rb_tainted_str_new2(strptr));
            }
        }

        return retVal;
    }

get_bytes(offset, length) click to toggle source
Return string contained in memory.
@param [Numeric] offset point in buffer to start from
@param [Numeric] length string's length in bytes.
@return [String]
@raise {IndexError} if +length+ is too great
@raise {NullPointerError} if memory not initialized

    static VALUE
    memory_get_bytes(VALUE self, VALUE offset, VALUE length)
    {
        AbstractMemory* ptr = MEMORY(self);
        long off, len;

        off = NUM2LONG(offset);
        len = NUM2LONG(length);

        checkRead(ptr);
        checkBounds(ptr, off, len);

        return rb_tainted_str_new((char *) ptr->address + off, len);
    }

get_double(p1) click to toggle source
Alias for: get_float64
get_float(p1) click to toggle source
Alias for: get_float32
get_float32(offset) click to toggle source
@param [Numeric] offset
@return [Float]
Get a 32-bit float from memory at offset +offset+ (alias: #get_float).
Also aliased as: get_float
get_float64(offset) click to toggle source
@param [Numeric] offset
@return [Float]
Get a 64-bit float (double) from memory at offset +offset+ (alias: #get_double).
Also aliased as: get_double
get_pointer(offset) click to toggle source
@param [Numeric] offset
@return [Pointer]
Get a {Pointer} to the memory from +offset+.
get_string(offset, length=nil) click to toggle source
Return string contained in memory.
@param [Numeric] offset point in buffer to start from
@param [Numeric] length string's length in bytes. If nil, a (memory size - offset) length string is returned).
@return [String]
@raise {IndexError} if +length+ is too great
@raise {NullPointerError} if memory not initialized

    static VALUE
    memory_get_string(int argc, VALUE* argv, VALUE self)
    {
        VALUE length = Qnil, offset = Qnil;
        AbstractMemory* ptr = MEMORY(self);
        long off, len;
        char* end;
        int nargs = rb_scan_args(argc, argv, "11", &offset, &length);

        off = NUM2LONG(offset);
        len = nargs > 1 && length != Qnil ? NUM2LONG(length) : (ptr->size - off);
        checkRead(ptr);
        checkBounds(ptr, off, len);

        end = memchr(ptr->address + off, 0, len);
        return rb_tainted_str_new((char *) ptr->address + off,
                (end != NULL ? end - ptr->address - off : len));
    }

put_" #name, "put_" #old); \ rb_define_alias(classMemory, "get_" #name, "get_" #old); \ rb_define_alias(classMemory, "put_u" #name, "put_u" #old); \ rb_define_alias(classMemory, "get_u" #name, "get_u" #old); \ rb_define_alias(classMemory, "write_" #name, "write_" #old); \ rb_define_alias(classMemory, "read_" #name, "read_" #old); \ rb_define_alias(classMemory, "write_u" #name, "write_u" #old); \ rb_define_alias(classMemory, "read_u" #name, "read_u" #old); \ rb_define_alias(classMemory, "put_array_of_" #name, "put_array_of_" #old); \ rb_define_alias(classMemory, "get_array_of_" #name, "get_array_of_" #old); \ rb_define_alias(classMemory, "put_array_of_u" #name, "put_array_of_u" #old); \ rb_define_alias(classMemory, "get_array_of_u" #name, "get_array_of_u" #old); \ rb_define_alias(classMemory, "write_array_of_" #name, "write_array_of_" #old); \ rb_define_alias(classMemory, "read_array_of_" #name, "read_array_of_" #old); \ rb_define_alias(classMemory, "write_array_of_u" #name, "write_array_of_u" #old); \ rb_define_alias(classMemory, "read_array_of_u" #name, "read_array_of_u" #old); ALIAS(char, int8); ALIAS(short, int16); ALIAS(int, int32); ALIAS(long_long, int64); /* * Document-method: put_float32 * call-seq: memory.put_float32offset, value) * @param [Numeric] offset * @param [Numeric] value * @return [self] * Put +value+ as a 32-bit float in memory at offset +offset+ (alias: #put_float). */ rb_define_method(classMemory, "put_float32", memory_put_float32, 2); /* * Document-method: get_float32 * call-seq: memory.get_float32(offset) * @param [Numeric] offset * @return [Float] * Get a 32-bit float from memory at offset +offset+ (alias: #get_float). */ rb_define_method(classMemory, "get_float32", memory_get_float32, 1); rb_define_alias(classMemory, "put_float(p1, p2) click to toggle source
Alias for: put_float32
put_array_of_double(p1, p2) click to toggle source
Alias for: put_array_of_float64
put_array_of_float(p1, p2) click to toggle source
Alias for: put_array_of_float32
put_array_of_float32(offset, ary) click to toggle source
@param [Numeric] offset
@param [Array<Numeric>] ary
@return [self]
Put values from +ary+ as 32-bit floats in memory from offset +offset+ (alias: #put_array_of_float).
Also aliased as: put_array_of_float
put_array_of_float64(offset, ary) click to toggle source
@param [Numeric] offset
@param [Array<Numeric>] ary
@return [self]
Put values from +ary+ as 64-bit floats (doubles) in memory from offset +offset+ (alias: #put_array_of_double).
Also aliased as: put_array_of_double
put_array_of_pointer(offset, ary) click to toggle source
@param [Numeric] offset
@param [Array<#to_ptr>] ary
@return [self]
Put an array of {Pointer} into memory from +offset+.
put_bytes(offset, str, index=0, length=nil) click to toggle source
Put a string in memory.
@param [Numeric] offset point in buffer to start from
@param [String] str string to put to memory
@param [Numeric] index
@param [Numeric] length string's length in bytes. If nil, a (memory size - offset) length string is returned).
@return [self]
@raise {IndexError} if +length+ is too great
@raise {NullPointerError} if memory not initialized
@raise {RangeError} if +index+ is negative, or if index+length is greater than size of string
@raise {SecurityError} when writing unsafe string to memory

    static VALUE
    memory_put_bytes(int argc, VALUE* argv, VALUE self)
    {
        AbstractMemory* ptr = MEMORY(self);
        VALUE offset = Qnil, str = Qnil, rbIndex = Qnil, rbLength = Qnil;
        long off, len, idx;
        int nargs = rb_scan_args(argc, argv, "22", &offset, &str, &rbIndex, &rbLength);

        Check_Type(str, T_STRING);

        off = NUM2LONG(offset);
        idx = nargs > 2 ? NUM2LONG(rbIndex) : 0;
        if (idx < 0) {
            rb_raise(rb_eRangeError, "index canot be less than zero");
            return Qnil;
        }
        len = nargs > 3 ? NUM2LONG(rbLength) : (RSTRING_LEN(str) - idx);
        if ((idx + len) > RSTRING_LEN(str)) {
            rb_raise(rb_eRangeError, "index+length is greater than size of string");
            return Qnil;
        }

        checkWrite(ptr);
        checkBounds(ptr, off, len);

        if (rb_safe_level() >= 1 && OBJ_TAINTED(str)) {
            rb_raise(rb_eSecurityError, "Writing unsafe string to memory");
            return Qnil;
        }
        memcpy(ptr->address + off, RSTRING_PTR(str) + idx, len);

        return self;
    }

put_double(p1, p2) click to toggle source
Alias for: put_float64
put_float32offset, value) click to toggle source
@param [Numeric] offset
@param [Numeric] value
@return [self]
Put +value+ as a 32-bit float in memory at offset +offset+ (alias: #put_float).
Also aliased as: put_" #name, "put_" #old); \ rb_define_alias(classMemory, "get_" #name, "get_" #old); \ rb_define_alias(classMemory, "put_u" #name, "put_u" #old); \ rb_define_alias(classMemory, "get_u" #name, "get_u" #old); \ rb_define_alias(classMemory, "write_" #name, "write_" #old); \ rb_define_alias(classMemory, "read_" #name, "read_" #old); \ rb_define_alias(classMemory, "write_u" #name, "write_u" #old); \ rb_define_alias(classMemory, "read_u" #name, "read_u" #old); \ rb_define_alias(classMemory, "put_array_of_" #name, "put_array_of_" #old); \ rb_define_alias(classMemory, "get_array_of_" #name, "get_array_of_" #old); \ rb_define_alias(classMemory, "put_array_of_u" #name, "put_array_of_u" #old); \ rb_define_alias(classMemory, "get_array_of_u" #name, "get_array_of_u" #old); \ rb_define_alias(classMemory, "write_array_of_" #name, "write_array_of_" #old); \ rb_define_alias(classMemory, "read_array_of_" #name, "read_array_of_" #old); \ rb_define_alias(classMemory, "write_array_of_u" #name, "write_array_of_u" #old); \ rb_define_alias(classMemory, "read_array_of_u" #name, "read_array_of_u" #old); ALIAS(char, int8); ALIAS(short, int16); ALIAS(int, int32); ALIAS(long_long, int64); /* * Document-method: put_float32 * call-seq: memory.put_float32offset, value) * @param [Numeric] offset * @param [Numeric] value * @return [self] * Put +value+ as a 32-bit float in memory at offset +offset+ (alias: #put_float). */ rb_define_method(classMemory, "put_float32", memory_put_float32, 2); /* * Document-method: get_float32 * call-seq: memory.get_float32(offset) * @param [Numeric] offset * @return [Float] * Get a 32-bit float from memory at offset +offset+ (alias: #get_float). */ rb_define_method(classMemory, "get_float32", memory_get_float32, 1); rb_define_alias(classMemory, "put_float
put_float64(offset, value) click to toggle source
@param [Numeric] offset
@param [Numeric] value
@return [self]
Put +value+ as a 64-bit float (double) in memory at offset +offset+ (alias: #put_double).
Also aliased as: put_double
put_pointer(offset, value) click to toggle source
@param [Numeric] offset
@param [nil,Pointer, Integer, #to_ptr] value
@return [self]
Put +value+ in memory from +offset+..
put_string(offset, str) click to toggle source
@param [Numeric] offset
@param [String] str
@return [self]
@raise {SecurityError} when writing unsafe string to memory
@raise {IndexError} if +offset+ is too great
@raise {NullPointerError} if memory not initialized
Put a string in memory.

    static VALUE
    memory_put_string(VALUE self, VALUE offset, VALUE str)
    {
        AbstractMemory* ptr = MEMORY(self);
        long off, len;

        Check_Type(str, T_STRING);
        off = NUM2LONG(offset);
        len = RSTRING_LEN(str);

        checkWrite(ptr);
        checkBounds(ptr, off, len + 1);

        memcpy(ptr->address + off, RSTRING_PTR(str), len);
        *((char *) ptr->address + off + len) = '\0';

        return self;
    }

read_array_of_double(length) click to toggle source
@param [Numeric] length number of Float to read
@return [Array<Float>]
Read 64-bit floats (doubles) from memory.

Same as:

    memory.get_array_of_double(0, ary)

read_array_of_float(length) click to toggle source
@param [Numeric] length number of Float to read
@return [Array<Float>]
Read 32-bit floats from memory.

Same as:

    memory.get_array_of_float(0, ary)

read_array_of_pointer(length) click to toggle source
@param [Numeric] length
@return [Array<Pointer>]
Read an array of {Pointer} of length +length+.

Same as:

    memory.get_array_of_pointer(0, length)

read_bytes(length) click to toggle source
@param [Numeric] length of string to return
@return [String]
equivalent to :
get_bytes(0, length)

    static VALUE
    memory_read_bytes(VALUE self, VALUE length)
    {
        return memory_get_bytes(self, INT2FIX(0), length);
    }

read_double click to toggle source
@return [Float]
Read a 64-bit float (double) from memory.

Same as:

    memory.get_double(0)

read_float click to toggle source
@return [Float]
Read a 32-bit float from memory.

Same as:

    memory.get_float(0)

read_pointer click to toggle source
@return [Pointer]
Get a {Pointer} to the memory from base address.

Equivalent to:

    memory.get_pointer(0)

size() click to toggle source
Alias for: total
size click to toggle source
Return memory size in bytes (alias: #total)
@return [Numeric]

    static VALUE
    memory_size(VALUE self)
    {
        AbstractMemory* ptr;

        Data_Get_Struct(self, AbstractMemory, ptr);

        return LONG2NUM(ptr->size);
    }

Also aliased as: size
type_size click to toggle source
@return [Numeric] type size in bytes
Get the memory's type size.

    static VALUE
    memory_type_size(VALUE self)
    {
        AbstractMemory* ptr;

        Data_Get_Struct(self, AbstractMemory, ptr);

        return INT2NUM(ptr->typeSize);
    }

write_array_of_double(ary) click to toggle source
@param [Array<Numeric>] ary
@return [self]
Write values from +ary+ as 64-bit floats (doubles) in memory.

Same as:

    memory.put_array_of_double(0, ary)

write_array_of_float(ary) click to toggle source
@param [Array<Numeric>] ary
@return [self]
Write values from +ary+ as 32-bit floats in memory.

Same as:

    memory.put_array_of_float(0, ary)

write_array_of_pointer(ary) click to toggle source
@param [Array<#to_ptr>] ary
@return [self]
Write an array of {Pointer} into memory from +offset+.

Same as :

    memory.put_array_of_pointer(0, ary)

write_bytes(str, index=0, length=nil) click to toggle source
@param [String] str string to put to memory
@param [Numeric] index
@param [Numeric] length string's length in bytes. If nil, a (memory size - offset) length string is returned).
@return [self]
equivalent to :
put_bytes(0, str, index, length)

    static VALUE
    memory_write_bytes(int argc, VALUE* argv, VALUE self)
    {
        VALUE* wargv = ALLOCA_N(VALUE, argc + 1);
        int i;

        wargv[0] = INT2FIX(0);
        for (i = 0; i < argc; i++) {
            wargv[i + 1] = argv[i];
        }

        return memory_put_bytes(argc + 1, wargv, self);
    }

write_double(value) click to toggle source
@param [Numeric] value
@return [self]
Write +value+ as a 64-bit float (double) in memory.

Same as:

    memory.put_double(0, value)

write_float(value) click to toggle source
@param [Numeric] value
@return [self]
Write +value+ as a 32-bit float in memory.

Same as:

    memory.put_float(0, value)

write_pointer(value) click to toggle source
@param [nil,Pointer, Integer, #to_ptr] value
@return [self]
Write +value+ in memory.

Equivalent to:

    memory.put_pointer(0, value)

[Validate]

Generated with the Darkfish Rdoc Generator 2.