1 #include <stdlib.h> 2 #include <string.h> 3 4 #include "ghash.h" 5 6 /** Remove an entry from a \c ghash table. This function attempts to do 7 * the minimum amount of rebuilding necessary to adjust the positions of 8 * entries that may fall in the same slot as the newly removed entry. 9 * 10 * \returns True if the entry was found (and removed), false otherwise. 11 */ ghash_remove(struct ghash * d,const void * key)12int ghash_remove(struct ghash* d, const void* key) 13 { 14 void* entry; 15 void** slot; 16 unsigned i; 17 18 /* First, locate the key in the hash table. */ 19 if ((slot = ghash_find(d, key)) == 0) 20 return 0; 21 entry = *slot; 22 23 /* Then free it, and empty out its slot. */ 24 if (d->keyfree != 0) 25 d->keyfree(ghash_entry_keyptr(entry)); 26 if (d->datafree != 0) 27 d->datafree(ghash_entry_dataptr(entry, d->keysize)); 28 free(entry); 29 *slot = 0; 30 --d->count; 31 32 /* Finally, make sure all subsequent entries are properly inserted. */ 33 for (i = (slot - d->table + 1) % d->size; 34 (entry = d->table[i]) != 0; 35 i = (i + 1) % d->size) { 36 --d->count; 37 d->table[i] = 0; 38 ghash_insert(d, entry); 39 } 40 return 1; 41 } 42