blob: c30f76dc8a27a736a5b3e010abfb3661647ab9a8 (
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
|
#include "mem.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "log.h"
#ifndef DEFAULT_ALIGNMENT
#define DEFAULT_ALIGNMENT (2 * sizeof(void*))
#endif
void* arena_alloc_align(arena* a, size_t size, size_t align) {
ptrdiff_t padding = -(uintptr_t)a->begin & (align - 1);
ptrdiff_t available = a->end - a->begin - padding;
TRACE("Padding %td available %td", padding, available);
if (available < 0 || (ptrdiff_t)size > available) {
ERROR_EXIT("Arena ran out of memory\n");
}
void* p = a->begin + padding;
a->begin += padding + size;
return memset(p, 0, size);
}
void* arena_alloc(arena* a, size_t size) { return arena_alloc_align(a, size, DEFAULT_ALIGNMENT); }
arena arena_create(void* backing_buffer, size_t capacity) {
return (arena){ .begin = backing_buffer, .end = backing_buffer + (ptrdiff_t)capacity };
}
|