aboutsummaryrefslogtreecommitdiff
path: root/src/buffer.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-02-24 17:53:46 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commita27a3e6ae09e8396584c95a3567b145e86d8bc40 (patch)
tree6b56721fb64f31e4b731e75942f81838d10b0612 /src/buffer.c
parenta307f17f44b461f58441926fcbf87883f17ebe61 (diff)
Fixed CHECK_RESULT macro, use scoped allocator
Scoped allocator gives us better performance and cleanup code for error cases is much cleaner
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c28
1 files changed, 14 insertions, 14 deletions
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 <assert.h>
-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;
}