jsfw/ser/arena_allocator.h
viandoxdev eec4668856
ser
2023-05-06 18:42:50 +02:00

36 lines
736 B
C

#ifndef ARENA_ALLOCATOR_H
#define ARENA_ALLOCATOR_H
#include "utils.h"
#include "vector_impl.h"
#include <stdint.h>
#include <stdlib.h>
#define ARENA_BLOCK_SIZE 4096
typedef struct {
size_t size;
byte *data;
byte *end;
} ArenaBlock;
void arena_block_drop(ArenaBlock block);
VECTOR_IMPL(ArenaBlock, ArenaBlockVec, arena_block, arena_block_drop);
// Simple growing arena allocator
typedef struct {
ArenaBlockVec blocks;
ArenaBlock *last;
byte *ptr;
} ArenaAllocator;
// Create a new arena allocator
ArenaAllocator arena_init();
// Allocate size bytes in the arena
void *arena_alloc(ArenaAllocator *alloc, size_t size);
// Destroy the arena, freeing its memory
void arena_drop(ArenaAllocator arena);
#endif