From 9eefbb4afc9601024eac6191addd98e21f90a5b3 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 18 Dec 2022 16:53:46 +0100 Subject: multithread outgoing network messages, thread safe allocator, refactor --- src/allocator.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/allocator.c') diff --git a/src/allocator.c b/src/allocator.c index ffb6763..bddc5e5 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -5,17 +5,30 @@ allocator create_allocator(uint64_t size) { allocator.cursor = 0; allocator.size = size; allocator.memory = mem_alloc(size); + allocator.mutex = mutex_create(); return allocator; } void* allocator_alloc(allocator* al, uint64_t size) { + mutex_lock(&al->mutex); if (al->cursor + size < al->size) { al->cursor += size; - return al->memory + al->cursor - size; + void* result = al->memory + al->cursor - size; + mutex_unlock(&al->mutex); + return result; } log_assert(0, "Allocator out of space"); + mutex_unlock(&al->mutex); } void destroy_allocator(allocator* al) { + mutex_lock(&al->mutex); mem_free(al->memory); + mutex_unlock(&al->mutex); +} + +void allocator_clear(allocator* al) { + mutex_lock(&al->mutex); + al->cursor = 0; + mutex_unlock(&al->mutex); } \ No newline at end of file -- cgit v1.2.3-70-g09d2