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 27106 int stencil_arena_init(stencil_arena *a, size_t initial_cap)
6 {
7 27106 ▶ if (initial_cap < 64)
Branch: 1 branch
BranchTrueFalseTotal
if (initial_cap < 64) 0 27106 27106
8 0 initial_cap = 64;
9 27106 a->base = (char *)malloc(initial_cap);
10 27106 ▶ if (!a->base)
Branch: 1 branch
BranchTrueFalseTotal
if (!a->base) 0 27106 27106
11 0 return 0;
12 27106 a->used = 0;
13 27106 a->cap = initial_cap;
14 27106 return 1;
15 }
16  
17 535428 uint32_t stencil_arena_alloc(stencil_arena *a, size_t size, size_t align)
18 {
19 535428 size_t off = (a->used + (align - 1)) & ~(align - 1);
20 535428 ▶ 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)) 535428 0 535428
if (STENCIL_UNLIKELY(off + size < off || off + size > (uint32_t)-2)) 0 535428 535428
21 0 return STENCIL_ARENA_NULL; /* offset space exhausted */
22 535428 ▶ if (STENCIL_UNLIKELY(off + size > a->cap)) {
Branch: 1 branch
BranchTrueFalseTotal
if (STENCIL_UNLIKELY(off + size > a->cap)) { 3338 532090 535428
23 3338 size_t ncap = a->cap;
24 char *nb;
25 6701 ▶ while (ncap < off + size)
Branch: 1 branch
BranchTrueFalseTotal
while (ncap < off + size) 3363 3338 6701
26 3363 ncap <<= 1;
27 3338 nb = (char *)realloc(a->base, ncap);
28 3338 ▶ if (!nb)
Branch: 1 branch
BranchTrueFalseTotal
if (!nb) 0 3338 3338
29 0 return STENCIL_ARENA_NULL;
30 3338 a->base = nb;
31 3338 a->cap = ncap;
32 }
33 535428 a->used = off + size;
34 535428 return (uint32_t)off;
35 }
36  
37 2428 void stencil_arena_finalise(stencil_arena *a)
38 {
39 char *nb;
40 2428 ▶ if (!a->used || a->used == a->cap)
Branch: 2 branches
BranchTrueFalseTotal
if (!a->used || a->used == a->cap) 2428 0 2428
if (!a->used || a->used == a->cap) 0 2428 2428
41 0 return;
42 2428 nb = (char *)realloc(a->base, a->used);
43 2428 ▶ if (nb) {
Branch: 1 branch
BranchTrueFalseTotal
if (nb) { 2428 0 2428
44 2428 a->base = nb;
45 2428 a->cap = a->used;
46 }
47 }
48  
49 27106 void stencil_arena_free(stencil_arena *a)
50 {
51 27106 free(a->base);
52 27106 a->base = NULL;
53 27106 a->used = a->cap = 0;
54 27106 }
55  
56 11058 uint64_t stencil_fnv1a(const char *p, size_t n)
57 {
58 11058 uint64_t h = 0xcbf29ce484222325ULL;
59 size_t i;
60 582538 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 571480 11058 582538
61 571480 h ^= (uint8_t)p[i];
62 571480 h *= 0x100000001b3ULL;
63 }
64 11058 return h;
65 }
66  
67 2743 int stencil_intern_init(stencil_intern *t)
68 {
69 2743 t->n_slots = 64;
70 2743 t->n_used = 0;
71 2743 t->slots = (stencil_intern_slot *)
72 2743 calloc(t->n_slots, sizeof(stencil_intern_slot));
73 2743 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 7821 uint32_t stencil_intern_get(stencil_intern *t, stencil_arena *a,
102 const char *bytes, uint32_t len)
103 {
104 7821 uint64_t h = stencil_fnv1a(bytes, len);
105 uint32_t j;
106 7821 ▶ if (!h)
Branch: 1 branch
BranchTrueFalseTotal
if (!h) 0 7821 7821
107 0 h = 1;
108 7821 j = (uint32_t)h & (t->n_slots - 1);
109 8500 ▶ while (t->slots[j].hash) {
Branch: 1 branch
BranchTrueFalseTotal
while (t->slots[j].hash) { 688 7812 8500
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 7812 uint32_t off = stencil_arena_alloc(a, len, 1);
117 7812 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 7812 7812
118 0 return STENCIL_ARENA_NULL;
119 7812 memcpy(stencil_arena_ptr(a, off), bytes, len);
120 7812 t->slots[j].hash = h;
121 7812 t->slots[j].off = off;
122 7812 t->slots[j].len = len;
123 7812 t->n_used++;
124 /* resize at 70% load */
125 7812 ▶ 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 7808 7812
126 4 ▶ if (!intern_rehash(t))
Branch: 1 branch
BranchTrueFalseTotal
if (!intern_rehash(t)) 0 4 4
127 0 return STENCIL_ARENA_NULL;
128 7812 return off;
129 }
130 }
131  
132 2743 void stencil_intern_free(stencil_intern *t)
133 {
134 2743 free(t->slots);
135 2743 t->slots = NULL;
136 2743 t->n_slots = t->n_used = 0;
137 2743 }
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 »