-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCirMem.c
59 lines (52 loc) · 1.14 KB
/
CirMem.c
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
#include "cir_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdalign.h>
#include <assert.h>
#define ALLOC_SIZE (1024 * 1024 * 1)
static uint8_t *currMem;
static size_t bytesAllocated;
void *
cir__xalloc(size_t n)
{
void *ptr = malloc(n);
if (!ptr)
cir_fatal("out of memory when trying to alloc %lu bytes", (unsigned long)n);
return ptr;
}
void *
cir__zalloc(size_t n)
{
void *ptr = calloc(1, n);
if (!ptr)
cir_fatal("out of memory when trying to zalloc %lu bytes", (unsigned long)n);
return ptr;
}
void *
cir__xrealloc(void *ptr, size_t size)
{
ptr = realloc(ptr, size);
if (!ptr)
cir_fatal("out of memory when trying to realloc %lu bytes", (unsigned long)size);
return ptr;
}
void
cir__xfree(void *ptr)
{
free(ptr);
}
void *
CirMem_balloc(size_t n, size_t align)
{
assert(align);
if (n % align)
n += align - n % align;
if (!currMem || bytesAllocated + n > ALLOC_SIZE) {
// Allocate another pool
currMem = cir__xalloc(ALLOC_SIZE);
bytesAllocated = 0;
}
void *ptr = currMem + bytesAllocated;
bytesAllocated += n;
return ptr;
}