summaryrefslogtreecommitdiff
path: root/src/allocator.c
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2022-12-18 16:53:46 +0100
committerAldrik Ramaekers <aldrik@amftech.nl>2022-12-18 16:53:46 +0100
commit9eefbb4afc9601024eac6191addd98e21f90a5b3 (patch)
treee26771a2ba2d93c58940f011d8701372419f6cb8 /src/allocator.c
parent40027e44e5d0f0040238cfc1bd6a8d80c17c54fb (diff)
multithread outgoing network messages, thread safe allocator, refactor
Diffstat (limited to 'src/allocator.c')
-rw-r--r--src/allocator.c15
1 files changed, 14 insertions, 1 deletions
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