Class: Minibwa::Buffer

Inherits:
Object
  • Object
show all
Defined in:
ext/minibwa/mb_buffer.c,
ext/minibwa/mb_buffer.c

Overview

Minibwa::Buffer -- wraps mb_tbuf_t, the reusable per-thread scratch buffer.

Passing a Buffer to Index#map saves an mb_tbuf_init/mb_tbuf_destroy pair on every call; passing nil lets mb_map() allocate one internally. A Buffer carries mutable working state and must never be shared between threads that map concurrently -- one Buffer per thread.

Freed with mb_tbuf_destroy().

Instance Method Summary collapse

Constructor Details

#new(no_kalloc: false) ⇒ Buffer

Allocates a new thread-local scratch buffer. Set no_kalloc: to true to disable the internal allocator.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/minibwa/mb_buffer.c', line 52

static VALUE
rb_minibwa_buffer_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE kwargs;
    rb_scan_args(argc, argv, ":", &kwargs);

    int no_kalloc = 0;
    if (!NIL_P(kwargs)) {
        VALUE v = rb_hash_aref(kwargs, ID2SYM(rb_intern("no_kalloc")));
        if (RTEST(v)) no_kalloc = 1;
    }

    /* The buffer was allocated by alloc with the default (kalloc enabled).
     * If the caller wants no_kalloc, rebuild it through the public API. */
    if (no_kalloc) {
        mb_tbuf_t *b = rb_minibwa_get_tbuf(self);
        if (b) mb_tbuf_destroy(b);
        b = mb_tbuf_init(1);
        if (!b) rb_raise(rb_eMinibwaError, "mb_tbuf_init failed");
        DATA_PTR(self) = b;
    }
    return Qnil;
}

Instance Method Details

#reset!(max_block_size) ⇒ Integer

Resets the buffer for reuse, optionally setting a new maximum block size. Returns the new capacity.

Returns:

  • (Integer)


91
92
93
94
95
96
97
98
# File 'ext/minibwa/mb_buffer.c', line 91

static VALUE
rb_minibwa_buffer_reset(VALUE self, VALUE max_block_size)
{
    mb_tbuf_t *b = rb_minibwa_get_tbuf(self);
    int64_t mbs = NUM2LL(max_block_size);
    int32_t cap = mb_tbuf_reset(b, mbs);
    return INT2NUM(cap);
}