/* Finalize (free) a hash table */ void ght_finalize(ght_hash_table_t *p_ht) { unsigned int i; DEBUG_ASSERT(p_ht); if (p_ht->pp_entries) { /* For each bucket, free all entries */ for (i=0; i<p_ht->i_size; i++) { free_entry_chain(p_ht, p_ht->pp_entries[i]); p_ht->pp_entries[i] = NULL; } DefaultFreeFunction (p_ht->pp_entries); p_ht->pp_entries = NULL; } if (p_ht->p_nr) { DefaultFreeFunction (p_ht->p_nr); p_ht->p_nr = NULL; } DefaultFreeFunction (p_ht); }
/* Finalize (free) a hash table */ void ght_finalize(ght_hash_table_t *p_ht) { int i; assert(p_ht); if (p_ht->pp_entries) { /* For each bucket, free all entries */ for (i=0; i<p_ht->i_size; i++) { free_entry_chain(p_ht, p_ht->pp_entries[i]); p_ht->pp_entries[i] = NULL; } free (p_ht->pp_entries); p_ht->pp_entries = NULL; } if (p_ht->p_nr) { free (p_ht->p_nr); p_ht->p_nr = NULL; } free (p_ht); }
/* Rehash the hash table (i.e. change its size and reinsert all * items). This operation is slow and should not be used frequently. */ void ght_rehash(ght_hash_table_t *p_ht, unsigned int i_size) { ght_hash_table_t *p_tmp; ght_iterator_t iterator; const void *p_key; void *p; unsigned int i; DEBUG_ASSERT(p_ht); /* Recreate the hash table with the new size */ p_tmp = ght_create(i_size); DEBUG_ASSERT(p_tmp); /* Set the flags for the new hash table */ ght_set_hash(p_tmp, p_ht->fn_hash); ght_set_alloc(p_tmp, p_ht->fn_alloc, p_ht->fn_free); ght_set_heuristics(p_tmp, GHT_HEURISTICS_NONE); ght_set_rehash(p_tmp, FALSE); /* Walk through all elements in the table and insert them into the temporary one. */ for (p = ght_first(p_ht, &iterator, &p_key); p; p = ght_next(p_ht, &iterator, &p_key)) { DEBUG_ASSERT(iterator.p_entry); /* Insert the entry into the new table */ if (ght_insert(p_tmp, iterator.p_entry->p_data, iterator.p_entry->key.i_size, iterator.p_entry->key.p_key) < 0) { LOG_ERROR("Out of memory error or entry already in hash table when rehashing (internal error)\n"); } } /* Remove the old table... */ for (i=0; i<p_ht->i_size; i++) { if (p_ht->pp_entries[i]) { /* Delete the entries in the bucket */ free_entry_chain (p_ht, p_ht->pp_entries[i]); p_ht->pp_entries[i] = NULL; } } DefaultFreeFunction (p_ht->pp_entries); DefaultFreeFunction (p_ht->p_nr); /* ... and replace it with the new */ p_ht->i_size = p_tmp->i_size; p_ht->i_size_mask = p_tmp->i_size_mask; p_ht->i_items = p_tmp->i_items; p_ht->pp_entries = p_tmp->pp_entries; p_ht->p_nr = p_tmp->p_nr; p_ht->p_oldest = p_tmp->p_oldest; p_ht->p_newest = p_tmp->p_newest; /* Clean up */ p_tmp->pp_entries = NULL; p_tmp->p_nr = NULL; DefaultFreeFunction (p_tmp); }