Merge pull request #10920 from yuwata/hashmap-destructor

hashmap: make hashmap_free() call destructors of key or value
This commit is contained in:
Lennart Poettering
2018-12-03 17:59:44 +01:00
committed by GitHub
59 changed files with 546 additions and 716 deletions
+4 -4
View File
@@ -382,7 +382,7 @@ static int image_flush_cache(sd_event_source *s, void *userdata) {
assert(s);
assert(m);
hashmap_clear_with_destructor(m->image_cache, image_unref);
hashmap_clear(m->image_cache);
return 0;
}
@@ -412,7 +412,7 @@ int image_object_find(sd_bus *bus, const char *path, const char *interface, void
return 1;
}
r = hashmap_ensure_allocated(&m->image_cache, &string_hash_ops);
r = hashmap_ensure_allocated(&m->image_cache, &image_hash_ops);
if (r < 0)
return r;
@@ -461,7 +461,7 @@ char *image_bus_path(const char *name) {
}
int image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
_cleanup_(image_hashmap_freep) Hashmap *images = NULL;
_cleanup_hashmap_free_ Hashmap *images = NULL;
_cleanup_strv_free_ char **l = NULL;
Image *image;
Iterator i;
@@ -471,7 +471,7 @@ int image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char **
assert(path);
assert(nodes);
images = hashmap_new(&string_hash_ops);
images = hashmap_new(&image_hash_ops);
if (!images)
return -ENOMEM;
+4 -3
View File
@@ -72,8 +72,9 @@ fail:
return mfree(m);
}
void machine_free(Machine *m) {
assert(m);
Machine* machine_free(Machine *m) {
if (!m)
return NULL;
while (m->operations)
operation_free(m->operations);
@@ -100,7 +101,7 @@ void machine_free(Machine *m) {
free(m->service);
free(m->root_directory);
free(m->netif);
free(m);
return mfree(m);
}
int machine_save(Machine *m) {
+1 -1
View File
@@ -65,7 +65,7 @@ struct Machine {
};
Machine* machine_new(Manager *manager, MachineClass class, const char *name);
void machine_free(Machine *m);
Machine* machine_free(Machine *m);
bool machine_may_gc(Machine *m, bool drop_not_started);
void machine_add_to_gc_queue(Machine *m);
int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error);
+4 -4
View File
@@ -464,7 +464,7 @@ static int method_get_machine_os_release(sd_bus_message *message, void *userdata
static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(image_hashmap_freep) Hashmap *images = NULL;
_cleanup_hashmap_free_ Hashmap *images = NULL;
Manager *m = userdata;
Image *image;
Iterator i;
@@ -473,7 +473,7 @@ static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_er
assert(message);
assert(m);
images = hashmap_new(&string_hash_ops);
images = hashmap_new(&image_hash_ops);
if (!images)
return -ENOMEM;
@@ -741,7 +741,7 @@ static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_err
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m");
if (r == 0) {
_cleanup_(image_hashmap_freep) Hashmap *images = NULL;
_cleanup_hashmap_free_ Hashmap *images = NULL;
bool success = true;
Image *image;
Iterator i;
@@ -749,7 +749,7 @@ static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_err
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
images = hashmap_new(&string_hash_ops);
images = hashmap_new(&image_hash_ops);
if (!images) {
r = -ENOMEM;
goto child_fail;
+5 -8
View File
@@ -25,6 +25,9 @@
static Manager* manager_unref(Manager *m);
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(machine_hash_ops, void, trivial_hash_func, trivial_compare_func,
Machine, machine_free);
static int manager_new(Manager **ret) {
_cleanup_(manager_unrefp) Manager *m = NULL;
int r;
@@ -37,7 +40,7 @@ static int manager_new(Manager **ret) {
m->machines = hashmap_new(&string_hash_ops);
m->machine_units = hashmap_new(&string_hash_ops);
m->machine_leaders = hashmap_new(NULL);
m->machine_leaders = hashmap_new(&machine_hash_ops);
if (!m->machines || !m->machine_units || !m->machine_leaders)
return -ENOMEM;
@@ -61,8 +64,6 @@ static int manager_new(Manager **ret) {
}
static Manager* manager_unref(Manager *m) {
Machine *machine;
if (!m)
return NULL;
@@ -71,14 +72,10 @@ static Manager* manager_unref(Manager *m) {
assert(m->n_operations == 0);
while ((machine = hashmap_first(m->machines)))
machine_free(machine);
hashmap_free(m->machines);
hashmap_free(m->machine_units);
hashmap_free(m->machine_leaders);
hashmap_free_with_destructor(m->image_cache, image_unref);
hashmap_free(m->image_cache);
sd_event_source_unref(m->image_cache_defer_event);