From a27a3e6ae09e8396584c95a3567b145e86d8bc40 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 24 Feb 2019 17:53:46 +0100 Subject: Fixed CHECK_RESULT macro, use scoped allocator Scoped allocator gives us better performance and cleanup code for error cases is much cleaner --- src/buffer.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c index 972e23a..490418a 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -3,12 +3,14 @@ #include "../include/mem.h" #include -void buffer_init(Buffer *self) { - self->data = NULL; +int buffer_init(Buffer *self, ScopedAllocator *allocator, usize initial_capacity) { + self->allocator = allocator; self->size = 0; - self->capacity = 0; + self->capacity = initial_capacity; + return scoped_allocator_alloc(self->allocator, initial_capacity, (void**)&self->data); } +<<<<<<< HEAD void buffer_deinit(Buffer *self) { am_free(self->data); self->data = NULL; @@ -18,6 +20,9 @@ void buffer_deinit(Buffer *self) { static CHECK_RESULT int buffer_ensure_capacity_for(Buffer *self, usize size) { usize capacity; +======= +static CHECK_RESULT int buffer_ensure_capacity_for(Buffer *self, usize size) { +>>>>>>> Fixed CHECK_RESULT macro, use scoped allocator void *new_mem; int alloc_result; usize new_capacity; @@ -26,21 +31,16 @@ static CHECK_RESULT int buffer_ensure_capacity_for(Buffer *self, usize size) { if(self->capacity >= new_capacity) return BUFFER_OK; - capacity = self->capacity; - if(capacity == 0) { - capacity = new_capacity; - } else { - while(capacity < new_capacity) { - capacity *= 1.5; - } - } - - alloc_result = am_realloc(self->data, capacity, &new_mem); + /* + We are using alloc here instead of realloc because we are using scoped allocator + which doesn't reallocate. TODO: Verify if scoped allocator node size is enough to hold buffers... + */ + alloc_result = scoped_allocator_alloc(self->allocator, new_capacity, &new_mem); if(alloc_result != ALLOC_OK) return BUFFER_ALLOC_FAIL; self->data = new_mem; - self->capacity = capacity; + self->capacity = new_capacity; return BUFFER_OK; } -- cgit v1.2.3