src/stencil_arena.c

filter: Statementstmt 82.6% 119 / 144 Branchbran 66.3% 61 / 92 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.2% 180 / 236 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/stencil_escape.c ↑ Summary src/stencil_engine.c »
1 #include "stencil.h"
2  
3 #include <stdlib.h>
4  
5 123926 int stencil_arena_init(stencil_arena *a, size_t initial_cap)
6 {
7 123926 ▶ if (initial_cap < 64)
Branch: 1 branch
BranchTrueFalseTotal
if (initial_cap < 64) 0 123926 123926
8 0 initial_cap = 64;
9 123926 a->base = (char *)malloc(initial_cap);
10 123926 ▶ if (!a->base)
Branch: 1 branch
BranchTrueFalseTotal
if (!a->base) 0 123926 123926
11 0 return 0;
12 123926 a->used = 0;
13 123926 a->cap = initial_cap;
14 123926 return 1;
15 }
16  
17 3396470 uint32_t stencil_arena_alloc(stencil_arena *a, size_t size, size_t align)
18 {
19 3396470 size_t off = (a->used + (align - 1)) & ~(align - 1);
20 3396470 ▶ if (STENCIL_UNLIKELY(off + size < off || off + size > (uint32_t)-2))
Branch: 2 branches
BranchTrueFalseTotal
if (STENCIL_UNLIKELY(off + size < off || off + size > (uint32_t)-2)) 3396470 0 3396470
if (STENCIL_UNLIKELY(off + size < off || off + size > (uint32_t)-2)) 0 3396470 3396470
21 0 return STENCIL_ARENA_NULL; /* offset space exhausted */
22 3396470 ▶ if (STENCIL_UNLIKELY(off + size > a->cap)) {
Branch: 1 branch
BranchTrueFalseTotal
if (STENCIL_UNLIKELY(off + size > a->cap)) { 25111 3371359 3396470
23 25111 size_t ncap = a->cap;
24 char *nb;
25 50247 ▶ while (ncap < off + size)
Branch: 1 branch
BranchTrueFalseTotal
while (ncap < off + size) 25136 25111 50247
26 25136 ncap <<= 1;
27 25111 nb = (char *)realloc(a->base, ncap);
28 25111 ▶ if (!nb)
Branch: 1 branch
BranchTrueFalseTotal
if (!nb) 0 25111 25111
29 0 return STENCIL_ARENA_NULL;
30 25111 a->base = nb;
31 25111 a->cap = ncap;
32 }
33 3396470 a->used = off + size;
34 3396470 return (uint32_t)off;
35 }
36  
37 12110 void stencil_arena_finalise(stencil_arena *a)
38 {
39 char *nb;
40 12110 ▶ if (!a->used || a->used == a->cap)
Branch: 2 branches
BranchTrueFalseTotal
if (!a->used || a->used == a->cap) 12110 0 12110
if (!a->used || a->used == a->cap) 0 12110 12110
41 0 return;
42 12110 nb = (char *)realloc(a->base, a->used);
43 12110 ▶ if (nb) {
Branch: 1 branch
BranchTrueFalseTotal
if (nb) { 12110 0 12110
44 12110 a->base = nb;
45 12110 a->cap = a->used;
46 }
47 }
48  
49 123926 void stencil_arena_free(stencil_arena *a)
50 {
51 123926 free(a->base);
52 123926 a->base = NULL;
53 123926 a->used = a->cap = 0;
54 123926 }
55  
56 49786 uint64_t stencil_fnv1a(const char *p, size_t n)
57 {
58 49786 uint64_t h = 0xcbf29ce484222325ULL;
59 size_t i;
60 776178 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 726392 49786 776178
61 726392 h ^= (uint8_t)p[i];
62 726392 h *= 0x100000001b3ULL;
63 }
64 49786 return h;
65 }
66  
67 12425 int stencil_intern_init(stencil_intern *t)
68 {
69 12425 t->n_slots = 64;
70 12425 t->n_used = 0;
71 12425 t->slots = (stencil_intern_slot *)
72 12425 calloc(t->n_slots, sizeof(stencil_intern_slot));
73 12425 return t->slots != NULL;
74 }
75  
76 4 static int intern_rehash(stencil_intern *t)
77 {
78 4 uint32_t old_n = t->n_slots;
79 4 stencil_intern_slot *old = t->slots;
80 uint32_t i;
81 4 t->n_slots <<= 1;
82 4 t->slots = (stencil_intern_slot *)
83 4 calloc(t->n_slots, sizeof(stencil_intern_slot));
84 4 ▶ if (!t->slots) {
Branch: 1 branch
BranchTrueFalseTotal
if (!t->slots) { 0 4 4
85 0 t->slots = old;
86 0 t->n_slots = old_n;
87 0 return 0;
88 }
89 964 ▶ for (i = 0; i < old_n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < old_n; i++) { 960 4 964
90 960 ▶ if (old[i].hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (old[i].hash) { 674 286 960
91 674 uint32_t j = (uint32_t)old[i].hash & (t->n_slots - 1);
92 826 ▶ while (t->slots[j].hash)
Branch: 1 branch
BranchTrueFalseTotal
while (t->slots[j].hash) 152 674 826
93 152 j = (j + 1) & (t->n_slots - 1);
94 674 t->slots[j] = old[i];
95 }
96 }
97 4 free(old);
98 4 return 1;
99 }
100  
101 46549 uint32_t stencil_intern_get(stencil_intern *t, stencil_arena *a,
102 const char *bytes, uint32_t len)
103 {
104 46549 uint64_t h = stencil_fnv1a(bytes, len);
105 uint32_t j;
106 46549 ▶ if (!h)
Branch: 1 branch
BranchTrueFalseTotal
if (!h) 0 46549 46549
107 0 h = 1;
108 46549 j = (uint32_t)h & (t->n_slots - 1);
109 47228 ▶ while (t->slots[j].hash) {
Branch: 1 branch
BranchTrueFalseTotal
while (t->slots[j].hash) { 688 46540 47228
110 688 ▶ if (t->slots[j].hash == h && t->slots[j].len == len
Branch: 2 branches
BranchTrueFalseTotal
if (t->slots[j].hash == h && t->slots[j].len == len 9 679 688
if (t->slots[j].hash == h && t->slots[j].len == len 9 0 9
111 9 ▶ && memcmp(stencil_arena_ptr(a, t->slots[j].off), bytes, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(stencil_arena_ptr(a, t->slots[j].off), bytes, len) == 0) 9 0 9
112 9 return t->slots[j].off;
113 679 j = (j + 1) & (t->n_slots - 1);
114 }
115 {
116 46540 uint32_t off = stencil_arena_alloc(a, len, 1);
117 46540 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 46540 46540
118 0 return STENCIL_ARENA_NULL;
119 46540 memcpy(stencil_arena_ptr(a, off), bytes, len);
120 46540 t->slots[j].hash = h;
121 46540 t->slots[j].off = off;
122 46540 t->slots[j].len = len;
123 46540 t->n_used++;
124 /* resize at 70% load */
125 46540 ▶ if (STENCIL_UNLIKELY(t->n_used * 10 >= t->n_slots * 7))
Branch: 1 branch
BranchTrueFalseTotal
if (STENCIL_UNLIKELY(t->n_used * 10 >= t->n_slots * 7)) 4 46536 46540
126 4 ▶ if (!intern_rehash(t))
Branch: 1 branch
BranchTrueFalseTotal
if (!intern_rehash(t)) 0 4 4
127 0 return STENCIL_ARENA_NULL;
128 46540 return off;
129 }
130 }
131  
132 12425 void stencil_intern_free(stencil_intern *t)
133 {
134 12425 free(t->slots);
135 12425 t->slots = NULL;
136 12425 t->n_slots = t->n_used = 0;
137 12425 }
138  
139 /* ---- self test ---------------------------------------------------- */
140  
141 1 const char *stencil_arena_selftest(void)
142 {
143 stencil_arena a;
144 stencil_intern t;
145 uint32_t offs[512];
146 int i;
147  
148 1 ▶ if (!stencil_arena_init(&a, 64))
Branch: 1 branch
BranchTrueFalseTotal
if (!stencil_arena_init(&a, 64)) 0 1 1
149 0 return "arena_init failed";
150  
151 /* alignment */
152 6 ▶ for (i = 0; i < 5; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) { 5 1 6
153 5 size_t align = (size_t)1 << i;
154 5 uint32_t off = stencil_arena_alloc(&a, 3, align);
155 5 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 5 5
156 0 return "alloc failed";
157 5 ▶ if (off & (align - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (off & (align - 1)) 0 5 5
158 0 return "alloc misaligned";
159 }
160  
161 /* offsets stable across growth: write a pattern per block, force
162 * many reallocs, then verify every pattern survived */
163 513 ▶ for (i = 0; i < 512; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 512; i++) { 512 1 513
164 512 offs[i] = stencil_arena_alloc(&a, 97, 8);
165 512 ▶ if (offs[i] == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (offs[i] == STENCIL_ARENA_NULL) 0 512 512
166 0 return "alloc failed during growth";
167 512 memset(stencil_arena_ptr(&a, offs[i]), i & 0xff, 97);
168 }
169 513 ▶ for (i = 0; i < 512; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 512; i++) { 512 1 513
170 512 unsigned char *p = (unsigned char *)stencil_arena_ptr(&a, offs[i]);
171 int j;
172 50176 ▶ for (j = 0; j < 97; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 97; j++) 49664 512 50176
173 49664 ▶ if (p[j] != (i & 0xff))
Branch: 1 branch
BranchTrueFalseTotal
if (p[j] != (i & 0xff)) 0 49664 49664
174 0 return "pattern corrupted by growth";
175 }
176  
177 /* finalise shrink-wraps */
178 1 stencil_arena_finalise(&a);
179 1 ▶ if (a.cap != a.used)
Branch: 1 branch
BranchTrueFalseTotal
if (a.cap != a.used) 0 1 1
180 0 return "finalise did not shrink";
181  
182 /* intern dedup */
183 1 ▶ if (!stencil_intern_init(&t))
Branch: 1 branch
BranchTrueFalseTotal
if (!stencil_intern_init(&t)) 0 1 1
184 0 return "intern_init failed";
185 {
186 1 uint32_t x = stencil_intern_get(&t, &a, "</li></ul>", 10);
187 1 uint32_t y = stencil_intern_get(&t, &a, "</li></ul>", 10);
188 1 uint32_t z = stencil_intern_get(&t, &a, "</li></ol>", 10);
189 1 ▶ if (x == STENCIL_ARENA_NULL || z == STENCIL_ARENA_NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (x == STENCIL_ARENA_NULL || z == STENCIL_ARENA_NULL) 1 0 1
if (x == STENCIL_ARENA_NULL || z == STENCIL_ARENA_NULL) 0 1 1
190 0 return "intern alloc failed";
191 1 ▶ if (x != y)
Branch: 1 branch
BranchTrueFalseTotal
if (x != y) 0 1 1
192 0 return "intern failed to dedup identical bytes";
193 1 ▶ if (x == z)
Branch: 1 branch
BranchTrueFalseTotal
if (x == z) 0 1 1
194 0 return "intern conflated distinct bytes";
195 1 ▶ if (memcmp(stencil_arena_ptr(&a, x), "</li></ul>", 10) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(stencil_arena_ptr(&a, x), "</li></ul>", 10) != 0) 0 1 1
196 0 return "interned bytes corrupt";
197 }
198 /* force rehash */
199 513 ▶ for (i = 0; i < 512; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 512; i++) { 512 1 513
200 char buf[16];
201 512 int n = i;
202 512 int k = 0;
203 512 buf[k++] = 'k';
204 1426 ▶ do { buf[k++] = (char)('0' + (n % 10)); n /= 10; } while (n);
Branch: 1 branch
BranchTrueFalseTotal
do { buf[k++] = (char)('0' + (n % 10)); n /= 10; } while (n); 914 512 1426
205 512 ▶ if (stencil_intern_get(&t, &a, buf, (uint32_t)k)
Branch: 1 branch
BranchTrueFalseTotal
if (stencil_intern_get(&t, &a, buf, (uint32_t)k) 0 512 512
206 == STENCIL_ARENA_NULL)
207 0 return "intern failed during rehash";
208 }
209 2 ▶ if (stencil_intern_get(&t, &a, "</li></ul>", 10)
Branch: 1 branch
BranchTrueFalseTotal
if (stencil_intern_get(&t, &a, "</li></ul>", 10) 0 1 1
210 1 != stencil_intern_get(&t, &a, "</li></ul>", 10))
211 0 return "dedup broken after rehash";
212  
213 1 stencil_intern_free(&t);
214 1 stencil_arena_free(&a);
215 1 ▶ if (a.base || a.cap || a.used)
Branch: 3 branches
BranchTrueFalseTotal
if (a.base || a.cap || a.used) 1 0 1
if (a.base || a.cap || a.used) 1 0 1
if (a.base || a.cap || a.used) 0 1 1
216 0 return "free did not reset";
217 1 return NULL;
218 }
« src/stencil_escape.c ↑ Summary src/stencil_engine.c »