aboutsummaryrefslogtreecommitdiffstats
path: root/src/arena.c
blob: 22216165e799b64e8d2e60cc2ccb956e0e9da78c (plain) (blame)
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
#include "arena.h"

struct block {
    size_t size;
    size_t used;
    struct block *next;
    char data_start[];
};

static const size_t min_block_room = 16 * 1024 - sizeof(struct block);

static void add_block(struct arena* a, size_t room_wanted)
{
    if (room_wanted < min_block_room) {
        room_wanted = min_block_room;
    }
    struct block *new_block = malloc(sizeof(struct block) + room_wanted);
    new_block->size = room_wanted;
    new_block->used = 0;
    new_block->next = a->cur_block;
    a->cur_block = new_block;
}

void arena_init(struct arena *a)
{
    a->cur_block = NULL;
}

void* arena_malloc(struct arena *a, size_t amount)
{
    struct block* b = a->cur_block;
    if (b == NULL || b->size - b->used < amount) {
        add_block(a, amount);
        b = a->cur_block;
    }
    void* result = &b->data_start[b->used];
    b->used += amount;
    return result;
}

void arena_free(struct arena *a)
{
    struct block* b = a->cur_block;
    while (b != NULL) {
        struct block* next = b->next;
        free(b);
        b = next;
    }
    a->cur_block = NULL;
}