77 lines
1.5 KiB
C
77 lines
1.5 KiB
C
#ifndef ARENA_H
|
|
# define ARENA_H
|
|
|
|
# include <stddef.h>
|
|
# include <stdint.h>
|
|
# include <stdbool.h>
|
|
# include <assert.h>
|
|
# include <stdlib.h>
|
|
# include <string.h>
|
|
|
|
#define ARENA_PAGE_SIZE 4096
|
|
|
|
typedef struct {
|
|
|
|
} allocation_t;
|
|
|
|
typedef struct {
|
|
uint8_t buffer[ARENA_PAGE_SIZE];//this allow me to have all memory allocated on arena init
|
|
bool empty;
|
|
uint16_t size;
|
|
} page_t;
|
|
|
|
typedef struct {
|
|
page_t* pages;
|
|
uint32_t capacity;
|
|
uint32_t used;
|
|
uint32_t latest_access;
|
|
} arena_t;
|
|
|
|
typedef struct {
|
|
} forgotten_t;//??what is this i forgot what as was doing ffs
|
|
|
|
typedef void ainit(uint32_t n_page);
|
|
typedef void* aalloc(size_t nmemb, uint32_t size);
|
|
typedef void afree(void* ptr);
|
|
|
|
# define ARENA_IMPLEMENTATION
|
|
# ifdef ARENA_IMPLEMENTATION
|
|
|
|
static inline arena_t ainit(uint32_t n_page) {
|
|
arena_t a;
|
|
a.capacity = n_page;
|
|
a.latest_access = 0;
|
|
a.used = 0;
|
|
a.pages = (page_t*)calloc(n_page, sizeof(page_t));
|
|
assert(a.pages);
|
|
return (a);
|
|
}
|
|
|
|
static inline void* aalloc(arena_t* arena, uint32_t nmemb, uint32_t size) {
|
|
assert(arena);
|
|
uint64_t alloc_size = size * nmemb;
|
|
assert(alloc_size <= ARENA_PAGE_SIZE);
|
|
for (uint64_t i = arena->latest_access; i < arena->capacity; i++) {
|
|
page_t* page = &arena->pages[i];
|
|
if (page->size + alloc_size <= ARENA_PAGE_SIZE) {
|
|
void* ptr = page->buffer + page->size;
|
|
page->size += alloc_size;
|
|
arena->latest_access = i;
|
|
if (i >= arena->used) {
|
|
arena->used = 1 + i;
|
|
}
|
|
return (ptr);
|
|
}
|
|
}
|
|
assert(false && "Arena out of memory !");
|
|
return (0x00);
|
|
}
|
|
|
|
static inline void afree(void* ptr) {
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#endif
|