include/jsf_arena.h

filter: Statementstmt 100% 65 / 65 Branchbran 70.0% 28 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.5% 93 / 105 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/jsf_interp.h ↑ Summary blib/lib/JSON/Schema/Fast.pm »
1 #ifndef JSF_ARENA_H
2 #define JSF_ARENA_H
3  
4 /* Bump arena: one growable block holds the whole IR (nodes, interned string
5 * pool, prop tables, ref table). Children are referenced by uint32_t OFFSET,
6 * never a pointer, so the block can realloc during build with no fixups
7 * (principle 9). Freed in one shot on DESTROY.
8 *
9 * All functions are C89-clean (declarations at block start) so the module
10 * builds under perl's conservative C dialect on every smoker. */
11  
12 /* Alignment a type actually requires, asked of the compiler rather than
13 * guessed. Every jsf_arena_alloc passes JSF_ALIGNOF(T) for the T it is about
14 * to store: a hardcoded 8 was correct only while NV was a double. Build perl
15 * -Duselongdouble or -Dusequadmath and NV becomes a 16-byte-aligned type, so
16 * jsf_node_t needs 16; at 8 every node landed on an odd 8-byte boundary and
17 * the SSE load gcc emits for __float128 faulted, segfaulting the first
18 * compile() on x86-64. Deriving it from the type keeps that true for any
19 * member anyone adds later. */
20 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
21 # define JSF_ALIGNOF(T) ((uint32_t)_Alignof(T))
22 #elif defined(__GNUC__)
23 # define JSF_ALIGNOF(T) ((uint32_t)__alignof__(T))
24 #elif defined(_MSC_VER)
25 # define JSF_ALIGNOF(T) ((uint32_t)__alignof(T))
26 #else
27 # define JSF_ALIGNOF(T) ((uint32_t)offsetof(struct { char c_; T t_; }, t_))
28 #endif
29  
30 typedef struct jsf_arena {
31 char *base; /* the block */
32 uint32_t used; /* bytes in use (offset 0 reserved as null) */
33 uint32_t cap; /* allocated capacity */
34 uint32_t *itab; /* intern index: offsets of interned strings */
35 uint32_t in; /* interned count */
36 uint32_t icap; /* intern index capacity */
37 } jsf_arena_t;
38  
39 /* An interned string: length prefix, then `len` bytes, then a NUL. */
40 typedef struct jsf_str { uint32_t len; } jsf_str_t;
41  
42 481 static jsf_arena_t *jsf_arena_new(uint32_t cap) {
43 jsf_arena_t *a;
44 481 a = (jsf_arena_t *)malloc(sizeof *a);
45 481 ▶ if (!a) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return NULL; 0 481 481
46 481 ▶ if (cap < 64) cap = 64;
Branch: 1 branch
BranchTrueFalseTotal
if (cap < 64) cap = 64; 1 480 481
47 481 a->base = (char *)malloc(cap);
48 481 ▶ if (!a->base) { free(a); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!a->base) { free(a); return NULL; } 0 481 481
49 481 a->cap = cap;
50 481 a->used = 1; /* reserve offset 0 as the null sentinel */
51 481 a->base[0] = 0;
52 481 a->itab = NULL;
53 481 a->in = 0;
54 481 a->icap = 0;
55 481 return a;
56 }
57  
58 479 static void jsf_arena_free(jsf_arena_t *a) {
59 479 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 0 479 479
60 479 free(a->itab);
61 479 free(a->base);
62 479 free(a);
63 }
64  
65 159 static int jsf__arena_grow(jsf_arena_t *a, uint32_t need) {
66 uint32_t cap;
67 char *nb;
68 159 cap = a->cap;
69 322 ▶ while (cap < need) {
Branch: 1 branch
BranchTrueFalseTotal
while (cap < need) { 163 159 322
70 163 ▶ if (cap > 0x80000000u) return 0; /* would overflow uint32 */
Branch: 1 branch
BranchTrueFalseTotal
if (cap > 0x80000000u) return 0; /* would overflow uint32 */ 0 163 163
71 163 cap <<= 1;
72 }
73 159 ▶ if (cap != a->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (cap != a->cap) { 159 0 159
74 159 nb = (char *)realloc(a->base, cap);
75 159 ▶ if (!nb) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!nb) return 0; 0 159 159
76 159 a->base = nb;
77 159 a->cap = cap;
78 }
79 159 return 1;
80 }
81  
82 /* Allocate `size` bytes at `align`; returns an offset, or JSF_NULL_OFF on OOM/
83 * overflow. The region (and any alignment padding) is zeroed. */
84 4089 static uint32_t jsf_arena_alloc(jsf_arena_t *a, uint32_t size, uint32_t align) {
85 uint32_t off, end;
86 4089 off = a->used;
87 4089 ▶ if (align > 1) off = (off + (align - 1)) & ~(align - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (align > 1) off = (off + (align - 1)) & ~(align - 1); 4089 0 4089
88 4089 end = off + size;
89 4089 ▶ if (end < off) return JSF_NULL_OFF; /* size overflow */
Branch: 1 branch
BranchTrueFalseTotal
if (end < off) return JSF_NULL_OFF; /* size overflow */ 0 4089 4089
90 4089 ▶ if (end > a->cap && !jsf__arena_grow(a, end)) return JSF_NULL_OFF;
Branch: 2 branches
BranchTrueFalseTotal
if (end > a->cap && !jsf__arena_grow(a, end)) return JSF_NULL_OFF; 159 3930 4089
if (end > a->cap && !jsf__arena_grow(a, end)) return JSF_NULL_OFF; 0 159 159
91 4089 memset(a->base + a->used, 0, (size_t)(end - a->used));
92 4089 a->used = end;
93 4089 return off;
94 }
95  
96 /* Resolve an offset to a live pointer. Never cache the result across a call
97 * that might grow the arena - recompute from the offset instead. */
98 9752 static char *jsf_arena_ptr(jsf_arena_t *a, uint32_t off) {
99 9752 return a->base + off;
100 }
101  
102 /* Intern a string; identical (len+bytes) strings dedup to one offset. Returns
103 * the offset of the jsf_str_t header. */
104 3482 static uint32_t jsf_arena_intern(jsf_arena_t *a, const char *s, uint32_t len) {
105 uint32_t i, off;
106 jsf_str_t *st;
107 49902 ▶ for (i = 0; i < a->in; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < a->in; i++) { 47832 2070 49902
108 47832 uint32_t o = a->itab[i];
109 47832 st = (jsf_str_t *)(a->base + o);
110 47832 ▶ if (st->len == len &&
Branch: 1 branch
BranchTrueFalseTotal
if (st->len == len && 3070 44762 47832
111 3070 ▶ memcmp(a->base + o + sizeof(jsf_str_t), s, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
memcmp(a->base + o + sizeof(jsf_str_t), s, len) == 0) 1412 1658 3070
112 1412 return o;
113 }
114 2070 off = jsf_arena_alloc(a, (uint32_t)(sizeof(jsf_str_t) + len + 1),
115 JSF_ALIGNOF(jsf_str_t));
116 2070 ▶ if (off == JSF_NULL_OFF) return JSF_NULL_OFF;
Branch: 1 branch
BranchTrueFalseTotal
if (off == JSF_NULL_OFF) return JSF_NULL_OFF; 0 2070 2070
117 2070 st = (jsf_str_t *)(a->base + off);
118 2070 st->len = len;
119 2070 memcpy(a->base + off + sizeof(jsf_str_t), s, len);
120 2070 a->base[off + sizeof(jsf_str_t) + len] = 0;
121 2070 ▶ if (a->in == a->icap) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->in == a->icap) { 491 1579 2070
122 491 ▶ uint32_t nc = a->icap ? a->icap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
uint32_t nc = a->icap ? a->icap * 2 : 16; 10 481 491
123 491 uint32_t *ni = (uint32_t *)realloc(a->itab, nc * sizeof(uint32_t));
124 491 ▶ if (!ni) return off; /* dedup degrades; still usable */
Branch: 1 branch
BranchTrueFalseTotal
if (!ni) return off; /* dedup degrades; still usable */ 0 491 491
125 491 a->itab = ni;
126 491 a->icap = nc;
127 }
128 2070 a->itab[a->in++] = off;
129 2070 return off;
130 }
131  
132 /* Bytes + length of an interned string. */
133 2155 static const char *jsf_str_bytes(jsf_arena_t *a, uint32_t off, uint32_t *len) {
134 2155 jsf_str_t *st = (jsf_str_t *)(a->base + off);
135 2155 ▶ if (len) *len = st->len;
Branch: 1 branch
BranchTrueFalseTotal
if (len) *len = st->len; 2155 0 2155
136 2155 return a->base + off + sizeof(jsf_str_t);
137 }
138  
139 #endif /* JSF_ARENA_H */
« include/jsf_interp.h ↑ Summary blib/lib/JSON/Schema/Fast.pm »