Method: FFI::Buffer#initialize
- Defined in:
- ext/ffi_c/Buffer.c
#initialize(size, count = 1, clear = false) ⇒ self
A new instance of 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;
}
|