Proj-Rewrite: arena allocator with mmap

This commit is contained in:
0x221E
2026-01-16 00:53:35 +01:00
parent cf0a35c8ee
commit 58d405b397
9 changed files with 183 additions and 704 deletions

26
src/main.c Normal file
View File

@@ -0,0 +1,26 @@
#include "memory.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
int main(void) {
Backend b = {NULL, 0};
backend_initialize(&b);
Arena a;
a.b = &b;
char* test = (char*)arena_alloc(&a, 6);
char* test2 = "Noice";
memcpy(test, test2, 5);
test[5] = '\0';
printf("That's right. %s\n", test);
return 0;
}

68
src/memory.c Normal file
View File

@@ -0,0 +1,68 @@
#include "memory.h"
#include "utils.h"
#include <assert.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
void backend_initialize(Backend* b)
{
assert(b != NULL);
size_t cap = MEM_BACKEND_MAX_CAP;
void* mem = mmap(NULL, cap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(mem == MAP_FAILED) DIE("mmap failed!");
b->mem = mem;
b->last = b->mem;
b->len = 0;
b->cap = cap;
}
void backend_free(Backend* b)
{
if(munmap(b->mem, b->cap) == -1) DIE("munmap failed!");
}
void* backend_reserve(Backend* b)
{
assert(b != NULL);
void* ptr = b->last;
if(b->len + MEM_ARENA_MAX_CAP >= MEM_BACKEND_MAX_CAP)
DIE("Out of memory, requested %d (current usage %d out of %d).", MEM_ARENA_MAX_CAP, b->len, b->cap);
b->last = (char*)b->last + (int)MEM_ARENA_MAX_CAP;
b->len += MEM_ARENA_MAX_CAP;
return ptr;
}
void arena_init(Arena* a)
{
assert(a != NULL);
assert(a->b != NULL);
void* mem = backend_reserve(a->b);
a->mem = mem;
a->cap = MEM_ARENA_MAX_CAP;
a->len = 0;
a->last = a->mem;
a->next = NULL;
LOG_DEBUG("Arena initialized with block @ %p.", a->mem);
}
void* arena_alloc(Arena* a, size_t len)
{
assert(a != NULL);
assert(a->b != NULL);
if(a->mem == NULL || a->last == NULL) arena_init(a);
int len_aligned = (len + 7) & ~7;
if(a->len + len_aligned > a->cap)
{
DIE("Arena exceeded memory, requested: %d (%d out of %d used)", len, a->len, a->cap);
}
void* res = a->last;
a->len = len_aligned + a->len;
a->last = (char*)a->last + len_aligned;
LOG_DEBUG("Arena with block @ %p, allocated %d bytes of memory, arena last is now @ %p.", a->mem, len_aligned, a->last);
return res;
}

42
src/memory.h Normal file
View File

@@ -0,0 +1,42 @@
#ifndef MEMORY_H
#define MEMORY_H
#ifndef MEM_ARENA_MAX_CAP
#define MEM_ARENA_MAX_CAP 1024 * 1024
#endif
#ifndef MEM_BACKEND_MAX_CAP
#define MEM_BACKEND_MAX_CAP 1024 * 1024 * 256
#endif
#include <stddef.h>
typedef struct
{
void* mem;
void* last;
size_t len;
size_t cap;
} Backend;
typedef struct
{
void* mem;
void* last;
size_t len;
size_t cap;
void* next;
Backend* b;
} Arena;
void backend_initialize(Backend* b);
void backend_free(Backend* b);
void* backend_reserve(Backend* b);
void arena_init(Arena* a);
//void arena_reset(Arena* a);
void* arena_alloc(Arena* a, size_t len);
#endif

29
src/utils.c Normal file
View File

@@ -0,0 +1,29 @@
#include "utils.h"
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
void die_f(const char* func, int l, const char* fmt,...)
{
va_list args;
va_start(args);
printf("IBUILD Error (%d:%s): ", l, func);
vprintf(fmt, args);
printf("\nlast syscall error: \n", strerror(errno));
va_end(args);
exit(1);
}
void log_f(const char* func, int l, const char* fmt, ...)
{
va_list args;
va_start(args);
printf("IBUILD Log (%d:%s): ", l, func);
vprintf(fmt, args);
printf("\n");
va_end(args);
}

17
src/utils.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef UTILS_H
#define UTILS_H
#define DIE(fmt, ...) die_f(__func__, __LINE__, fmt, ## __VA_ARGS__)
#define LOG(fmt, ...) log_f(__func__, __LINE__, fmt, ## __VA_ARGS__)
#ifdef DEBUG
#define LOG_DEBUG(fmt, ...) log_f(__func__, __LINE__, fmt, ## __VA_ARGS__)
#else
#define LOG_DEBUG(...)
#endif
void die_f(const char* func, int l, const char* fmt, ...);
void log_f(const char* func, int l, const char* fmt, ...);
#endif