diff --git a/allocator.c b/allocator.c new file mode 100644 index 0000000..df2e203 --- /dev/null +++ b/allocator.c @@ -0,0 +1,65 @@ +#include "allocator.h" +#include "aliases.h" +#include +#include + +#define HEADER_MAGIC_01 76329872 +#define HEADER_MAGIC_02 120830238 +#define FOOTER_MAGIC_01 2783946 +#define FOOTER_MAGIC_02 878512284 + +struct allocator { + u8 *buf; + u64 size; + u64 used; +}; + +struct block_boundary { + u64 size; + bool free; + u64 magic_01; + u64 magic_02; +}; + +typedef struct block_boundary block_header; +typedef struct block_boundary block_footer; + +typedef struct { + block_header header; + u8 *data; + block_footer footer; +} block_t; + +allocator_t *init_allocator(u64 size) { + allocator_t *allocator = (allocator_t *)malloc(sizeof(allocator_t)); + + if (!allocator) { + return NULL; + } + + allocator->buf = (u8 *)malloc(size); + + if (!(allocator->buf)) { + free(allocator); + return NULL; + } + + allocator->size = size; + allocator->used = 0; + + return allocator; +} + +void deinit_allocator(allocator_t **allocator) { + if (*allocator != NULL) { + if ((*allocator)->buf != NULL) { + free((*allocator)->buf); + (*allocator)->buf = NULL; + } + + free(*allocator); + *allocator = NULL; + } +} + +void *alloc(allocator_t *allocator, u64 size) {} diff --git a/allocator.h b/allocator.h new file mode 100644 index 0000000..3baccd1 --- /dev/null +++ b/allocator.h @@ -0,0 +1,13 @@ +#ifndef ALLOCATOR_H +#define ALLOCATOR_H + +#include "aliases.h" +#include + +typedef struct allocator allocator_t; + +allocator_t *init_allocator(u64 size); +void deinit_allocator(allocator_t **allocator); +void *alloc(allocator_t *allocator, u64 size); + +#endif // !ALLOCATOR_H