diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-04-25 23:32:23 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 1b68fdcf5aebf2bc53bbd9234c77aea243c0decd (patch) | |
tree | 1a26f5b5ab32e6b435fa2afca789a5b846d11f46 | |
parent | 7f524c427597cc998f243769b0e22e4f450c55cf (diff) |
Fix scoped allocator deinit crash with buffers
-rw-r--r-- | include/std/buffer.h | 3 | ||||
-rw-r--r-- | include/std/scoped_allocator.h | 4 | ||||
-rw-r--r-- | src/std/buffer.c | 12 | ||||
-rw-r--r-- | src/std/scoped_allocator.c | 25 | ||||
-rw-r--r-- | tests/main.amal.z | 4 |
5 files changed, 30 insertions, 18 deletions
diff --git a/include/std/buffer.h b/include/std/buffer.h index aa82bcc..af6b986 100644 --- a/include/std/buffer.h +++ b/include/std/buffer.h @@ -12,6 +12,9 @@ typedef struct { char* data; usize size; usize capacity; + + ScopedAllocator *allocator; + usize allocator_index; } Buffer; CHECK_RESULT int buffer_init(Buffer *self, struct ScopedAllocator *allocator); diff --git a/include/std/scoped_allocator.h b/include/std/scoped_allocator.h index d7ec1e1..60abcce 100644 --- a/include/std/scoped_allocator.h +++ b/include/std/scoped_allocator.h @@ -15,9 +15,9 @@ struct ScopedAllocatorNode { struct ScopedAllocator { ScopedAllocatorNode head; ScopedAllocatorNode *current; + Buffer/*<void*>*/ mems; /* TODO: Use linked-list instead, since the elements are dynamically allocated anyways */ /* Find another way to store mutexes */ - Buffer/*<Buffer*>*/ buffers; Buffer/*<amal_mutex*>*/ mutexes; }; @@ -27,7 +27,7 @@ void scoped_allocator_node_deinit(ScopedAllocatorNode *self); CHECK_RESULT int scoped_allocator_init(ScopedAllocator *self); void scoped_allocator_deinit(ScopedAllocator *self); CHECK_RESULT int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem); -CHECK_RESULT int scoped_allocator_add_buffer(ScopedAllocator *self, Buffer *buffer); +CHECK_RESULT int scoped_allocator_add_mem(ScopedAllocator *self, usize *result_index); CHECK_RESULT int scoped_allocator_create_mutex(ScopedAllocator *self, amal_mutex **mutex); #endif 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) { diff --git a/tests/main.amal.z b/tests/main.amal.z index 8095a3a..73d9dae 100644 --- a/tests/main.amal.z +++ b/tests/main.amal.z @@ -12,7 +12,7 @@ i64 r3 = 356; PUSH r3 *** f64 r4 = 13.370000; PUSH r4 *** -r5 = CALL 0x7f40f8002610 *** +r5 = CALL 0x621000010bc8 *** i64 r6 = 23232; i64 r7 = r6; i64 r8 = 30; @@ -30,7 +30,7 @@ r19 = r12 * r18; i64 r20 = r19; const char* r21 = "lole"; PUSH r21 *** -r22 = CALL 0x55c96755d568 *** +r22 = CALL 0x621000006948 *** } void f2() { } |