hashmap: Add the ability to free keys

Since the hashmap stores a pointer to the key provided at pa_hashmap_put()
time, it make sense to allow the hashmap to be given ownership of the key and
have it free it at pa_hashmap_remove/free time.

To do this cleanly, we now provide the key and value free functions at hashmap
creation time with a pa_hashmap_new_full. With this, we do away with the free
function that was provided at remove/free time for freeing the value.
This commit is contained in:
Arun Raghavan 2013-09-14 11:50:10 +05:30
parent 317b46b571
commit 6825df8cec
41 changed files with 232 additions and 205 deletions

View file

@ -238,7 +238,7 @@ pa_database* pa_database_open(const char *fn, bool for_write) {
if (f || errno == ENOENT) { /* file not found is ok */
db = pa_xnew0(simple_data, 1);
db->map = pa_hashmap_new(hash_func, compare_func);
db->map = pa_hashmap_new_full(hash_func, compare_func, NULL, (pa_free_cb_t) free_entry);
db->filename = pa_xstrdup(path);
db->tmp_filename = pa_sprintf_malloc(".%s.tmp", db->filename);
db->read_only = !for_write;
@ -265,7 +265,7 @@ void pa_database_close(pa_database *database) {
pa_database_sync(database);
pa_xfree(db->filename);
pa_xfree(db->tmp_filename);
pa_hashmap_free(db->map, (pa_free_cb_t) free_entry);
pa_hashmap_free(db->map);
pa_xfree(db);
}
@ -341,7 +341,7 @@ int pa_database_clear(pa_database *database) {
pa_assert(db);
pa_hashmap_remove_all(db->map, (pa_free_cb_t) free_entry);
pa_hashmap_remove_all(db->map);
return 0;
}