From 4f308829ad0e81a59971e172284c018cf2bdca3d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 22 Mar 2019 20:48:40 +0100 Subject: Add mutex for lhs expr, add error for missing lhs expr for func, struct TODO: Use mutex in lhs expr and set resolved_type --- src/std/scoped_allocator.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'src/std/scoped_allocator.c') diff --git a/src/std/scoped_allocator.c b/src/std/scoped_allocator.c index c66639a..3104fd3 100644 --- a/src/std/scoped_allocator.c +++ b/src/std/scoped_allocator.c @@ -1,5 +1,6 @@ #include "../../include/std/scoped_allocator.h" #include "../../include/std/alloc.h" +#include "../../include/std/thread.h" #include "../../include/std/log.h" #include @@ -26,7 +27,8 @@ 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 buffer_init(&self->buffers, NULL); + return_if_error(buffer_init(&self->buffers, NULL)); + return buffer_init(&self->mutexes, NULL); } static void buffer_deinit(Buffer *self) { @@ -36,11 +38,9 @@ static void buffer_deinit(Buffer *self) { self->capacity = 0; } -void scoped_allocator_deinit(ScopedAllocator *self) { +static void scoped_allocator_deinit_buffers(ScopedAllocator *self) { Buffer **buffer; Buffer **buffers_end; - - self->current = NULL; buffer = buffer_start(&self->buffers); buffers_end = buffer_end(&self->buffers); while(buffer != buffers_end) { @@ -48,6 +48,24 @@ void scoped_allocator_deinit(ScopedAllocator *self) { ++buffer; } buffer_deinit(&self->buffers); +} + +static void scoped_allocator_deinit_mutexes(ScopedAllocator *self) { + amal_mutex **mutex; + amal_mutex **mutexes_end; + mutex = buffer_start(&self->mutexes); + mutexes_end = buffer_end(&self->mutexes); + while(mutex != mutexes_end) { + amal_mutex_deinit(*mutex); + ++mutex; + } + buffer_deinit(&self->mutexes); +} + +void scoped_allocator_deinit(ScopedAllocator *self) { + self->current = NULL; + scoped_allocator_deinit_buffers(self); + scoped_allocator_deinit_mutexes(self); scoped_allocator_node_deinit(&self->head); } @@ -79,6 +97,8 @@ static usize align_ptr_ceil_offset(void *ptr, uintptr_t alignment) { return (uintptr_t)align_ptr_ceil(ptr, alignment) - (uintptr_t)ptr; } +#define SCOPED_ALLOC_ALIGNMENT 8 + int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) { ScopedAllocatorNode *current; usize alloc_size; @@ -90,14 +110,14 @@ int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) { return -1; } - alloc_size = size + align_ptr_ceil_offset(self->current->data + self->current->size, 16); + alloc_size = size + align_ptr_ceil_offset(self->current->data + self->current->size, SCOPED_ALLOC_ALIGNMENT); return_if_error(scoped_allocator_ensure_capacity_for(self, alloc_size)); /* Reallocated (new node created) */ if(self->current != current) { *mem = self->current->data; self->current->size += size; } else { - *mem = align_ptr_ceil(self->current->data + self->current->size, 16); + *mem = align_ptr_ceil(self->current->data + self->current->size, SCOPED_ALLOC_ALIGNMENT); self->current->size += alloc_size; } return 0; @@ -106,3 +126,10 @@ int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) { int scoped_allocator_add_buffer(ScopedAllocator *self, Buffer *buffer) { return buffer_append(&self->buffers, &buffer, sizeof(Buffer*)); } + +int scoped_allocator_create_mutex(ScopedAllocator *self, amal_mutex **mutex) { + *mutex = NULL; + return_if_error(scoped_allocator_alloc(self, sizeof(amal_mutex), (void**)mutex)); + amal_mutex_init(*mutex); + return buffer_append(&self->mutexes, mutex, sizeof(amal_mutex*)); +} -- cgit v1.2.3