24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'ext/nuklear/nkrb_buffer.c', line 24
VALUE nkrb_buffer_new(int argc, VALUE* argv, VALUE module) {
VALUE rsize = Qnil;
VALUE rptr = Qnil;
rb_scan_args(argc, argv, "02", &rsize, &rptr); // optional rsize, rptr
void (*freefn)(struct nk_buffer *buf) = NULL;
struct nk_buffer *buf = malloc(sizeof(struct nk_buffer));
nk_size size = 0; // if size is 0, the buffer will be dynamic.
if (!NIL_P(rsize)) size = (nk_size) NUM2INT(rsize);
if (size) {
void *mem = NULL;
if (!NIL_P(rptr)) {
// we were passed a pointer; we will treat it as a 64 bit unsigned in
// ruby, and a void * in c. We will not zero the user-provided buffer.
mem = (void *) NUM2ULL(rptr);
freefn = nkrb_buffer_managed_free;
} else {
// we were not passed a pointer; we will allocate a buffer of the
// requested size and use that. We will zero the uninitialized memory.
mem = malloc(size);
memset(mem, 0, size);
freefn = nkrb_buffer_fixed_free;
}
nk_buffer_init_fixed(buf, mem, size);
} else {
nk_buffer_init_default(buf);
freefn = nkrb_buffer_dynamic_free;
}
VALUE result = Data_Wrap_Struct(cNuklearBuffer, NULL, freefn, buf);
return result;
}
|