aboutsummaryrefslogtreecommitdiff
path: root/src/std/scoped_allocator.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/std/scoped_allocator.c')
-rw-r--r--src/std/scoped_allocator.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/std/scoped_allocator.c b/src/std/scoped_allocator.c
index dfed0b7..c66639a 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/log.h"
#include <assert.h>
#define ALLOC_NODE_SIZE 4096
@@ -36,18 +37,18 @@ static void buffer_deinit(Buffer *self) {
}
void scoped_allocator_deinit(ScopedAllocator *self) {
- Buffer *buffer;
- Buffer *buffer_end;
+ Buffer **buffer;
+ Buffer **buffers_end;
- scoped_allocator_node_deinit(&self->head);
self->current = NULL;
- buffer = (Buffer*)self->buffers.data;
- buffer_end = buffer + self->buffers.size / sizeof(Buffer);
- while(buffer != buffer_end) {
- buffer_deinit(buffer);
+ buffer = buffer_start(&self->buffers);
+ buffers_end = buffer_end(&self->buffers);
+ while(buffer != buffers_end) {
+ buffer_deinit(*buffer);
++buffer;
}
buffer_deinit(&self->buffers);
+ scoped_allocator_node_deinit(&self->head);
}
static CHECK_RESULT int scoped_allocator_ensure_capacity_for(ScopedAllocator *self, usize size) {
@@ -82,9 +83,13 @@ int scoped_allocator_alloc(ScopedAllocator *self, usize size, void **mem) {
ScopedAllocatorNode *current;
usize alloc_size;
assert(self->current);
- assert(size <= ALLOC_NODE_SIZE);
current = self->current;
+ if(size >= ALLOC_NODE_SIZE) {
+ amal_log_error("scoped_allocator_alloc: tried to alloc memory of size %lu. Max allowed alloc size is %lu", size, ALLOC_NODE_SIZE);
+ return -1;
+ }
+
alloc_size = size + align_ptr_ceil_offset(self->current->data + self->current->size, 16);
return_if_error(scoped_allocator_ensure_capacity_for(self, alloc_size));
/* Reallocated (new node created) */
@@ -99,5 +104,5 @@ 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));
+ return buffer_append(&self->buffers, &buffer, sizeof(Buffer*));
}