| 1 |
|
| /* |
| 2 |
|
| * This is a customized version of the pointer table implementation in sv.c |
| 3 |
|
| */ |
| 4 |
|
| |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| |
| 7 |
|
| #if PTRSIZE == 8 |
| 8 |
|
| /* |
| 9 |
|
| * This is one of Thomas Wang's hash functions for 64-bit integers from: |
| 10 |
|
| * https://gist.github.com/badboy/6267743 |
| 11 |
|
| */ |
| 12 |
1530 |
| U32 ptr_hash(PTRV u) { |
| 13 |
1530 |
| u = (~u) + (u << 18); |
| 14 |
1530 |
| u = u ^ (u >> 31); |
| 15 |
1530 |
| u = u * 21; |
| 16 |
1530 |
| u = u ^ (u >> 11); |
| 17 |
1530 |
| u = u + (u << 6); |
| 18 |
1530 |
| u = u ^ (u >> 22); |
| 19 |
1530 |
| return (U32)u; |
| 20 |
|
| } |
| 21 |
|
| #else |
| 22 |
|
| /* |
| 23 |
|
| * This is one of Bob Jenkins' hash functions for 32-bit integers |
| 24 |
|
| * from: https://burtleburtle.net/bob/hash/integer.html |
| 25 |
|
| */ |
| 26 |
|
| U32 ptr_hash(PTRV u) { |
| 27 |
|
| u = (u + 0x7ed55d16) + (u << 12); |
| 28 |
|
| u = (u ^ 0xc761c23c) ^ (u >> 19); |
| 29 |
|
| u = (u + 0x165667b1) + (u << 5); |
| 30 |
|
| u = (u + 0xd3a2646c) ^ (u << 9); |
| 31 |
|
| u = (u + 0xfd7046c5) + (u << 3); |
| 32 |
|
| u = (u ^ 0xb55a4f09) ^ (u >> 16); |
| 33 |
|
| return u; |
| 34 |
|
| } |
| 35 |
|
| #endif |
| 36 |
|
| |
| 37 |
|
| #define PTABLE_HASH(ptr) ptr_hash(PTR2nat(ptr)) |
| 38 |
|
| |
| 39 |
|
| struct PTABLE_entry { |
| 40 |
|
| struct PTABLE_entry *next; |
| 41 |
|
| void *key; |
| 42 |
|
| void *value; |
| 43 |
|
| }; |
| 44 |
|
| |
| 45 |
|
| struct PTABLE { |
| 46 |
|
| struct PTABLE_entry **tbl_ary; |
| 47 |
|
| UV tbl_max; |
| 48 |
|
| UV tbl_items; |
| 49 |
|
| }; |
| 50 |
|
| |
| 51 |
|
| typedef struct PTABLE_entry PTABLE_ENTRY_t; |
| 52 |
|
| typedef struct PTABLE PTABLE_t; |
| 53 |
|
| |
| 54 |
|
| static PTABLE_t * PTABLE_new(void); |
| 55 |
|
| static PTABLE_ENTRY_t * PTABLE_find(PTABLE_t *tbl, const void *key); |
| 56 |
|
| static void * PTABLE_fetch(PTABLE_t *tbl, const void *key); |
| 57 |
|
| static void PTABLE_store(PTABLE_t *tbl, void *key, void *value); |
| 58 |
|
| static void PTABLE_grow(PTABLE_t *tbl); |
| 59 |
|
| static void PTABLE_clear(PTABLE_t *tbl); |
| 60 |
|
| static void PTABLE_free(PTABLE_t *tbl); |
| 61 |
|
| |
| 62 |
|
| /* create a new pointer => pointer table */ |
| 63 |
|
| |
| 64 |
|
| static PTABLE_t * |
| 65 |
23 |
| PTABLE_new(void) |
| 66 |
|
| { |
| 67 |
|
| PTABLE_t *tbl; |
| 68 |
23 |
| Newxz(tbl, 1, PTABLE_t); |
| 69 |
23 |
| tbl->tbl_max = 511; |
| 70 |
23 |
| tbl->tbl_items = 0; |
| 71 |
23 |
▶ | Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTABLE_ENTRY_t*); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTABLE_ENTRY_t*); |
0 |
23 |
23 |
|
| 72 |
23 |
| return tbl; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| /* map an existing pointer using a table */ |
| 76 |
|
| |
| 77 |
|
| static PTABLE_ENTRY_t * |
| 78 |
1014 |
| PTABLE_find(PTABLE_t *tbl, const void *key) { |
| 79 |
|
| PTABLE_ENTRY_t *tblent; |
| 80 |
1014 |
| const UV hash = PTABLE_HASH(key); |
| 81 |
1014 |
| tblent = tbl->tbl_ary[hash & tbl->tbl_max]; |
| 82 |
1175 |
▶ | for (; tblent; tblent = tblent->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; tblent; tblent = tblent->next) { |
659 |
516 |
1175 |
|
| 83 |
659 |
▶ | if (tblent->key == key) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tblent->key == key) |
498 |
161 |
659 |
|
| 84 |
498 |
| return tblent; |
| 85 |
|
| } |
| 86 |
516 |
| return NULL; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| static void * |
| 90 |
498 |
| PTABLE_fetch(PTABLE_t *tbl, const void *key) |
| 91 |
|
| { |
| 92 |
498 |
| PTABLE_ENTRY_t const *const tblent = PTABLE_find(tbl, key); |
| 93 |
498 |
▶ | return tblent ? tblent->value : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return tblent ? tblent->value : NULL; |
498 |
0 |
498 |
|
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
|
| /* add a new entry to a pointer => pointer table */ |
| 97 |
|
| |
| 98 |
|
| static void |
| 99 |
516 |
| PTABLE_store(PTABLE_t *tbl, void *key, void *value) |
| 100 |
|
| { |
| 101 |
516 |
| PTABLE_ENTRY_t *tblent = PTABLE_find(tbl, key); |
| 102 |
|
| |
| 103 |
516 |
▶ | if (tblent) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tblent) { |
0 |
516 |
516 |
|
| 104 |
0 |
| tblent->value = value; |
| 105 |
|
| } else { |
| 106 |
516 |
| const UV entry = PTABLE_HASH(key) & tbl->tbl_max; |
| 107 |
516 |
| Newx(tblent, 1, PTABLE_ENTRY_t); |
| 108 |
|
| |
| 109 |
516 |
| tblent->key = key; |
| 110 |
516 |
| tblent->value = value; |
| 111 |
516 |
| tblent->next = tbl->tbl_ary[entry]; |
| 112 |
516 |
| tbl->tbl_ary[entry] = tblent; |
| 113 |
516 |
| tbl->tbl_items++; |
| 114 |
516 |
▶ | if (tblent->next && (tbl->tbl_items > tbl->tbl_max)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (tblent->next && (tbl->tbl_items > tbl->tbl_max)) |
65 |
451 |
516 |
| if (tblent->next && (tbl->tbl_items > tbl->tbl_max)) |
0 |
65 |
65 |
|
| 115 |
0 |
| PTABLE_grow(tbl); |
| 116 |
|
| } |
| 117 |
516 |
| } |
| 118 |
|
| |
| 119 |
|
| /* double the hash bucket size of an existing ptr table */ |
| 120 |
|
| |
| 121 |
|
| static void |
| 122 |
0 |
| PTABLE_grow(PTABLE_t *tbl) |
| 123 |
|
| { |
| 124 |
0 |
| PTABLE_ENTRY_t **ary = tbl->tbl_ary; |
| 125 |
0 |
| const UV oldsize = tbl->tbl_max + 1; |
| 126 |
0 |
| UV newsize = oldsize * 2; |
| 127 |
|
| UV i; |
| 128 |
|
| |
| 129 |
0 |
▶ | Renew(ary, newsize, PTABLE_ENTRY_t*); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(ary, newsize, PTABLE_ENTRY_t*); |
0 |
0 |
0 |
|
| 130 |
0 |
▶ | Zero(&ary[oldsize], newsize - oldsize, PTABLE_ENTRY_t*); |
Branch: 1 branch
| Branch | True | False | Total |
| Zero(&ary[oldsize], newsize - oldsize, PTABLE_ENTRY_t*); |
0 |
0 |
0 |
|
| 131 |
0 |
| tbl->tbl_max = --newsize; |
| 132 |
0 |
| tbl->tbl_ary = ary; |
| 133 |
|
| |
| 134 |
0 |
▶ | for (i = 0; i < oldsize; i++, ary++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < oldsize; i++, ary++) { |
0 |
0 |
0 |
|
| 135 |
|
| PTABLE_ENTRY_t **curentp, **entp, *ent; |
| 136 |
0 |
▶ | if (!*ary) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*ary) |
0 |
0 |
0 |
|
| 137 |
0 |
| continue; |
| 138 |
0 |
| curentp = ary + oldsize; |
| 139 |
0 |
▶ | for (entp = ary, ent = *ary; ent; ent = *entp) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (entp = ary, ent = *ary; ent; ent = *entp) { |
0 |
0 |
0 |
|
| 140 |
0 |
▶ | if ((newsize & PTABLE_HASH(ent->key)) != i) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((newsize & PTABLE_HASH(ent->key)) != i) { |
0 |
0 |
0 |
|
| 141 |
0 |
| *entp = ent->next; |
| 142 |
0 |
| ent->next = *curentp; |
| 143 |
0 |
| *curentp = ent; |
| 144 |
0 |
| continue; |
| 145 |
|
| } else { |
| 146 |
0 |
| entp = &ent->next; |
| 147 |
|
| } |
| 148 |
|
| } |
| 149 |
|
| } |
| 150 |
0 |
| } |
| 151 |
|
| |
| 152 |
|
| /* remove all the entries from a ptr table */ |
| 153 |
|
| |
| 154 |
|
| static void |
| 155 |
23 |
| PTABLE_clear(PTABLE_t *tbl) |
| 156 |
|
| { |
| 157 |
23 |
▶ | if (tbl && tbl->tbl_items) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tbl && tbl->tbl_items) { |
23 |
0 |
23 |
| if (tbl && tbl->tbl_items) { |
16 |
7 |
23 |
|
| 158 |
16 |
| register PTABLE_ENTRY_t * * const array = tbl->tbl_ary; |
| 159 |
16 |
| UV riter = tbl->tbl_max; |
| 160 |
|
| |
| 161 |
|
| do { |
| 162 |
8192 |
| PTABLE_ENTRY_t *entry = array[riter]; |
| 163 |
|
| |
| 164 |
8708 |
▶ | while (entry) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (entry) { |
516 |
8192 |
8708 |
|
| 165 |
516 |
| PTABLE_ENTRY_t * const oentry = entry; |
| 166 |
516 |
| entry = entry->next; |
| 167 |
516 |
| Safefree(oentry); |
| 168 |
|
| } |
| 169 |
|
| |
| 170 |
|
| /* chocolateboy 2008-01-08 |
| 171 |
|
| * |
| 172 |
|
| * make sure we clear the array entry, so that subsequent probes fail |
| 173 |
|
| */ |
| 174 |
|
| |
| 175 |
8192 |
| array[riter] = NULL; |
| 176 |
8192 |
▶ | } while (riter--); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (riter--); |
8176 |
16 |
8192 |
|
| 177 |
|
| |
| 178 |
16 |
| tbl->tbl_items = 0; |
| 179 |
|
| } |
| 180 |
23 |
| } |
| 181 |
|
| |
| 182 |
|
| /* clear and free a ptr table */ |
| 183 |
|
| |
| 184 |
|
| static void |
| 185 |
23 |
| PTABLE_free(PTABLE_t *tbl) |
| 186 |
|
| { |
| 187 |
23 |
▶ | if (!tbl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tbl) { |
0 |
23 |
23 |
|
| 188 |
0 |
| return; |
| 189 |
|
| } |
| 190 |
23 |
| PTABLE_clear(tbl); |
| 191 |
23 |
| Safefree(tbl->tbl_ary); |
| 192 |
23 |
| Safefree(tbl); |
| 193 |
|
| } |