| 1 |
|
| /* |
| 2 |
|
| * chocolateboy 2009-02-25 |
| 3 |
|
| * |
| 4 |
|
| * This is a customised version of the pointer table implementation in sv.c |
| 5 |
|
| */ |
| 6 |
|
| |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #if PTRSIZE == 8 |
| 10 |
|
| /* |
| 11 |
|
| * This is one of Thomas Wang's hash functions for 64-bit integers from: |
| 12 |
|
| * http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 13 |
|
| */ |
| 14 |
0 |
| U32 hash(PTRV u) { |
| 15 |
0 |
| u = (~u) + (u << 18); |
| 16 |
0 |
| u = u ^ (u >> 31); |
| 17 |
0 |
| u = u * 21; |
| 18 |
0 |
| u = u ^ (u >> 11); |
| 19 |
0 |
| u = u + (u << 6); |
| 20 |
0 |
| u = u ^ (u >> 22); |
| 21 |
0 |
| return (U32)u; |
| 22 |
|
| } |
| 23 |
|
| #else |
| 24 |
|
| /* |
| 25 |
|
| * This is one of Bob Jenkins' hash functions for 32-bit integers |
| 26 |
|
| * from: http://burtleburtle.net/bob/hash/integer.html |
| 27 |
|
| */ |
| 28 |
|
| U32 hash(PTRV u) { |
| 29 |
|
| u = (u + 0x7ed55d16) + (u << 12); |
| 30 |
|
| u = (u ^ 0xc761c23c) ^ (u >> 19); |
| 31 |
|
| u = (u + 0x165667b1) + (u << 5); |
| 32 |
|
| u = (u + 0xd3a2646c) ^ (u << 9); |
| 33 |
|
| u = (u + 0xfd7046c5) + (u << 3); |
| 34 |
|
| u = (u ^ 0xb55a4f09) ^ (u >> 16); |
| 35 |
|
| return u; |
| 36 |
|
| } |
| 37 |
|
| #endif |
| 38 |
|
| |
| 39 |
|
| #define OPTABLE_HASH(op) hash(PTR2nat(op)) |
| 40 |
|
| |
| 41 |
|
| typedef struct OPTableEntry { |
| 42 |
|
| struct OPTableEntry *next; |
| 43 |
|
| const OP *key; |
| 44 |
|
| OPAnnotation *value; |
| 45 |
|
| } OPTableEntry; |
| 46 |
|
| |
| 47 |
|
| typedef struct OPAnnotationGroupImpl { |
| 48 |
|
| struct OPTableEntry **array; |
| 49 |
|
| UV size; |
| 50 |
|
| UV items; |
| 51 |
|
| NV threshold; |
| 52 |
|
| } OPTable; |
| 53 |
|
| |
| 54 |
|
| typedef void (*OPTableEntryValueDtor)(pTHX_ OPAnnotation *); |
| 55 |
|
| |
| 56 |
|
| STATIC OPAnnotation *OPTable_delete(OPTable *table, OP *op); |
| 57 |
|
| STATIC OPAnnotation *OPTable_fetch(OPTable *table, const OP *key); |
| 58 |
|
| STATIC OPAnnotation * OPTable_store(OPTable *table, const OP *key, OPAnnotation *value); |
| 59 |
|
| STATIC OPTableEntry *OPTable_find(OPTable *table, const OP *key); |
| 60 |
|
| STATIC OPTable *OPTable_new(UV size, NV threshold); |
| 61 |
|
| STATIC void OPTable_clear(pTHX_ OPTable *table, OPTableEntryValueDtor dtor); |
| 62 |
|
| STATIC void OPTable_free(OPTable *table); |
| 63 |
|
| STATIC void OPTable_grow(OPTable *table); |
| 64 |
|
| |
| 65 |
0 |
| STATIC OPTable * OPTable_new(UV size, NV threshold) { |
| 66 |
|
| OPTable *table; |
| 67 |
|
| |
| 68 |
0 |
▶ | if ((size < 2) || (size & (size - 1))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((size < 2) || (size & (size - 1))) { |
0 |
0 |
0 |
| if ((size < 2) || (size & (size - 1))) { |
0 |
0 |
0 |
|
| 69 |
0 |
| croak(__PACKAGE__ ": invalid op table size: expected a power of 2 (>= 2), got %u", (unsigned)size); |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
0 |
▶ | if (!((threshold > 0) && (threshold < 1))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!((threshold > 0) && (threshold < 1))) { |
0 |
0 |
0 |
| if (!((threshold > 0) && (threshold < 1))) { |
0 |
0 |
0 |
|
| 73 |
0 |
| croak(__PACKAGE__ ": invalid threshold: expected 0.0 < threshold < 1.0, got %f", threshold); |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
0 |
| Newxz(table, 1, OPTable); |
| 77 |
|
| |
| 78 |
0 |
| table->size = size; |
| 79 |
0 |
| table->threshold = threshold; |
| 80 |
0 |
| table->items = 0; |
| 81 |
|
| |
| 82 |
0 |
▶ | Newxz(table->array, size, OPTableEntry *); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(table->array, size, OPTableEntry *); |
0 |
0 |
0 |
|
| 83 |
|
| |
| 84 |
0 |
| return table; |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
0 |
| STATIC OPTableEntry * OPTable_find(OPTable *table, const OP *key) { |
| 88 |
|
| OPTableEntry *entry; |
| 89 |
0 |
| UV index = OPTABLE_HASH(key) & (table->size - 1); |
| 90 |
|
| |
| 91 |
0 |
▶ | for (entry = table->array[index]; entry; entry = entry->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (entry = table->array[index]; entry; entry = entry->next) { |
0 |
0 |
0 |
|
| 92 |
0 |
▶ | if (entry->key == key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->key == key) { |
0 |
0 |
0 |
|
| 93 |
0 |
| break; |
| 94 |
|
| } |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
0 |
| return entry; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
0 |
| STATIC OPAnnotation *OPTable_delete(OPTable *table, OP *op) { |
| 101 |
0 |
| OPTableEntry *entry, *prev = NULL; |
| 102 |
0 |
| OPAnnotation *annotation = NULL; |
| 103 |
0 |
| UV index = OPTABLE_HASH(op) & (table->size - 1); |
| 104 |
|
| |
| 105 |
0 |
▶ | for (entry = table->array[index]; entry; prev = entry, entry = entry->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (entry = table->array[index]; entry; prev = entry, entry = entry->next) { |
0 |
0 |
0 |
|
| 106 |
0 |
▶ | if (entry->key == op) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->key == op) { |
0 |
0 |
0 |
|
| 107 |
|
| |
| 108 |
0 |
▶ | if (prev) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (prev) { |
0 |
0 |
0 |
|
| 109 |
0 |
| prev->next = entry->next; |
| 110 |
|
| } else { |
| 111 |
0 |
| table->array[index] = entry->next; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
0 |
| --(table->items); |
| 115 |
0 |
| annotation = entry->value; |
| 116 |
0 |
| Safefree(entry); |
| 117 |
0 |
| break; |
| 118 |
|
| } |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
0 |
| return annotation; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
0 |
| STATIC OPAnnotation * OPTable_fetch(OPTable *table, const OP *key) { |
| 125 |
0 |
| OPTableEntry const * const entry = OPTable_find(table, key); |
| 126 |
|
| |
| 127 |
0 |
▶ | return entry ? entry->value : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return entry ? entry->value : NULL; |
0 |
0 |
0 |
|
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
0 |
| STATIC OPAnnotation * OPTable_store(OPTable *table, const OP *key, OPAnnotation *value) { |
| 131 |
0 |
| OPAnnotation *annotation = NULL; |
| 132 |
0 |
| OPTableEntry *entry = OPTable_find(table, key); |
| 133 |
|
| |
| 134 |
0 |
▶ | if (entry) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry) { |
0 |
0 |
0 |
|
| 135 |
0 |
| annotation = entry->value; |
| 136 |
0 |
| entry->value = value; |
| 137 |
|
| } else { |
| 138 |
0 |
| const UV index = OPTABLE_HASH(key) & (table->size - 1); |
| 139 |
0 |
| Newx(entry, 1, OPTableEntry); |
| 140 |
|
| |
| 141 |
0 |
| entry->key = key; |
| 142 |
0 |
| entry->value = value; |
| 143 |
0 |
| entry->next = table->array[index]; |
| 144 |
|
| |
| 145 |
0 |
| table->array[index] = entry; |
| 146 |
0 |
| ++(table->items); |
| 147 |
|
| |
| 148 |
0 |
▶ | if (((NV)table->items / (NV)table->size) > table->threshold) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (((NV)table->items / (NV)table->size) > table->threshold) { |
0 |
0 |
0 |
|
| 149 |
0 |
| OPTable_grow(table); |
| 150 |
|
| } |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
0 |
| return annotation; |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
|
| /* double the size of the array */ |
| 157 |
0 |
| STATIC void OPTable_grow(OPTable *table) { |
| 158 |
0 |
| OPTableEntry **array = table->array; |
| 159 |
0 |
| const UV oldsize = table->size; |
| 160 |
0 |
| UV newsize = oldsize * 2; |
| 161 |
|
| UV i; |
| 162 |
|
| |
| 163 |
0 |
▶ | Renew(array, newsize, OPTableEntry*); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(array, newsize, OPTableEntry*); |
0 |
0 |
0 |
|
| 164 |
0 |
▶ | Zero(&array[oldsize], newsize - oldsize, OPTableEntry*); |
Branch: 1 branch
| Branch | True | False | Total |
| Zero(&array[oldsize], newsize - oldsize, OPTableEntry*); |
0 |
0 |
0 |
|
| 165 |
0 |
| table->size = newsize; |
| 166 |
0 |
| table->array = array; |
| 167 |
|
| |
| 168 |
0 |
▶ | for (i = 0; i < oldsize; ++i, ++array) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < oldsize; ++i, ++array) { |
0 |
0 |
0 |
|
| 169 |
|
| OPTableEntry **current_entry_ptr, **entry_ptr, *entry; |
| 170 |
|
| |
| 171 |
0 |
▶ | if (!*array) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*array) { |
0 |
0 |
0 |
|
| 172 |
0 |
| continue; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
0 |
| current_entry_ptr = array + oldsize; |
| 176 |
|
| |
| 177 |
0 |
▶ | for (entry_ptr = array, entry = *array; entry; entry = *entry_ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (entry_ptr = array, entry = *array; entry; entry = *entry_ptr) { |
0 |
0 |
0 |
|
| 178 |
0 |
| UV index = OPTABLE_HASH(entry->key) & (newsize - 1); |
| 179 |
|
| |
| 180 |
0 |
▶ | if (index != i) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (index != i) { |
0 |
0 |
0 |
|
| 181 |
0 |
| *entry_ptr = entry->next; |
| 182 |
0 |
| entry->next = *current_entry_ptr; |
| 183 |
0 |
| *current_entry_ptr = entry; |
| 184 |
0 |
| continue; |
| 185 |
|
| } else { |
| 186 |
0 |
| entry_ptr = &entry->next; |
| 187 |
|
| } |
| 188 |
|
| } |
| 189 |
|
| } |
| 190 |
0 |
| } |
| 191 |
|
| |
| 192 |
0 |
| STATIC void OPTable_clear(pTHX_ OPTable *table, OPTableEntryValueDtor dtor) { |
| 193 |
0 |
▶ | if (table && table->items) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (table && table->items) { |
0 |
0 |
0 |
| if (table && table->items) { |
0 |
0 |
0 |
|
| 194 |
0 |
| OPTableEntry ** const array = table->array; |
| 195 |
0 |
| UV riter = table->size - 1; |
| 196 |
|
| |
| 197 |
|
| do { |
| 198 |
0 |
| OPTableEntry *entry = array[riter]; |
| 199 |
|
| |
| 200 |
0 |
▶ | while (entry) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (entry) { |
0 |
0 |
0 |
|
| 201 |
0 |
| OPTableEntry * const temp = entry; |
| 202 |
0 |
| entry = entry->next; |
| 203 |
0 |
| dtor(aTHX_ temp->value); |
| 204 |
0 |
| Safefree(temp); |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| /* chocolateboy 2008-01-08 |
| 208 |
|
| * |
| 209 |
|
| * make sure we clear the array entry, so that subsequent probes fail |
| 210 |
|
| */ |
| 211 |
|
| |
| 212 |
0 |
| array[riter] = NULL; |
| 213 |
0 |
▶ | } while (riter--); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (riter--); |
0 |
0 |
0 |
|
| 214 |
|
| |
| 215 |
0 |
| table->items = 0; |
| 216 |
|
| } |
| 217 |
0 |
| } |
| 218 |
|
| |
| 219 |
0 |
| STATIC void OPTable_free(OPTable *table) { |
| 220 |
0 |
▶ | if (table) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (table) { |
0 |
0 |
0 |
|
| 221 |
0 |
| Safefree(table); |
| 222 |
|
| } |
| 223 |
0 |
| } |