include/hamt/hamt_entry.h

filter: Statementstmt 100% 27 / 27 Branchbran 95.8% 21 / 24 (2 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 98.0% 48 / 51 (2 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/test.xs ↑ Summary include/hamt/hamt_node.h »
1 #ifndef HAMT_ENTRY_H
2 #define HAMT_ENTRY_H
3  
4 /* One key and its value, in one allocation, shared by every node of every
5 * version that holds it.
6 *
7 * An entry never changes after it is made. Giving a key a new value makes a
8 * new entry and copies the key's bytes: a key is short, and in exchange a
9 * node carries one pointer for each entry and retains one thing when it is
10 * copied. */
11  
12 typedef struct hamt_entry {
13 uint32_t refcnt;
14 uint8_t utf8; /* the key is UTF-8 that could not be downgraded */
15 uint64_t hash;
16 void *val; /* NULL in a set */
17 size_t klen;
18 char key[1]; /* klen bytes, then a NUL */
19 } hamt_entry;
20  
21 #define HAMT_ENTRY_BYTES(klen) (offsetof(hamt_entry, key) + (size_t)(klen) + 1)
22  
23 /* One reference, owned by the caller. The value is retained; the caller
24 * keeps whatever reference it came in with. NULL when there is no memory, in
25 * which case the value was not retained. */
26 1135281 static hamt_entry *hamt_entry_new(void *cx, const char *key, size_t klen,
27 int utf8, uint64_t hash, void *val) {
28 hamt_entry *e;
29  
30 /*
31 # uncoverable branch true
32 # uncoverable condition left count:1 */
33 1135281 ▶ if (klen > (size_t)-1 - offsetof(hamt_entry, key) - 1) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (klen > (size_t)-1 - offsetof(hamt_entry, key) - 1) return NULL; 0 1135281 1135281
34 1135281 e = (hamt_entry *)HAMT_ALLOC(cx, HAMT_ENTRY_BYTES(klen));
35 1135281 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 2193 1133088 1135281
36  
37 1133088 e->refcnt = 1;
38 1133088 e->utf8 = utf8 ? 1 : 0;
39 1133088 e->hash = hash;
40 1133088 e->val = val;
41 1133088 e->klen = klen;
42 1133088 ▶ if (klen) memcpy(e->key, key, klen);
Branch: 1 branch
BranchTrueFalseTotal
if (klen) memcpy(e->key, key, klen); 1133075 13 1133088
43 1133088 e->key[klen] = '\0';
44  
45 1133088 ▶ if (val) HAMT_VAL_RETAIN(cx, val);
Branch: 1 branch
BranchTrueFalseTotal
if (val) HAMT_VAL_RETAIN(cx, val); 492950 640138 1133088
46 (void)cx;
47 1133088 return e;
48 }
49  
50 /* Reaching the limit takes four billion live nodes pointing at one entry.
51 * It stops rather than wrapping to a count that frees the entry under them. */
52 5023849 static hamt_entry *hamt_entry_retain(hamt_entry *e) {
53 /*
54 # uncoverable branch true
55 # uncoverable condition left count:1 */
56 5023849 ▶ if (e->refcnt == 0xffffffffU) HAMT_FATAL("an entry's reference count would wrap");
Branch: 1 branch
BranchTrueFalseTotal
if (e->refcnt == 0xffffffffU) HAMT_FATAL("an entry's reference count would wrap"); 0 5023849 5023849
57 5023849 e->refcnt++;
58 5023849 return e;
59 }
60  
61 /* The order matters. Releasing the value can run code that comes back into
62 * this library, and by then the entry must be gone: so the value's pointer
63 * is taken out, the entry is freed, and the value is released last. */
64 6156937 static void hamt_entry_release(void *cx, hamt_entry *e) {
65 void *val;
66  
67 6156937 ▶ if (--e->refcnt) return;
Branch: 1 branch
BranchTrueFalseTotal
if (--e->refcnt) return; 5023849 1133088 6156937
68 1133088 val = e->val;
69 1133088 HAMT_FREE(cx, e, HAMT_ENTRY_BYTES(e->klen));
70 1133088 ▶ if (val) HAMT_VAL_RELEASE(cx, val);
Branch: 1 branch
BranchTrueFalseTotal
if (val) HAMT_VAL_RELEASE(cx, val); 492950 640138 1133088
71 (void)cx;
72 }
73  
74 /* Cheapest test first: almost every miss is settled by the hash. */
75 89906269 static int hamt_entry_key_eq(const hamt_entry *e, const char *key, size_t klen,
76 int utf8, uint64_t hash) {
77 89906269 return e->hash == hash
78 89662397 ▶ && e->klen == klen
Branch: 1 branch
BranchTrueFalseTotal
&& e->klen == klen 47695146 41967251 89662397
79 47695146 ▶ && e->utf8 == (utf8 ? 1 : 0)
Branch: 1 branch
BranchTrueFalseTotal
&& e->utf8 == (utf8 ? 1 : 0) 47695146 0 47695146
80 179568666 ▶ && (klen == 0 || memcmp(e->key, key, klen) == 0);
Branch: 3 branches
BranchTrueFalseTotal
&& (klen == 0 || memcmp(e->key, key, klen) == 0); 89662397 243872 89906269
&& (klen == 0 || memcmp(e->key, key, klen) == 0); 47695132 14 47695146
&& (klen == 0 || memcmp(e->key, key, klen) == 0); 1830706 45864426 47695132
81 }
82  
83 #endif
« xs/test.xs ↑ Summary include/hamt/hamt_node.h »