diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-07-17 19:23:16 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 84e65c63e7482590d535e86f7660a00ae8a0cecb (patch) | |
tree | c79de87b7136e96b977003db85d43e5e676bbfc1 /src/std | |
parent | 85c654a102701958d3748e82ecac9c1bc4dbbcba (diff) |
Start on amal program
Fix mutex issue in lhs expr which can cause a deadlock when a file has
an error and throws and doesn't close the mutex and another thread waits
for that mutex. The mutex can instead be removed and ignore race
conditions which are uncommon. This should improve memory usage and
performance.
Diffstat (limited to 'src/std')
-rw-r--r-- | src/std/buffer.c | 8 | ||||
-rw-r--r-- | src/std/hash_map.c | 2 | ||||
-rw-r--r-- | src/std/scoped_allocator.c | 30 |
3 files changed, 10 insertions, 30 deletions
diff --git a/src/std/buffer.c b/src/std/buffer.c index 3676cee..f4e93e5 100644 --- a/src/std/buffer.c +++ b/src/std/buffer.c @@ -17,6 +17,14 @@ int buffer_init(Buffer *self, struct ScopedAllocator *allocator) { } } +void buffer_deinit(Buffer *self) { + assert(!self->allocator && "Can't deinit buffer if it has an allocator"); + am_free(self->data); + self->data = NULL; + self->size = 0; + self->capacity = 0; +} + static CHECK_RESULT int buffer_ensure_capacity(Buffer *self, usize new_capacity) { usize capacity; void *new_mem; diff --git a/src/std/hash_map.c b/src/std/hash_map.c index d13bf3d..1ad0dea 100644 --- a/src/std/hash_map.c +++ b/src/std/hash_map.c @@ -202,7 +202,7 @@ bool hash_map_get(HashMap *self, BufferView key, void *value) { #define MIN(a, b) ((a) < (b) ? (a) : (b)) -int hash_compare_string(const void *a, const void *b) { +int hash_map_compare_string(const void *a, const void *b) { const BufferView *lhs; const BufferView *rhs; int mem_diff; diff --git a/src/std/scoped_allocator.c b/src/std/scoped_allocator.c index 84ec920..d8acbf6 100644 --- a/src/std/scoped_allocator.c +++ b/src/std/scoped_allocator.c @@ -27,15 +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->mems, NULL)); - return buffer_init(&self->mutexes, NULL); -} - -static void buffer_deinit(Buffer *self) { - am_free(self->data); - self->data = NULL; - self->size = 0; - self->capacity = 0; + return buffer_init(&self->mems, NULL); } static void scoped_allocator_deinit_buffers(ScopedAllocator *self) { @@ -50,22 +42,9 @@ static void scoped_allocator_deinit_buffers(ScopedAllocator *self) { buffer_deinit(&self->mems); } -static void scoped_allocator_deinit_mutexes(ScopedAllocator *self) { - amal_mutex **mutex; - amal_mutex **mutexes_end; - mutex = buffer_begin(&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); } @@ -129,10 +108,3 @@ int scoped_allocator_add_mem(ScopedAllocator *self, usize *result_index) { *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) { - *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*)); -} |