-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharena.h
75 lines (65 loc) · 1.56 KB
/
arena.h
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include <stdbool.h>
#include <setjmp.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "allocator.h"
typedef struct
{
char *beg;
char *end;
jmp_buf *jmp_oom;
} Arena;
static void *arena_allocate_memory_(Arena *a, ptrdiff_t size, ptrdiff_t align, ptrdiff_t count)
{
ptrdiff_t padding = -(uintptr_t)a->beg & (align - 1);
ptrdiff_t available = a->end - a->beg - padding;
if(available < 0 || count > available / size)
{
if(a->jmp_oom)
longjmp(*a->jmp_oom, 1);
return NULL;
}
void *p = a->beg + padding;
a->beg += padding + count * size;
return memset(p, 0, count * size);
}
#define new(a, t, n) (t *)arena_allocate_memory_(a, sizeof(t), _Alignof(t), n)
static void arena_init(Arena *a, char *buffer, size_t size)
{
a->beg = buffer;
a->end = buffer + size;
a->jmp_oom = NULL;
}
static float arena_available_mib(Arena *a)
{
return (float)(a->end - a->beg) / 1024.f / 1024.f;
}
static Arena arena_split(Arena *base, int size)
{
Arena a = {0};
arena_init(&a, base->beg, size);
base->beg += size;
a.jmp_oom = base->jmp_oom;
return a;
}
static void *arena_malloc_(void *ctx, size_t size)
{
Arena *arena = (Arena*)ctx;
return new(arena, char, size);
}
static void arena_free_(void *ctx, void *ptr)
{
}
static Allocator arena_allocator(Arena *arena)
{
return (Allocator) { arena_malloc_, arena_free_, arena };
}
#define ARENA_ALLOCATOR(ARENA) \
(Allocator) \
{ \
arena_malloc_, arena_free_, &(ARENA) \
}