blob: 1142321759396815e821ee3b017fd53ef0fa34aa (
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
|
#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
#define ARENA_ALLIGNMENT 2
// #include <assert.h>
void *arena_allocate(struct arena_ctx *ctx, size_t sz)
{
if(sz % ARENA_ALLIGNMENT != 0)
sz += ARENA_ALLIGNMENT - (sz % ARENA_ALLIGNMENT);
if(ctx->offset + sz > ctx->size) return NULL;
void *off = (void *)((intptr_t)ctx->buffer + ctx->offset);
ctx->offset += sz;
// assert(((intptr_t)off & 0x1) == 0);
return off;
}
void arena_reset(struct arena_ctx *ctx) { ctx->offset = 0; }
#endif
#endif
|