ext/say_buffer_slice.c in ray-0.1.1 vs ext/say_buffer_slice.c in ray-0.2.0
- old
+ new
@@ -11,153 +11,148 @@
#define say_make_range(begin, size) ((say_range){begin, size})
typedef struct {
say_buffer *buf;
- say_array *ranges;
+ mo_list *ranges;
} say_global_buffer;
-static say_array *say_global_buffers = NULL;
+static mo_array *say_global_buffers = NULL;
static void say_global_buffer_free(say_global_buffer *buf) {
say_buffer_free(buf->buf);
- say_array_free(buf->ranges);
+ if (buf->ranges) mo_list_free(buf->ranges);
}
-static say_global_buffer *say_global_buffer_create(say_array *bufs,
+static say_global_buffer *say_global_buffer_create(mo_array *bufs,
size_t vtype, size_t size) {
say_global_buffer buffer;
buffer.buf = say_buffer_create(vtype, SAY_STREAM, size);
- buffer.ranges = say_array_create(sizeof(say_range), NULL, NULL);
+ buffer.ranges = NULL;
- say_array_push(bufs, &buffer);
+ mo_array_push(bufs, &buffer);
- return say_array_get(bufs, say_array_get_size(bufs) - 1);
+ return mo_array_at(bufs, bufs->size - 1);
}
-static void say_global_buffer_array_alloc(say_array **ary) {
- *ary = say_array_create(sizeof(say_global_buffer),
- (say_destructor)say_global_buffer_free,
- NULL);
+static void say_global_buffer_array_alloc(mo_array *ary) {
+ mo_array_init(ary, sizeof(say_global_buffer));
+ ary->release = (say_destructor)say_global_buffer_free;
}
-static void say_global_buffer_array_free(say_array **ary) {
- if (*ary)
- say_array_free(*ary);
+static void say_global_buffer_array_free(mo_array *ary) {
+ mo_array_release(ary);
}
-static size_t say_global_buffer_add_range_before(say_global_buffer *buf,
- size_t before, size_t n) {
- say_range tmp = say_make_range(0, n);
- say_array_insert(buf->ranges, before, &tmp);
+static size_t say_global_buffer_prepend(say_global_buffer *buf, size_t n) {
+ say_range range = say_make_range(0, n);
+ buf->ranges = mo_list_prepend(buf->ranges, &range);
+ return 0;
+}
- say_range *range = say_array_get(buf->ranges, before);
+static size_t say_global_buffer_insert(mo_list *list, size_t n) {
+ say_range *previous = mo_list_data_ptr(list, say_range);
- if (before != 0) {
- say_range *prev = say_array_get(buf->ranges, before - 1);
- range->loc = prev->loc + prev->size;
- }
+ say_range range = say_make_range(previous->loc + previous->size, n);
+ mo_list_insert(list, &range);
- return range->loc;
+ return range.loc;
}
static size_t say_global_buffer_find(say_global_buffer *buf, size_t n) {
/* Buffer too small for this object */
if (n > say_buffer_get_size(buf->buf))
return SAY_MAX_SIZE;
- size_t ary_size = say_array_get_size(buf->ranges);
- say_range *first = say_array_get(buf->ranges, 0);
-
- /* There's room at the begin of the buffer */
- if (ary_size == 0 || first->loc >= n) {
- return say_global_buffer_add_range_before(buf, 0, n);
+ if (!buf->ranges) {
+ buf->ranges = mo_list_create(sizeof(say_range));
+ say_range *range = mo_list_data_ptr(buf->ranges, say_range);
+ *range = say_make_range(0, n);
}
- say_range *current = first, *next = NULL;
+ say_range *first = mo_list_data_ptr(buf->ranges, say_range);
- for (size_t i = 0; i < ary_size - 1; i++) {
- next = say_array_get(buf->ranges, i + 1);
+ /* There's room at the begin of the buffer */
+ if (first->loc >= n)
+ return say_global_buffer_prepend(buf, 0);
+ mo_list *it = buf->ranges;
+ for (; it->next; it = it->next) { /* stop before last element */
+ say_range *current = mo_list_data_ptr(it, say_range);
+ say_range *next = mo_list_data_ptr(it->next, say_range);
+
size_t begin = current->loc + current->size;
size_t end = next->loc;
/* There's enough room between those two elements */
- if (end - begin >= n) {
- return say_global_buffer_add_range_before(buf, i + 1, n);
- }
-
- current = next;
+ if (end - begin >= n)
+ return say_global_buffer_insert(it, n);
}
- say_range *last = say_array_get(buf->ranges, ary_size - 1);
+ say_range *last = mo_list_data_ptr(it, say_range);
/* There's enough room at the end of the buffer */
if ((last->loc + last->size + n) < say_buffer_get_size(buf->buf)) {
- return say_global_buffer_add_range_before(buf, ary_size, n);
+ return say_global_buffer_insert(it, n);
}
/* Not enough room here */
return SAY_MAX_SIZE;
}
-static void say_global_buffer_delete_at(say_global_buffer *buf, size_t loc) {
+static void say_global_buffer_delete_at(say_global_buffer *buf, size_t loc,
+ size_t range_size) {
if (!buf)
return;
- size_t n = 0, size = say_array_get_size(buf->ranges);
- for (; n < size; n++) {
- say_range *range = say_array_get(buf->ranges, n);
- if (range->loc == loc)
- break;
- }
+ for (mo_list *it = buf->ranges; it; it = it->next) {
+ say_range *range = mo_list_data_ptr(it, say_range);
+ if (range->loc == loc && range->size == range_size) {
+ mo_list *next = it->next;
+ mo_list_delete(it);
- if (n == size)
- return; /* Element could not be found */
+ if (it == buf->ranges) buf->ranges = next;
- say_array_delete(buf->ranges, n);
+ return;
+ }
+ }
}
static void say_global_buffer_reduce_size(say_global_buffer *buf, size_t loc,
- size_t size) {
- for (say_range *range = say_array_get(buf->ranges, 0); range;
- say_array_next(buf->ranges, (void**)&range)) {
- if (range->loc == loc) {
+ size_t old_size, size_t size) {
+ for (mo_list *it = buf->ranges; it; it = it->next) {
+ say_range *range = mo_list_data_ptr(it, say_range);
+ if (range->loc == loc && range->size == size) {
range->size = size;
return;
}
}
}
-static size_t say_global_buffer_reserve(size_t vtype, size_t size, size_t *ret_id) {
+static size_t say_global_buffer_reserve(size_t vtype, size_t size,
+ size_t *ret_id) {
if (!say_global_buffers) {
- say_global_buffers = say_array_create(sizeof(say_array*),
- (say_destructor)say_global_buffer_array_free,
- (say_creator)say_global_buffer_array_alloc);
+ say_global_buffers = mo_array_create(sizeof(mo_array));
+ say_global_buffers->init = (mo_init)say_global_buffer_array_alloc;
+ say_global_buffers->release = (mo_release)say_global_buffer_array_free;
}
- if (say_array_get_size(say_global_buffers) <= vtype) {
- say_array_resize(say_global_buffers, vtype + 1);
+ if (say_global_buffers->size <= vtype) {
+ mo_array_resize(say_global_buffers, vtype + 1);
}
- say_array *global_bufs = *(say_array**)say_array_get(say_global_buffers,
- vtype);
+ mo_array *global_bufs = mo_array_get_ptr(say_global_buffers, vtype, mo_array);
- size_t i = 0;
- for (say_global_buffer *buf = say_array_get(global_bufs, 0);
- buf;
- say_array_next(global_bufs, (void**)&buf)) {
- size_t res = say_global_buffer_find(buf, size);
+ for (size_t i = 0; i < global_bufs->size; i++) {
+ size_t res = say_global_buffer_find(mo_array_at(global_bufs, i), size);
if (res != SAY_MAX_SIZE) {
*ret_id = i;
return res;
}
-
- i++;
}
/* Existing buffers can't store this object, save it somewhere else */
say_vertex_type *type = say_get_vertex_type(vtype);
@@ -167,20 +162,19 @@
size_t buf_size = normal_size > size ? normal_size : size;
say_global_buffer *buf = say_global_buffer_create(global_bufs,
vtype,
buf_size);
- *ret_id = say_array_get_size(global_bufs) - 1;
+ *ret_id = global_bufs->size - 1;
return say_global_buffer_find(buf, size);
}
static say_global_buffer *say_global_buffer_at(size_t vtype, size_t id) {
if (!say_global_buffers)
return NULL;
- return say_array_get(*(say_array**)say_array_get(say_global_buffers, vtype),
- id);
+ return mo_array_at(mo_array_at(say_global_buffers, vtype), id);
}
say_buffer_slice *say_buffer_slice_create(size_t vtype, size_t size) {
say_buffer_slice *slice = malloc(sizeof(say_buffer_slice));
slice->loc = say_global_buffer_reserve(vtype, size, &slice->buf_id);
@@ -189,11 +183,11 @@
return slice;
}
void say_buffer_slice_free(say_buffer_slice *slice) {
say_global_buffer_delete_at(say_global_buffer_at(slice->vtype, slice->buf_id),
- slice->loc);
+ slice->loc, slice->size);
free(slice);
}
void say_buffer_slice_use(say_buffer_slice *slice) {
say_buffer_bind(say_global_buffer_at(slice->vtype, slice->buf_id)->buf);
@@ -201,18 +195,18 @@
void say_buffer_slice_recreate(say_buffer_slice *slice, size_t size) {
if (size > slice->size) {
say_global_buffer_delete_at(say_global_buffer_at(slice->vtype,
slice->buf_id),
- slice->loc);
+ slice->loc, slice->size);
slice->loc = say_global_buffer_reserve(slice->vtype, size, &slice->buf_id);
}
else {
say_global_buffer_reduce_size(say_global_buffer_at(slice->vtype,
slice->buf_id),
- slice->loc, size);
+ slice->loc, slice->size, size);
}
slice->size = size;
}
@@ -239,9 +233,9 @@
say_buffer_bind(say_global_buffer_at(slice->vtype, slice->buf_id)->buf);
}
void say_buffer_slice_clean_up() {
if (say_global_buffers) {
- say_array_free(say_global_buffers);
+ mo_array_free(say_global_buffers);
say_global_buffers = NULL;
}
}