basic/set: let set_put_strdup() create the set with string hash ops
If we're using a set with _put_strdup(), most of the time we want to use string hash ops on the set, and free the strings when done. This defines the appropriate a new string_hash_ops_free structure to automatically free the keys when removing the set, and makes set_put_strdup() and set_put_strdupv() instantiate the set with those hash ops. hashmap_put_strdup() was already doing something similar. (It is OK to instantiate the set earlier, possibly with a different hash ops structure. set_put_strdup() will then use the existing set. It is also OK to call set_free_free() instead of set_free() on a set with string_hash_ops_free, the effect is the same, we're just overriding the override of the cleanup function.) No functional change intended.
This commit is contained in:
@@ -1695,14 +1695,10 @@ int bus_introspect_implementations(
|
||||
if (impl != main_impl)
|
||||
bus_introspect_implementation(&intro, impl);
|
||||
|
||||
_cleanup_set_free_free_ Set *nodes = NULL;
|
||||
_cleanup_set_free_ Set *nodes = NULL;
|
||||
|
||||
for (size_t i = 0; impl->children && impl->children[i]; i++) {
|
||||
r = set_ensure_allocated(&nodes, &string_hash_ops);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
r = set_put_strdup(nodes, impl->children[i]->path);
|
||||
r = set_put_strdup(&nodes, impl->children[i]->path);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ void bus_wait_for_jobs_free(BusWaitForJobs *d) {
|
||||
if (!d)
|
||||
return;
|
||||
|
||||
set_free_free(d->jobs);
|
||||
set_free(d->jobs);
|
||||
|
||||
sd_bus_slot_unref(d->slot_disconnected);
|
||||
sd_bus_slot_unref(d->slot_job_removed);
|
||||
@@ -315,15 +315,9 @@ int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_ar
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path) {
|
||||
int r;
|
||||
|
||||
assert(d);
|
||||
|
||||
r = set_ensure_allocated(&d->jobs, &string_hash_ops);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return set_put_strdup(d->jobs, path);
|
||||
return set_put_strdup(&d->jobs, path);
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet) {
|
||||
|
||||
+9
-14
@@ -1133,30 +1133,25 @@ int show_journal_entry(
|
||||
const size_t highlight[2],
|
||||
bool *ellipsized) {
|
||||
|
||||
int ret;
|
||||
_cleanup_set_free_free_ Set *fields = NULL;
|
||||
_cleanup_set_free_ Set *fields = NULL;
|
||||
int r;
|
||||
|
||||
assert(mode >= 0);
|
||||
assert(mode < _OUTPUT_MODE_MAX);
|
||||
|
||||
if (n_columns <= 0)
|
||||
n_columns = columns();
|
||||
|
||||
if (output_fields) {
|
||||
fields = set_new(&string_hash_ops);
|
||||
if (!fields)
|
||||
return log_oom();
|
||||
r = set_put_strdupv(&fields, output_fields);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
ret = set_put_strdupv(fields, output_fields);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
}
|
||||
r = output_funcs[mode](f, j, mode, n_columns, flags, fields, highlight);
|
||||
|
||||
ret = output_funcs[mode](f, j, mode, n_columns, flags, fields, highlight);
|
||||
|
||||
if (ellipsized && ret > 0)
|
||||
if (ellipsized && r > 0)
|
||||
*ellipsized = true;
|
||||
|
||||
return ret;
|
||||
return r;
|
||||
}
|
||||
|
||||
static int maybe_print_begin_newline(FILE *f, OutputFlags *flags) {
|
||||
|
||||
@@ -239,7 +239,7 @@ int bind_remount_recursive_with_mountinfo(
|
||||
}
|
||||
|
||||
if (!set_contains(done, path)) {
|
||||
r = set_put_strdup(todo, path);
|
||||
r = set_put_strdup(&todo, path);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
@@ -266,7 +266,7 @@ int bind_remount_recursive_with_mountinfo(
|
||||
|
||||
log_debug("Made top-level directory %s a mount point.", prefix);
|
||||
|
||||
r = set_put_strdup(done, simplified);
|
||||
r = set_put_strdup(&done, simplified);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -463,7 +463,7 @@ int unit_file_find_fragment(
|
||||
|
||||
/* The unit always has its own name if it's not a template. */
|
||||
if (IN_SET(name_type, UNIT_NAME_PLAIN, UNIT_NAME_INSTANCE)) {
|
||||
r = set_put_strdup(names, unit_name);
|
||||
r = set_put_strdup(&names, unit_name);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
@@ -493,7 +493,7 @@ int unit_file_find_fragment(
|
||||
if (!streq(unit_name, *t))
|
||||
log_debug("%s: %s has alias %s", __func__, unit_name, *t);
|
||||
|
||||
r = set_put_strdup(names, *t);
|
||||
r = set_put_strdup(&names, *t);
|
||||
}
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
Reference in New Issue
Block a user