include/frx/frx_arena.h

filter: Statementstmt 100% 66 / 66 Branchbran 63.8% 23 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.2% 89 / 102 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/frx/frx_model.h ↑ Summary include/frx/frx_uri.h »
1 #ifndef FRX_ARENA_H
2 #define FRX_ARENA_H
3  
4 /* The allocator, and the string it hands out.
5 *
6 * A page-chained bump allocator: 64 KiB pages chained through the head,
7 * allocations over a quarter of a page on a page of their own, sixteen-byte
8 * alignment, and never a realloc - so a pointer handed out is stable for
9 * the life of the document, which is what the ABI's frx_node * promises.
10 * This is Markdown::Simple's arena with the profiling counters dropped;
11 * `pages` remains only because the selftest counts them. Page-chained
12 * rather than Template::Stencil's offset-based arena, which survives a
13 * realloc by paying a base-plus-offset on every access: a tree the consumer
14 * walks by pointer wants the pointers.
15 *
16 * NULL on failure, never abort; the caller turns it into FRX_E_NOMEM with
17 * the offset it was at. malloc and free by rule 3; frx_compat.h has already
18 * removed PerlMem's macros so these are libc's.
19 *
20 * An frx_str is a pointer into the arena and a length. Always
21 * NUL-terminated, so a C consumer compares with memcmp and prints with %s
22 * and never copies.
23 *
24 * Needs nothing. */
25  
26 #define FRX_ARENA_PAGE (64u * 1024u)
27 #define FRX_ARENA_BIG (FRX_ARENA_PAGE / 4u)
28 #define FRX_ARENA_ALIGN 16u
29  
30 typedef struct frx_str {
31 const char *p;
32 size_t len;
33 } frx_str;
34  
35 typedef struct frx_arena_page {
36 struct frx_arena_page *next;
37 size_t used;
38 size_t cap;
39 unsigned char data[1]; /* the page follows inline */
40 } frx_arena_page;
41  
42 typedef struct frx_arena {
43 frx_arena_page *head; /* the page being filled */
44 frx_arena_page *big; /* oversize pages, one allocation each */
45 size_t pages; /* both lists; for the selftest */
46 } frx_arena;
47  
48 static frx_arena_page *
49 796773 frx_arena_page_new(size_t cap)
50 {
51 796773 frx_arena_page *p = (frx_arena_page *)malloc(sizeof(frx_arena_page) + cap);
52 796773 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 796773 796773
53 796773 p->next = NULL;
54 796773 p->used = 0;
55 796773 p->cap = cap;
56 796773 return p;
57 }
58  
59 /* the padding that brings data + used onto the alignment */
60 static size_t
61 796773 frx_arena_pad(const frx_arena_page *p)
62 {
63 796773 size_t base = (size_t)(const void *)p->data;
64 796773 return (FRX_ARENA_ALIGN - (base & (FRX_ARENA_ALIGN - 1))) & (FRX_ARENA_ALIGN - 1);
65 }
66  
67 static void
68 791099 frx_arena_init(frx_arena *a)
69 {
70 791099 a->head = NULL;
71 791099 a->big = NULL;
72 791099 a->pages = 0;
73 791099 }
74  
75 static void *
76 47459899 frx_arena_alloc(frx_arena *a, size_t n)
77 {
78 47459899 size_t aligned = (n + (FRX_ARENA_ALIGN - 1)) & ~(size_t)(FRX_ARENA_ALIGN - 1);
79 void *out;
80  
81 47459899 ▶ if (aligned < n) return NULL; /* size_t wrapped */
Branch: 1 branch
BranchTrueFalseTotal
if (aligned < n) return NULL; /* size_t wrapped */ 0 47459899 47459899
82  
83 47459899 ▶ if (aligned > FRX_ARENA_BIG) {
Branch: 1 branch
BranchTrueFalseTotal
if (aligned > FRX_ARENA_BIG) { 1463 47458436 47459899
84 1463 frx_arena_page *p = frx_arena_page_new(aligned + FRX_ARENA_ALIGN);
85 size_t pad;
86 1463 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 1463 1463
87 1463 pad = frx_arena_pad(p);
88 1463 p->used = pad + aligned;
89 1463 p->next = a->big;
90 1463 a->big = p;
91 1463 a->pages++;
92 1463 return p->data + pad;
93 }
94  
95 47458436 ▶ if (!a->head || a->head->used + aligned > a->head->cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (!a->head || a->head->used + aligned > a->head->cap) { 46667395 791041 47458436
if (!a->head || a->head->used + aligned > a->head->cap) { 4269 46663126 46667395
96 795310 frx_arena_page *p = frx_arena_page_new(FRX_ARENA_PAGE);
97 795310 ▶ if (!p) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!p) return NULL; 0 795310 795310
98 795310 p->used = frx_arena_pad(p); /* the first pointer is aligned */
99 795310 p->next = a->head;
100 795310 a->head = p;
101 795310 a->pages++;
102 }
103 47458436 out = a->head->data + a->head->used;
104 47458436 a->head->used += aligned;
105 47458436 return out;
106 }
107  
108 /* n bytes of s into the arena with a NUL after them; p NULL on failure */
109 static frx_str
110 34717826 frx_arena_strndup(frx_arena *a, const char *s, size_t n)
111 {
112 frx_str out;
113 34717826 char *p = (char *)frx_arena_alloc(a, n + 1);
114 34717826 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 34717826 0 34717826
115 34717826 ▶ if (n) memcpy(p, s, n);
Branch: 1 branch
BranchTrueFalseTotal
if (n) memcpy(p, s, n); 31019089 3698737 34717826
116 34717826 p[n] = '\0';
117 }
118 34717826 out.p = p;
119 34717826 ▶ out.len = p ? n : 0;
Branch: 1 branch
BranchTrueFalseTotal
out.len = p ? n : 0; 34717826 0 34717826
120 34717826 return out;
121 }
122  
123 /* A mark is a position in the arena: everything allocated after it can be
124 * released together, which is what a streaming reader does at the end of
125 * each record so a million of them cost the memory of one. Pages are
126 * pushed at the front of each list, so the pages allocated since the
127 * mark are the ones before the marked page. */
128 typedef struct frx_arena_mark {
129 frx_arena_page *head;
130 size_t used;
131 frx_arena_page *big;
132 size_t pages;
133 } frx_arena_mark;
134  
135 static void
136 820845 frx_arena_mark_get(const frx_arena *a, frx_arena_mark *m)
137 {
138 820845 m->head = a->head;
139 820845 ▶ m->used = a->head ? a->head->used : 0;
Branch: 1 branch
BranchTrueFalseTotal
m->used = a->head ? a->head->used : 0; 820845 0 820845
140 820845 m->big = a->big;
141 820845 m->pages = a->pages;
142 820845 }
143  
144 static void
145 820812 frx_arena_release(frx_arena *a, const frx_arena_mark *m)
146 {
147 820812 ▶ while (a->head && a->head != m->head) { frx_arena_page *n = a->head->next; free(a->head); a->head = n; }
Branch: 2 branches
BranchTrueFalseTotal
while (a->head && a->head != m->head) { frx_arena_page *n = a->head->next; free(a->head); a->head = n; } 820812 0 820812
while (a->head && a->head != m->head) { frx_arena_page *n = a->head->next; free(a->head); a->head = n; } 0 820812 820812
148 820812 ▶ if (a->head) a->head->used = m->used;
Branch: 1 branch
BranchTrueFalseTotal
if (a->head) a->head->used = m->used; 820812 0 820812
149 820812 ▶ while (a->big && a->big != m->big) { frx_arena_page *n = a->big->next; free(a->big); a->big = n; }
Branch: 2 branches
BranchTrueFalseTotal
while (a->big && a->big != m->big) { frx_arena_page *n = a->big->next; free(a->big); a->big = n; } 0 820812 820812
while (a->big && a->big != m->big) { frx_arena_page *n = a->big->next; free(a->big); a->big = n; } 0 0 0
150 820812 a->pages = m->pages;
151 820812 }
152  
153 static void
154 791099 frx_arena_free(frx_arena *a)
155 {
156 791099 frx_arena_page *p = a->head;
157 1586409 ▶ while (p) { frx_arena_page *n = p->next; free(p); p = n; }
Branch: 1 branch
BranchTrueFalseTotal
while (p) { frx_arena_page *n = p->next; free(p); p = n; } 795310 791099 1586409
158 791099 p = a->big;
159 792562 ▶ while (p) { frx_arena_page *n = p->next; free(p); p = n; }
Branch: 1 branch
BranchTrueFalseTotal
while (p) { frx_arena_page *n = p->next; free(p); p = n; } 1463 791099 792562
160 791099 a->head = NULL;
161 791099 a->big = NULL;
162 791099 a->pages = 0;
163 791099 }
164  
165 #endif /* FRX_ARENA_H */
« include/frx/frx_model.h ↑ Summary include/frx/frx_uri.h »