forked from commonmark/cmark
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy patharena.c
More file actions
131 lines (110 loc) · 2.81 KB
/
arena.c
File metadata and controls
131 lines (110 loc) · 2.81 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "cmark-gfm.h"
#include "cmark-gfm-extension_api.h"
#include "mutex.h"
CMARK_DEFINE_LOCK(arena)
static struct arena_chunk {
size_t sz, used;
uint8_t push_point;
void *ptr;
struct arena_chunk *prev;
} *A = NULL;
static struct arena_chunk *alloc_arena_chunk(size_t sz, struct arena_chunk *prev) {
struct arena_chunk *c = (struct arena_chunk *)calloc(1, sizeof(*c));
if (!c)
abort();
c->sz = sz;
c->ptr = calloc(1, sz);
if (!c->ptr)
abort();
c->prev = prev;
return c;
}
void cmark_arena_push(void) {
CMARK_INITIALIZE_AND_LOCK(arena);
if (A) {
A->push_point = 1;
A = alloc_arena_chunk(10240, A);
}
CMARK_UNLOCK(arena);
}
int cmark_arena_pop(void) {
int ret = 1;
CMARK_INITIALIZE_AND_LOCK(arena);
if (!A)
ret = 0;
else {
while (A && !A->push_point) {
free(A->ptr);
struct arena_chunk *n = A->prev;
free(A);
A = n;
}
if (A)
A->push_point = 0;
}
CMARK_UNLOCK(arena);
return ret;
}
static void init_arena(void) {
CMARK_INITIALIZE_AND_LOCK(arena);
A = alloc_arena_chunk(4 * 1048576, NULL);
CMARK_UNLOCK(arena);
}
void cmark_arena_reset(void) {
CMARK_INITIALIZE_AND_LOCK(arena);
while (A) {
free(A->ptr);
struct arena_chunk *n = A->prev;
free(A);
A = n;
}
CMARK_UNLOCK(arena);
}
static void *arena_calloc(size_t nmem, size_t size) {
if (!A)
init_arena();
size_t sz = nmem * size + sizeof(size_t);
// Round allocation sizes to largest integer size to
// ensure returned memory is correctly aligned
const size_t align = sizeof(size_t) - 1;
sz = (sz + align) & ~align;
CMARK_INITIALIZE_AND_LOCK(arena);
struct arena_chunk *chunk;
if (sz > A->sz) {
A->prev = chunk = alloc_arena_chunk(sz, A->prev);
} else if (sz > A->sz - A->used) {
A = chunk = alloc_arena_chunk(A->sz + A->sz / 2, A);
} else {
chunk = A;
}
void *ptr = (uint8_t *) chunk->ptr + chunk->used;
chunk->used += sz;
*((size_t *) ptr) = sz - sizeof(size_t);
CMARK_UNLOCK(arena);
return (uint8_t *) ptr + sizeof(size_t);
}
static void *arena_realloc(void *ptr, size_t size) {
if (!A)
init_arena();
void *new_ptr = arena_calloc(1, size);
if (ptr)
memcpy(new_ptr, ptr, ((size_t *) ptr)[-1]);
return new_ptr;
}
static void arena_free(void *ptr) {
(void) ptr;
/* no-op */
}
static void *arena_calloc_typed(size_t nmem, size_t size, cmark_malloc_type_id type_id) {
return arena_calloc(nmem, size);
}
static void *arena_realloc_typed(void *ptr, size_t size, cmark_malloc_type_id type_id) {
return arena_realloc(ptr, size);
}
cmark_mem CMARK_ARENA_MEM_ALLOCATOR = {arena_calloc, arena_realloc, arena_free, arena_calloc_typed, arena_realloc_typed};
cmark_mem *cmark_get_arena_mem_allocator(void) {
return &CMARK_ARENA_MEM_ALLOCATOR;
}