Class: FFI::Buffer

Inherits:
AbstractMemoryClass
  • Object
show all
Defined in:
ext/ffi_c/Buffer.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/ffi_c/Buffer.c', line 74

static VALUE
buffer_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE rbSize = Qnil, rbCount = Qnil, rbClear = Qnil;
    Buffer* p;
    int nargs;

    Data_Get_Struct(self, Buffer, p);

    nargs = rb_scan_args(argc, argv, "12", &rbSize, &rbCount, &rbClear);
    p->memory.typeSize = rbffi_type_size(rbSize);
    p->memory.size = p->memory.typeSize * (nargs > 1 ? NUM2LONG(rbCount) : 1);

    p->storage = xmalloc(p->memory.size + 7);
    if (p->storage == NULL) {
        rb_raise(rb_eNoMemError, "Failed to allocate memory size=%lu bytes", p->memory.size);
    }

    /* ensure the memory is aligned on at least a 8 byte boundary */
    p->memory.address = (void *) (((uintptr_t) p->storage + 0x7) & (uintptr_t) ~0x7UL);
    
    if (nargs > 2 && (RTEST(rbClear) || rbClear == Qnil) && p->memory.size > 0) {
        memset(p->memory.address, 0, p->memory.size);
    }

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

    return self;
}

Class Method Details

.alloc_in(*args) ⇒ Object



106
107
108
109
110
# File 'ext/ffi_c/Buffer.c', line 106

static VALUE
buffer_alloc_inout(int argc, VALUE* argv, VALUE klass)
{
    return buffer_initialize(argc, argv, buffer_allocate(klass));
}

.alloc_inout(*args) ⇒ Object



106
107
108
109
110
# File 'ext/ffi_c/Buffer.c', line 106

static VALUE
buffer_alloc_inout(int argc, VALUE* argv, VALUE klass)
{
    return buffer_initialize(argc, argv, buffer_allocate(klass));
}

.alloc_out(*args) ⇒ Object



106
107
108
109
110
# File 'ext/ffi_c/Buffer.c', line 106

static VALUE
buffer_alloc_inout(int argc, VALUE* argv, VALUE klass)
{
    return buffer_initialize(argc, argv, buffer_allocate(klass));
}

Instance Method Details

#+(rbOffset) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/ffi_c/Buffer.c', line 112

static VALUE
buffer_plus(VALUE self, VALUE rbOffset)
{
    Buffer* ptr;
    Buffer* result;
    VALUE obj = Qnil;
    long offset = NUM2LONG(rbOffset);

    Data_Get_Struct(self, Buffer, ptr);
    checkBounds(&ptr->memory, offset, 1);

    obj = Data_Make_Struct(BufferClass, Buffer, buffer_mark, -1, result);
    result->memory.address = ptr->memory.address + offset;
    result->memory.size = ptr->memory.size - offset;
    result->memory.ops = ptr->memory.ops;
    result->memory.access = ptr->memory.access;
    result->memory.typeSize = ptr->memory.typeSize;
    result->rbParent = self;

    return obj;
}

#inspectObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/ffi_c/Buffer.c', line 134

static VALUE
buffer_inspect(VALUE self)
{
    char tmp[100];
    Buffer* ptr;

    Data_Get_Struct(self, Buffer, ptr);

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