From 1b68fdcf5aebf2bc53bbd9234c77aea243c0decd Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 25 Apr 2019 23:32:23 +0200 Subject: Fix scoped allocator deinit crash with buffers --- src/std/buffer.c | 12 +++++++++--- src/std/scoped_allocator.c | 25 ++++++++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) (limited to 'src/std') diff --git a/src/std/buffer.c b/src/std/buffer.c index 5fb4b05..ddf7d39 100644 --- a/src/std/buffer.c +++ b/src/std/buffer.c @@ -8,9 +8,13 @@ int buffer_init(Buffer *self, struct ScopedAllocator *allocator) { self->data = NULL; self->size = 0; self->capacity = 0; - if(allocator) - return scoped_allocator_add_buffer(allocator, self); - return 0; + self->allocator = allocator; + if(allocator) { + return scoped_allocator_add_mem(allocator, &self->allocator_index); + } else { + self->allocator_index = ~0ULL; + return 0; + } } static CHECK_RESULT int buffer_ensure_capacity(Buffer *self, usize new_capacity) { @@ -36,6 +40,8 @@ static CHECK_RESULT int buffer_ensure_capacity(Buffer *self, usize new_capacity) self->data = new_mem; self->capacity = capacity; + if(self->allocator) + am_memcpy(self->allocator->mems.data + sizeof(void*) * self->allocator_index, &self->data, sizeof(void*)); return BUFFER_OK; } diff --git a/src/std/scoped_allocator.c b/src/std/scoped_allocator.c index 8b161a1..84ec920 100644 --- a/src/std/scoped_allocator.c +++ b/src/std/scoped_allocator.c @@ -27,7 +27,7 @@ void scoped_allocator_node_deinit(ScopedAllocatorNode *self) { int scoped_allocator_init(ScopedAllocator *self) { return_if_error(scoped_allocator_node_init(&self->head)); self->current = &self->head; - return_if_error(buffer_init(&self->buffers, NULL)); + return_if_error(buffer_init(&self->mems, NULL)); return buffer_init(&self->mutexes, NULL); } @@ -39,15 +39,15 @@ static void buffer_deinit(Buffer *self) { } static void scoped_allocator_deinit_buffers(ScopedAllocator *self) { - Buffer **buffer; - Buffer **buffers_end; - buffer = buffer_begin(&self->buffers); - buffers_end = buffer_end(&self->buffers); - while(buffer != buffers_end) { - buffer_deinit(*buffer); - ++buffer; + void **mem; + void **mems_end; + mem = buffer_begin(&self->mems); + mems_end = buffer_end(&self->mems); + while(mem != mems_end) { + am_free(*mem); + ++mem; } - buffer_deinit(&self->buffers); + buffer_deinit(&self->mems); } static void scoped_allocator_deinit_mutexes(ScopedAllocator *self) { @@ -123,8 +123,11 @@ int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) { return 0; } -int scoped_allocator_add_buffer(ScopedAllocator *self, Buffer *buffer) { - return buffer_append(&self->buffers, &buffer, sizeof(Buffer*)); +int scoped_allocator_add_mem(ScopedAllocator *self, usize *result_index) { + void *null_data; + null_data = NULL; + *result_index = buffer_get_size(&self->mems, sizeof(void*)); + return buffer_append(&self->mems, &null_data, sizeof(void*)); } int scoped_allocator_create_mutex(ScopedAllocator *self, amal_mutex **mutex) { -- cgit v1.2.3