aboutsummaryrefslogtreecommitdiff
path: root/util/arena.h
blob: 3d82b95319ed37cfcf0e6bd5ac4e0f978f6bae70 (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
#ifndef ARENA_H
#define ARENA_H

#include <stddef.h> // size_t

struct arena_ctx {
    void *buffer;
    size_t size;
    size_t offset;
};

#define ARENA_CTX_INIT(buffer, sz) (struct arena_ctx){(buffer), (sz), 0}
void *arena_allocate(struct arena_ctx *ctx, size_t sz);
void arena_reset(struct arena_ctx *ctx);

#ifdef ARENA_IMPLEMENTATION

void *arena_allocate(struct arena_ctx *ctx, size_t sz)
{
    if(ctx->offset + sz > ctx->size) return NULL;

    void *off = ctx->buffer + ctx->offset;
    ctx->offset += sz;
    return off;
}

void arena_reset(struct arena_ctx *ctx) { ctx->offset = 0; }

#endif
#endif