Class: FFI::MemoryPointer

Inherits:
Pointer
  • Object
show all
Defined in:
lib/ffi/memorypointer.rb,
ext/ffi_c/MemoryPointer.c

Constant Summary

Constants inherited from Pointer

Pointer::NULL, Pointer::SIZE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pointer

#+, #==, #address, #null?, #read_array_of_int, #read_array_of_long, #read_array_of_pointer, #read_array_of_type, #read_float, #read_int, #read_long, #read_long_long, #read_pointer, #read_string, #read_string_length, #read_string_to_null, size, #write_array_of_int, #write_array_of_long, #write_array_of_pointer, #write_array_of_type, #write_float, #write_int, #write_long, #write_long_long, #write_pointer, #write_string, #write_string_length

Constructor Details

#initialize(*args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'ext/ffi_c/MemoryPointer.c', line 72

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) ⇒ Object

def self.new(type, count=nil, clear=true)

  size = if type.kind_of? Fixnum
    type
  elsif type.kind_of? Symbol
    FFI.type_size(type)
  else
    type.size
  end
  ptr = self.__allocate(size, count, clear)
  ptr.type_size = size
  if block_given?
    begin
      value = yield ptr
    ensure
      ptr.free
    end
    value
  else
    ptr
  end
end


47
48
49
50
51
# File 'lib/ffi/memorypointer.rb', line 47

def self.from_string(s)
  ptr = self.new(s.length + 1, 1, false)
  ptr.put_string(0, s)
  ptr
end

Instance Method Details

#autorelease=(autorelease) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'ext/ffi_c/MemoryPointer.c', line 146

static VALUE
memptr_autorelease(VALUE self, VALUE autorelease)
{
    MemoryPointer* ptr;

    Data_Get_Struct(self, MemoryPointer, ptr);
    ptr->autorelease = autorelease == Qtrue;

    return autorelease;
}

#freeObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/ffi_c/MemoryPointer.c', line 128

static VALUE
memptr_free(VALUE self)
{
    MemoryPointer* ptr;

    Data_Get_Struct(self, MemoryPointer, ptr);

    if (ptr->allocated) {
        if (ptr->storage != NULL) {
            free(ptr->storage);
            ptr->storage = NULL;
        }
        ptr->allocated = false;
    }

    return self;
}

#inspectObject



116
117
118
119
120
121
122
123
124
125
126
# File 'ext/ffi_c/MemoryPointer.c', line 116

static VALUE
memptr_inspect(VALUE self)
{
    MemoryPointer* ptr;
    char tmp[100];

    Data_Get_Struct(self, MemoryPointer, ptr);

    snprintf(tmp, sizeof(tmp), "#<FFI::MemoryPointer address=%p size=%lu>", ptr->memory.address, ptr->memory.size);
    return rb_str_new2(tmp);
}