| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "hash.h" |
| 17 |
|
| 18 |
#include "libc_wrappers.h" |
| 19 |
|
| 20 |
#define EMPTY 0 |
| 21 |
#define TOMBSTONE 1 |
| 22 |
#define INUSE 2 |
| 23 |
|
| 24 |
struct hash_entry_t { |
| 25 |
hash_key_t key; |
| 26 |
hash_data_t data; |
| 27 |
hash_hashvalue_t hash_value; |
| 28 |
struct hash_entry_t *next; |
| 29 |
struct hash_entry_t *prev; |
| 30 |
uint32_t status; |
| 31 |
}; |
| 32 |
|
| 33 |
typedef struct hash_entry_t hash_entry_t; |
| 34 |
|
| 35 |
int create_hashtable(hash_table_t *table, size_t initial_size, |
| 36 |
hash_func_t hashfunc, hash_cmp_t keycmp) { |
| 37 |
hash_entry_t *newtable; |
| 38 |
int entries_per_page; |
| 39 |
|
| 40 |
entries_per_page = gotcha_getpagesize() / sizeof(hash_entry_t); |
| 41 |
if (initial_size % entries_per_page) |
| 42 |
initial_size += entries_per_page - (initial_size % entries_per_page); |
| 43 |
|
| 44 |
newtable = (hash_entry_t *)gotcha_malloc(initial_size * sizeof(hash_entry_t)); |
| 45 |
if (!newtable) return -1; |
| 46 |
gotcha_memset(newtable, 0, initial_size * sizeof(hash_entry_t)); |
| 47 |
|
| 48 |
table->table_size = initial_size; |
| 49 |
table->entry_count = 0; |
| 50 |
table->hashfunc = hashfunc; |
| 51 |
table->keycmp = keycmp; |
| 52 |
table->table = newtable; |
| 53 |
table->head = NULL; |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
static hash_entry_t *insert(hash_table_t *table, hash_key_t key, |
| 59 |
hash_data_t data, hash_hashvalue_t value) { |
| 60 |
unsigned long index = (unsigned long)value % table->table_size; |
| 61 |
unsigned long startindex = index; |
| 62 |
|
| 63 |
hash_entry_t *entry = NULL; |
| 64 |
do { |
| 65 |
entry = table->table + index; |
| 66 |
if (entry->status == EMPTY || entry->status == TOMBSTONE) { |
| 67 |
entry->key = key; |
| 68 |
entry->data = data; |
| 69 |
entry->hash_value = value; |
| 70 |
entry->status = INUSE; |
| 71 |
break; |
| 72 |
} |
| 73 |
index++; |
| 74 |
if (index == table->table_size) index = 0; |
| 75 |
} while (index != startindex); |
| 76 |
|
| 77 |
if (!entry) return NULL; |
| 78 |
|
| 79 |
entry->next = table->head; |
| 80 |
entry->prev = NULL; |
| 81 |
if (table->head) table->head->prev = entry; |
| 82 |
table->head = entry; |
| 83 |
table->entry_count++; |
| 84 |
|
| 85 |
return entry; |
| 86 |
} |
| 87 |
|
| 88 |
int grow_hashtable(hash_table_t *table, size_t new_size) { |
| 89 |
hash_table_t newtable; |
| 90 |
hash_entry_t *result; |
| 91 |
size_t i; |
| 92 |
|
| 93 |
newtable.table_size = new_size; |
| 94 |
newtable.entry_count = 0; |
| 95 |
newtable.hashfunc = table->hashfunc; |
| 96 |
newtable.keycmp = table->keycmp; |
| 97 |
newtable.table = |
| 98 |
(hash_entry_t *)gotcha_malloc(new_size * sizeof(hash_entry_t)); |
| 99 |
newtable.head = NULL; |
| 100 |
gotcha_memset(newtable.table, 0, new_size * sizeof(hash_entry_t)); |
| 101 |
|
| 102 |
for (i = 0; i < table->table_size; i++) { |
| 103 |
if (table->table[i].status == EMPTY || table->table[i].status == TOMBSTONE) |
| 104 |
continue; |
| 105 |
result = insert(&newtable, table->table[i].key, table->table[i].data, |
| 106 |
table->table[i].hash_value); |
| 107 |
if (!result) { |
| 108 |
return -1; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
destroy_hashtable(table); |
| 113 |
*table = newtable; |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
int destroy_hashtable(hash_table_t *table) { |
| 118 |
gotcha_free(table->table); |
| 119 |
table->table_size = 0; |
| 120 |
table->entry_count = 0; |
| 121 |
table->hashfunc = NULL; |
| 122 |
table->keycmp = NULL; |
| 123 |
table->table = NULL; |
| 124 |
table->head = NULL; |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
static int lookup(hash_table_t *table, hash_key_t key, hash_entry_t **entry) { |
| 129 |
size_t index, startindex; |
| 130 |
hash_hashvalue_t hashval; |
| 131 |
|
| 132 |
hashval = table->hashfunc(key); |
| 133 |
index = hashval % table->table_size; |
| 134 |
startindex = index; |
| 135 |
|
| 136 |
for (;;) { |
| 137 |
hash_entry_t *cur = table->table + index; |
| 138 |
if ((cur->status == INUSE) && (cur->hash_value == hashval) && |
| 139 |
(table->keycmp(cur->key, key) == 0)) { |
| 140 |
*entry = cur; |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
if (cur->status == EMPTY) return -1; |
| 145 |
index++; |
| 146 |
if (index == table->table_size) index = 0; |
| 147 |
if (index == startindex) return -1; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
int lookup_hashtable(hash_table_t *table, hash_key_t key, hash_data_t *data) { |
| 152 |
hash_entry_t *entry; |
| 153 |
int result; |
| 154 |
|
| 155 |
result = lookup(table, key, &entry); |
| 156 |
if (result == -1) return -1; |
| 157 |
*data = entry->data; |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
int addto_hashtable(hash_table_t *table, hash_key_t key, hash_data_t data) { |
| 162 |
size_t newsize; |
| 163 |
int result; |
| 164 |
hash_hashvalue_t val; |
| 165 |
hash_entry_t *entry; |
| 166 |
|
| 167 |
newsize = table->table_size; |
| 168 |
while (table->entry_count > newsize / 2) newsize *= 2; |
| 169 |
if (newsize != table->table_size) { |
| 170 |
result = grow_hashtable(table, newsize); |
| 171 |
if (result == -1) return -1; |
| 172 |
} |
| 173 |
|
| 174 |
val = table->hashfunc(key); |
| 175 |
entry = insert(table, key, data, val); |
| 176 |
if (!entry) return -1; |
| 177 |
|
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
int removefrom_hashtable(hash_table_t *table, hash_key_t key) { |
| 182 |
hash_entry_t *entry; |
| 183 |
int result; |
| 184 |
|
| 185 |
result = lookup(table, key, &entry); |
| 186 |
if (result == -1) return -1; |
| 187 |
|
| 188 |
entry->key = NULL; |
| 189 |
entry->data = NULL; |
| 190 |
entry->hash_value = 0; |
| 191 |
entry->status = TOMBSTONE; |
| 192 |
if (entry->next) entry->next->prev = entry->prev; |
| 193 |
if (entry->prev) entry->prev->next = entry->next; |
| 194 |
if (table->head == entry) table->head = entry->next; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
table->entry_count--; |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
int foreach_hash_entry(hash_table_t *table, void *opaque, |
| 203 |
int (*cb)(hash_key_t key, hash_data_t data, |
| 204 |
void *opaque)) { |
| 205 |
int result; |
| 206 |
struct hash_entry_t *i; |
| 207 |
for (i = table->head; i != NULL; i = i->next) { |
| 208 |
result = cb(i->key, i->data, opaque); |
| 209 |
if (result != 0) return result; |
| 210 |
} |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
hash_hashvalue_t strhash(const char *str) { |
| 215 |
unsigned long hash = 5381; |
| 216 |
int c; |
| 217 |
|
| 218 |
while ((c = *str++)) hash = hash * 33 + c; |
| 219 |
|
| 220 |
return (hash_hashvalue_t)hash; |
| 221 |
} |