Class: Nuklear::Buffer

Inherits:
Object
  • Object
show all
Defined in:
ext/nuklear/nkrb_buffer.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*args) ⇒ Object



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;
}

Instance Method Details

#sizeObject



57
58
59
60
61
# File 'ext/nuklear/nkrb_buffer.c', line 57

VALUE nkrb_buffer_size(VALUE self) {
  struct nk_buffer *buf = NULL;
  Data_Get_Struct(self, struct nk_buffer, buf);
  return ULONG2NUM(nk_buffer_total(buf));
}

#to_ptrObject



63
64
65
66
67
68
69
70
71
72
# File 'ext/nuklear/nkrb_buffer.c', line 63

VALUE nkrb_buffer_to_ptr(VALUE self) {
  struct nk_buffer *buf = NULL;
  Data_Get_Struct(self, struct nk_buffer, buf);
  VALUE cFiddle = rb_const_get(rb_cObject, rb_intern("Fiddle"));
  VALUE cPointer = rb_const_get(cFiddle, rb_intern("Pointer"));
  VALUE fiddleptr = rb_funcall(cPointer, rb_intern("new"), 2,
                               ULONG2NUM((unsigned long) nk_buffer_memory(buf)),
                               nkrb_buffer_size(self));
  return fiddleptr;
}