summaryrefslogtreecommitdiff
path: root/src/std/mem.h
blob: eef97a0c42fec339b1d7fb0f17c0b24530e04f32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * @file mem.h
 * @brief Allocators, memory tracking
 * @version 0.1
 * @date 2024-02-24
 *
 * @copyright Copyright (c) 2024
 *
 */
#pragma once

#include <stddef.h>
#include "defines.h"

// --- Arena

// Inspired by https://nullprogram.com/blog/2023/09/27/
typedef struct arena {
  char* begin;
  char* curr;
  char* end;
} arena;

typedef struct arena_save {
  arena* arena;
  char* savepoint;
} arena_save;

arena arena_create(void* backing_buffer, size_t capacity);
void* arena_alloc(arena* a, size_t size);
void* arena_alloc_align(arena* a, size_t size, size_t align);
void arena_free_all(arena* a);
void arena_free_storage(arena* a);
arena_save arena_savepoint(arena* a);
void arena_rewind(arena_save savepoint);
// TODO: arena_resize

// --- Pool

typedef struct void_pool_header void_pool_header;
struct void_pool_header {
  void_pool_header* next;
};

typedef struct void_pool {
  u64 capacity;
  u64 entry_size;
  u64 count;
  void* backing_buffer;
  void_pool_header* free_list_head;
} void_pool;

void_pool void_pool_create(arena* a, u64 capacity, u64 entry_size);
void void_pool_free_all(void_pool* pool);
bool void_pool_is_empty(void_pool* pool);
bool void_pool_is_full(void_pool* pool);
void* void_pool_get(u32 raw_handle);

// TODO: macro that lets us specialise