Skip to content

Instantly share code, notes, and snippets.

@Juskr04
Created September 1, 2025 11:19
Show Gist options
  • Select an option

  • Save Juskr04/5300a00468e43aae9720525e16ad0f9d to your computer and use it in GitHub Desktop.

Select an option

Save Juskr04/5300a00468e43aae9720525e16ad0f9d to your computer and use it in GitHub Desktop.
Basic arena allocator(incomplete)
#include <sys/mman.h>
#include <stdint.h>
#include <stddef.h>
#include <sanitizer/asan_interface.h>
#include "./stdint.h"
typedef struct
{
u8 *arena_start_pos;
size_t size;//total bytes allocated for the arena
size_t offset;//bytes used up
} Arena, *ptr_arena;
#define KB(T) ((T)*1024)
#define MB(T) ((T)*1024*1024)
#define GB(T) ((T)*1024*1024*1024)
//#define arena_push_array(arena, type, count) (type *)arena_push((arena), (sizeof(type)*count))
#define arena_array_push_and_zero(arena, type, count) (type *)arena_push_and_zero((arena), (sizeof(type)*count))
#define arena_struct_push(arena, type) (type *)arena_push((arena), sizeof(type)*1 )
#define arena_struct_push_and_zero(arena, type) (type *)arena_push_and_zero((arena), sizeof(type)*1 )
#define arena_array_of_structs_push(arena, type, count) (type *)arena_push((arena), (sizeof(type)*count))
#define arena_init(name) Arena name = {0};
void arena_allocate(Arena *arena, size_t size)
{
arena->arena_start_pos = (u8 *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
arena->size = size;
}
void *arena_push(Arena *arena, size_t size)
{
void *ptr_temp = arena->arena_start_pos + arena->offset;
arena->offset += size;
return ptr_temp;
}
#define vector(T) struct { T *data; ptrdiff_t length, capacity; }
#define arena_array_init_and_push(arena, type, num_elements) \
{ \
.data = (type *)arena_push((arena), sizeof(type)*(num_elements)), \
.length = 0, \
.capacity = (num_elements) \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment