diff --git a/README.md b/README.md index 5f01af3..6d212b7 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,8 @@ are deferred, and called in the reverse order. This way the first gui function that captrues input can signal to the other function that input is captured, and the drawing of the gui elements can reflect the priority of the gui functions. Overlapping gui elements in raygui is the main painpoint imo. + +# TODO + +If there is an allocation error it does not actually cause a crash... +FIX!!! diff --git a/arena_allocator.c b/arena_allocator.c new file mode 100644 index 0000000..ced0ee5 --- /dev/null +++ b/arena_allocator.c @@ -0,0 +1,26 @@ +#include "cig.h" + + +void *arena_allocator_alloc_func( + arena_allocator_t *this, + size_t bytes, + const char *file, + int line +) { + allocator_t std_alloc = allocator_stdlib(); + buffer_allocator_t ba = (buffer_allocator_t) { + .size=dyn_array_length(this->bytes), + .capacity=dyn_array_capacity(this->bytes), + .data=this->bytes + }; +} + +void arena_allocator_reset(arena_allocator_t *this) { +} + +void arena_allocator_destroy(arena_allocator_t *this) { +} + +allocator_t arena_allocator_interface(arena_allocator_t *this) { +} + diff --git a/buffer_allocator.c b/buffer_allocator.c index bcb85b8..e7c310c 100644 --- a/buffer_allocator.c +++ b/buffer_allocator.c @@ -1,12 +1,13 @@ #include "cig.h" void *buffer_allocator_alloc(buffer_allocator_t *this, size_t bytes) { - // TODO: use max-align - // also make the default function crash if outOf memory? - size_t new_size = this->size + bytes; + size_t address_of_new_data = (size_t) &this->data[this->size]; + size_t offset = address_of_new_data % MAX_ALIGN; + size_t new_size = this->size + offset + bytes; if (new_size > this->capacity) { return NULL; } + this->size += offset; void *ptr = &this->data[this->size]; this->size = new_size; return ptr; diff --git a/cig.h b/cig.h index 0c948ce..a28f302 100644 --- a/cig.h +++ b/cig.h @@ -5,8 +5,8 @@ #include #include -typedef union any_align { char c; int i; long l; long long ll; float f; double d; void *p; long double ld; } any_align_t; -#define MAX_ALIGN (sizeof(any_align_t)) +typedef union any_align { char c; int i; long l; long long ll; float f; double d; void *p; } any_align_t; +#define MAX_ALIGN ((size_t) sizeof(any_align_t)) #define KB (1024) #define MB (KB * KB) #define GB (KB * KB * KB) @@ -112,18 +112,23 @@ allocator_t borrow_allocator_interface(borrow_allocator_t *this); // beforehand. Instead the number of bytes allocated will be recorded and // updated, so that the whole thing can be reallocated as a single chunk that is // as large as it needs to be. +// +// This basically exists for allocations that live for a single 'tick' of an +// application. +// It is intended to eventually stabilize and find the required size of the +// memory chunk that is needed for a single frame. typedef struct arena_allocator { - borrow_allocator_t borrow_allocator; - // Total size of bytes allocated. This allocator will only grow the - // allocated field as allocations are made, keeping track of the total - // memory allocated, and if the allocations have to be done in several - // chunks because your initial guess of how much memory was needed doesn't - // meet reality, all chunks will be freed, and then for the next - // initialization this total amount will be allocated as a single chunk. - // This freeing of memory happens on reset, the next alloc allocates the - // whole chunk. - size_t total_allocated; - uint8_t *bytes; + borrow_allocator_t borrow_allocator; + // Total size of bytes allocated. This allocator will only grow the + // allocated field as allocations are made, keeping track of the total + // memory allocated, and if the allocations have to be done in several + // chunks because your initial guess of how much memory was needed doesn't + // meet reality, all chunks will be freed, and then for the next + // initialization this total amount will be allocated as a single chunk. + // This freeing of memory happens on reset, the next alloc allocates the + // whole chunk. + size_t total_allocated; + uint8_t *bytes; } arena_allocator_t; #define arena_allocator_create() \ (arena_allocator_t) { \ @@ -142,6 +147,7 @@ void *arena_allocator_alloc_func( void arena_allocator_reset(arena_allocator_t *this); void arena_allocator_destroy(arena_allocator_t *this); +allocator_t arena_allocator_interface(arena_allocator_t *this); // dynamic arrays ////////////////////////////////////////////////////////////// @@ -234,6 +240,7 @@ void allocator_free_func(allocator_t this, void *ptr, const char *file, int line #include "buffer_allocator.c" #include "borrow_allocator.c" #include "dyn_array.c" +#include "arena_allocator.c" #endif // ALLOCATOR_IMPLEMENTATION #endif // ALLOCATOR_H diff --git a/test_macro_magic.c b/test_macro_magic.c index 4207f8d..270acf5 100644 --- a/test_macro_magic.c +++ b/test_macro_magic.c @@ -23,6 +23,4 @@ Test(macro_magic, default_values) { cr_assert_eq(a1.is_true, true); cr_assert_eq(a2.is_true, true); cr_assert_eq(a3.is_true, false); - // TODO if there is an allocation error it does not actually cause a crash... - // FIX!!! }