| 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 |
1135353 |
| 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 |
1135353 |
▶ | if (klen > (size_t)-1 - offsetof(hamt_entry, key) - 1) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (klen > (size_t)-1 - offsetof(hamt_entry, key) - 1) return NULL; |
0 |
1135353 |
1135353 |
|
| 34 |
1135353 |
| e = (hamt_entry *)HAMT_ALLOC(cx, HAMT_ENTRY_BYTES(klen)); |
| 35 |
1135353 |
▶ | if (!e) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) return NULL; |
2193 |
1133160 |
1135353 |
|
| 36 |
|
| |
| 37 |
1133160 |
| e->refcnt = 1; |
| 38 |
1133160 |
| e->utf8 = utf8 ? 1 : 0; |
| 39 |
1133160 |
| e->hash = hash; |
| 40 |
1133160 |
| e->val = val; |
| 41 |
1133160 |
| e->klen = klen; |
| 42 |
1133160 |
▶ | if (klen) memcpy(e->key, key, klen); |
Branch: 1 branch
| Branch | True | False | Total |
| if (klen) memcpy(e->key, key, klen); |
1133147 |
13 |
1133160 |
|
| 43 |
1133160 |
| e->key[klen] = '\0'; |
| 44 |
|
| |
| 45 |
1133160 |
▶ | if (val) HAMT_VAL_RETAIN(cx, val); |
Branch: 1 branch
| Branch | True | False | Total |
| if (val) HAMT_VAL_RETAIN(cx, val); |
493031 |
640129 |
1133160 |
|
| 46 |
|
| (void)cx; |
| 47 |
1133160 |
| 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 |
5001932 |
| static hamt_entry *hamt_entry_retain(hamt_entry *e) { |
| 53 |
|
| /* |
| 54 |
|
| # uncoverable branch true |
| 55 |
|
| # uncoverable condition left count:1 */ |
| 56 |
5001932 |
▶ | if (e->refcnt == 0xffffffffU) HAMT_FATAL("an entry's reference count would wrap"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->refcnt == 0xffffffffU) HAMT_FATAL("an entry's reference count would wrap"); |
0 |
5001932 |
5001932 |
|
| 57 |
5001932 |
| e->refcnt++; |
| 58 |
5001932 |
| 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 |
6135092 |
| static void hamt_entry_release(void *cx, hamt_entry *e) { |
| 65 |
|
| void *val; |
| 66 |
|
| |
| 67 |
6135092 |
▶ | if (--e->refcnt) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (--e->refcnt) return; |
5001932 |
1133160 |
6135092 |
|
| 68 |
1133160 |
| val = e->val; |
| 69 |
1133160 |
| HAMT_FREE(cx, e, HAMT_ENTRY_BYTES(e->klen)); |
| 70 |
1133160 |
▶ | if (val) HAMT_VAL_RELEASE(cx, val); |
Branch: 1 branch
| Branch | True | False | Total |
| if (val) HAMT_VAL_RELEASE(cx, val); |
493031 |
640129 |
1133160 |
|
| 71 |
|
| (void)cx; |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
|
| /* Cheapest test first: almost every miss is settled by the hash. */ |
| 75 |
89148508 |
| static int hamt_entry_key_eq(const hamt_entry *e, const char *key, size_t klen, |
| 76 |
|
| int utf8, uint64_t hash) { |
| 77 |
89148508 |
| return e->hash == hash |
| 78 |
88906939 |
▶ | && e->klen == klen |
Branch: 1 branch
| Branch | True | False | Total |
| && e->klen == klen |
47400148 |
41506791 |
88906939 |
|
| 79 |
47400148 |
▶ | && e->utf8 == (utf8 ? 1 : 0) |
Branch: 1 branch
| Branch | True | False | Total |
| && e->utf8 == (utf8 ? 1 : 0) |
47400148 |
0 |
47400148 |
|
| 80 |
178055447 |
▶ | && (klen == 0 || memcmp(e->key, key, klen) == 0); |
Branch: 3 branches
| Branch | True | False | Total |
| && (klen == 0 || memcmp(e->key, key, klen) == 0); |
88906939 |
241569 |
89148508 |
| && (klen == 0 || memcmp(e->key, key, klen) == 0); |
47400134 |
14 |
47400148 |
| && (klen == 0 || memcmp(e->key, key, klen) == 0); |
1828652 |
45571482 |
47400134 |
|
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| #endif |