Class: FFI::Buffer

Inherits:
AbstractMemory show all
Defined in:
ext/ffi_c/Buffer.c,
ext/ffi_c/Buffer.c

Overview

A Buffer is a function argument type. It should be use with functions playing with C arrays.

Constant Summary

Constants inherited from AbstractMemory

AbstractMemory::LONG_MAX

Class Method Summary collapse

Instance Method Summary collapse

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 = false) ⇒ self

A new instance of Buffer.

Parameters:

  • Type (Integer, Symbol, #size)

    or size in bytes of a buffer cell

  • count (Fixnum)

    number of cell in the Buffer

  • clear (Boolean)

    if true, set the buffer to all-zero

Raises:

  • (NoMemoryError)

    if failed to allocate memory for Buffer



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/ffi_c/Buffer.c', line 123

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

    TypedData_Get_Struct(self, Buffer, &buffer_data_type, 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);

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

        /* ensure the memory is aligned on at least a 8 byte boundary */
        p->memory.address = (void *) (((uintptr_t) p->data.storage + 0x7) & (uintptr_t) ~0x7ULL);

        if (p->memory.size > 0 && (nargs < 3 || RTEST(rbClear))) {
            memset(p->memory.address, 0, p->memory.size);
        }

    } else {
        p->memory.flags |= MEM_EMBED;
        p->memory.address = (void *) &p->data.embed[0];
    }

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

    return self;
}

Class Method Details

.alloc_in(*args) ⇒ Object

Create a new Buffer for in arguments (alias : new_in).



194
195
196
197
198
# File 'ext/ffi_c/Buffer.c', line 194

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

.alloc_inout(*args) ⇒ Object

Create a new Buffer for in and out arguments (alias : new_inout).



194
195
196
197
198
# File 'ext/ffi_c/Buffer.c', line 194

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

.alloc_out(*args) ⇒ Object

Create a new Buffer for out arguments (alias : new_out).



194
195
196
197
198
# File 'ext/ffi_c/Buffer.c', line 194

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

Instance Method Details

#+(offset) ⇒ Buffer

Add a Buffer with an offset

Parameters:

  • offset (Numeric)

Returns:

  • (Buffer)

    a new instance of Buffer pointing from offset until end of previous buffer.



226
227
228
229
230
231
232
233
234
235
# File 'ext/ffi_c/Buffer.c', line 226

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

    TypedData_Get_Struct(self, Buffer, &buffer_data_type, ptr);

    return slice(self, offset, ptr->memory.size - offset);
}

#initialize_copy(other) ⇒ self

DO NOT CALL THIS METHOD.

Returns:

  • (self)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/ffi_c/Buffer.c', line 167

static VALUE
buffer_initialize_copy(VALUE self, VALUE other)
{
    AbstractMemory* src;
    Buffer* dst;

    TypedData_Get_Struct(self, Buffer, &buffer_data_type, dst);
    src = rbffi_AbstractMemory_Cast(other, &buffer_data_type);
    if ((dst->memory.flags & MEM_EMBED) == 0 && dst->data.storage != NULL) {
        xfree(dst->data.storage);
    }
    dst->data.storage = xmalloc(src->size + 7);
    if (dst->data.storage == NULL) {
        rb_raise(rb_eNoMemError, "failed to allocate memory size=%lu bytes", src->size);
        return Qnil;
    }

    dst->memory.address = (void *) (((uintptr_t) dst->data.storage + 0x7) & (uintptr_t) ~0x7ULL);
    dst->memory.size = src->size;
    dst->memory.typeSize = src->typeSize;

    /* finally, copy the actual buffer contents */
    memcpy(dst->memory.address, src->address, src->size);

    return self;
}

#inspectString

Inspect a Buffer.

Returns:

  • (String)


255
256
257
258
259
260
261
262
263
264
265
266
# File 'ext/ffi_c/Buffer.c', line 255

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

    TypedData_Get_Struct(self, Buffer, &buffer_data_type, ptr);

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

    return rb_str_new2(tmp);
}

#order:big, :little #order(order) ⇒ self

Set or get endianness of Buffer.

Overloads:

  • #order:big, :little

    Get endianness of Buffer.

    Returns:

    • (:big, :little)
  • #order(order) ⇒ self

    Set endianness of Buffer (:network is an alias for :big).

    Parameters:

    • order (:big, :little, :network)

    Returns:

    • (self)


285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'ext/ffi_c/Buffer.c', line 285

static VALUE
buffer_order(int argc, VALUE* argv, VALUE self)
{
    Buffer* ptr;

    TypedData_Get_Struct(self, Buffer, &buffer_data_type, ptr);
    if (argc == 0) {
        int order = (ptr->memory.flags & MEM_SWAP) == 0 ? BYTE_ORDER : SWAPPED_ORDER;
        return order == BIG_ENDIAN ? ID2SYM(rb_intern("big")) : ID2SYM(rb_intern("little"));
    } else {
        VALUE rbOrder = Qnil;
        int order = BYTE_ORDER;

        if (rb_scan_args(argc, argv, "1", &rbOrder) < 1) {
            rb_raise(rb_eArgError, "need byte order");
        }
        if (SYMBOL_P(rbOrder)) {
            ID id = SYM2ID(rbOrder);
            if (id == rb_intern("little")) {
                order = LITTLE_ENDIAN;

            } else if (id == rb_intern("big") || id == rb_intern("network")) {
                order = BIG_ENDIAN;
            }
        }
        if (order != BYTE_ORDER) {
            Buffer* p2;
            VALUE retval = slice(self, 0, ptr->memory.size);

            TypedData_Get_Struct(retval, Buffer, &buffer_data_type, p2);
            p2->memory.flags |= MEM_SWAP;
            return retval;
        }

        return self;
    }
}

#slice(offset, length) ⇒ Buffer

Slice an existing Buffer.

Parameters:

  • offset (Numeric)
  • length (Numeric)

Returns:

  • (Buffer)

    a new instance of Buffer



244
245
246
247
248
# File 'ext/ffi_c/Buffer.c', line 244

static VALUE
buffer_slice(VALUE self, VALUE rbOffset, VALUE rbLength)
{
    return slice(self, NUM2LONG(rbOffset), NUM2LONG(rbLength));
}