1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

Merge pull request #18299 from ssahani/ensure-put

More use of hashmap_ensure_put and ordered_hashmap_ensure_put
This commit is contained in:
Yu Watanabe 2021-01-19 08:42:26 +09:00 committed by GitHub
commit ceed428305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 44 additions and 108 deletions

View File

@ -679,12 +679,6 @@ static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void
return 0;
}
r = set_ensure_allocated(&m->private_buses, NULL);
if (r < 0) {
log_oom();
return 0;
}
r = sd_bus_new(&bus);
if (r < 0) {
log_warning_errno(r, "Failed to allocate new private connection bus: %m");
@ -752,13 +746,17 @@ static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void
return 0;
}
r = set_put(m->private_buses, bus);
r = set_ensure_put(&m->private_buses, NULL, bus);
if (r == -ENOMEM) {
log_oom();
return 0;
}
if (r < 0) {
log_warning_errno(r, "Failed to add new connection bus to set: %m");
return 0;
}
bus = NULL;
TAKE_PTR(bus);
log_debug("Accepted new private connection.");

View File

@ -6069,15 +6069,11 @@ static int exec_runtime_add(
/* tmp_dir, var_tmp_dir, netns_storage_socket fds are donated on success */
r = hashmap_ensure_allocated(&m->exec_runtime_by_id, &string_hash_ops);
if (r < 0)
return r;
r = exec_runtime_allocate(&rt, id);
if (r < 0)
return r;
r = hashmap_put(m->exec_runtime_by_id, rt->id, rt);
r = hashmap_ensure_put(&m->exec_runtime_by_id, &string_hash_ops, rt->id, rt);
if (r < 0)
return r;

View File

@ -648,11 +648,7 @@ static int transaction_apply(
assert(!j->transaction_prev);
assert(!j->transaction_next);
r = hashmap_ensure_allocated(&m->jobs, NULL);
if (r < 0)
return r;
r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j);
r = hashmap_ensure_put(&m->jobs, NULL, UINT32_TO_PTR(j->id), j);
if (r < 0)
goto rollback;
}

View File

@ -149,10 +149,6 @@ static int transfer_new(Manager *m, Transfer **ret) {
if (hashmap_size(m->transfers) >= TRANSFERS_MAX)
return -E2BIG;
r = hashmap_ensure_allocated(&m->transfers, &trivial_hash_ops);
if (r < 0)
return r;
t = new(Transfer, 1);
if (!t)
return -ENOMEM;
@ -171,7 +167,7 @@ static int transfer_new(Manager *m, Transfer **ret) {
if (asprintf(&t->object_path, "/org/freedesktop/import1/transfer/_%" PRIu32, id) < 0)
return -ENOMEM;
r = hashmap_put(m->transfers, UINT32_TO_PTR(id), t);
r = hashmap_ensure_put(&m->transfers, &trivial_hash_ops, UINT32_TO_PTR(id), t);
if (r < 0)
return r;

View File

@ -102,17 +102,13 @@ static int client_context_compare(const void *a, const void *b) {
}
static int client_context_new(Server *s, pid_t pid, ClientContext **ret) {
ClientContext *c;
_cleanup_free_ ClientContext *c = NULL;
int r;
assert(s);
assert(pid_is_valid(pid));
assert(ret);
r = hashmap_ensure_allocated(&s->client_contexts, NULL);
if (r < 0)
return r;
r = prioq_ensure_allocated(&s->client_contexts_lru, client_context_compare);
if (r < 0)
return r;
@ -136,13 +132,11 @@ static int client_context_new(Server *s, pid_t pid, ClientContext **ret) {
.log_ratelimit_burst = s->ratelimit_burst,
};
r = hashmap_put(s->client_contexts, PID_TO_PTR(pid), c);
if (r < 0) {
free(c);
r = hashmap_ensure_put(&s->client_contexts, NULL, PID_TO_PTR(pid), c);
if (r < 0)
return r;
}
*ret = c;
*ret = TAKE_PTR(c);
return 0;
}

View File

@ -234,11 +234,7 @@ int sd_dhcp6_client_add_vendor_option(sd_dhcp6_client *client, sd_dhcp6_option *
assert_return(client, -EINVAL);
assert_return(v, -EINVAL);
r = ordered_hashmap_ensure_allocated(&client->vendor_options, &dhcp6_option_hash_ops);
if (r < 0)
return r;
r = ordered_hashmap_put(client->vendor_options, v, v);
r = ordered_hashmap_ensure_put(&client->vendor_options, &dhcp6_option_hash_ops, v, v);
if (r < 0)
return r;
@ -567,11 +563,7 @@ int sd_dhcp6_client_add_option(sd_dhcp6_client *client, sd_dhcp6_option *v) {
assert_return(client, -EINVAL);
assert_return(v, -EINVAL);
r = ordered_hashmap_ensure_allocated(&client->extra_options, &dhcp6_option_hash_ops);
if (r < 0)
return r;
r = ordered_hashmap_put(client->extra_options, UINT_TO_PTR(v->option), v);
r = ordered_hashmap_ensure_put(&client->extra_options, &dhcp6_option_hash_ops, UINT_TO_PTR(v->option), v);
if (r < 0)
return r;

View File

@ -115,19 +115,11 @@ static int lookup_id(sd_netlink *nl, sd_genl_family family, uint16_t *id) {
if (r < 0)
return r;
r = hashmap_ensure_allocated(&nl->genl_family_to_nlmsg_type, NULL);
r = hashmap_ensure_put(&nl->genl_family_to_nlmsg_type, NULL, INT_TO_PTR(family), UINT_TO_PTR(u));
if (r < 0)
return r;
r = hashmap_ensure_allocated(&nl->nlmsg_type_to_genl_family, NULL);
if (r < 0)
return r;
r = hashmap_put(nl->genl_family_to_nlmsg_type, INT_TO_PTR(family), UINT_TO_PTR(u));
if (r < 0)
return r;
r = hashmap_put(nl->nlmsg_type_to_genl_family, UINT_TO_PTR(u), INT_TO_PTR(family));
r = hashmap_ensure_put(&nl->nlmsg_type_to_genl_family, NULL, UINT_TO_PTR(u), INT_TO_PTR(family));
if (r < 0)
return r;

View File

@ -342,10 +342,6 @@ int config_parse_arp_ip_target_address(
continue;
}
r = ordered_set_ensure_allocated(&b->arp_ip_targets, NULL);
if (r < 0)
return log_oom();
if (ordered_set_size(b->arp_ip_targets) >= NETDEV_BOND_ARP_TARGETS_MAX) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Too many ARP IP targets are specified. The maximum number is %d. Ignoring assignment: %s",
@ -353,7 +349,9 @@ int config_parse_arp_ip_target_address(
continue;
}
r = ordered_set_put(b->arp_ip_targets, UINT32_TO_PTR(ip.in.s_addr));
r = ordered_set_ensure_put(&b->arp_ip_targets, NULL, UINT32_TO_PTR(ip.in.s_addr));
if (r == -ENOMEM)
return log_oom();
if (r == -EEXIST)
log_syntax(unit, LOG_WARNING, filename, line, r,
"Bond ARP IP target address is duplicated, ignoring assignment: %s", n);

View File

@ -143,11 +143,7 @@ static int route_prefix_new_static(Network *network, const char *filename, unsig
prefix->network = network;
prefix->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->route_prefixes_by_section, &network_config_hash_ops);
if (r < 0)
return r;
r = hashmap_put(network->route_prefixes_by_section, prefix->section, prefix);
r = hashmap_ensure_put(&network->route_prefixes_by_section, &network_config_hash_ops, prefix->section, prefix);
if (r < 0)
return r;

View File

@ -15,14 +15,7 @@ int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
assert(m);
assert(ifindex > 0);
r = hashmap_ensure_allocated(&m->links, NULL);
if (r < 0)
return r;
r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops);
if (r < 0)
return r;
assert(ifname);
n = strdup(ifname);
if (!n)
@ -39,11 +32,11 @@ int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
.required_operstate = LINK_OPERSTATE_RANGE_DEFAULT,
};
r = hashmap_put(m->links_by_name, l->ifname, l);
r = hashmap_ensure_put(&m->links, NULL, INT_TO_PTR(ifindex), l);
if (r < 0)
return r;
r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
r = hashmap_ensure_put(&m->links_by_name, &string_hash_ops, l->ifname, l);
if (r < 0)
return r;

View File

@ -31,17 +31,15 @@ static int track_pid(Hashmap **h, const char *path, pid_t pid) {
assert(path);
assert(pid_is_valid(pid));
r = hashmap_ensure_allocated(h, NULL);
if (r < 0)
return log_oom();
c = strdup(path);
if (!c)
return log_oom();
r = hashmap_put(*h, PID_TO_PTR(pid), c);
if (r < 0)
r = hashmap_ensure_put(h, NULL, PID_TO_PTR(pid), c);
if (r == -ENOMEM)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to store pid " PID_FMT, pid);
TAKE_PTR(c);
return 0;

View File

@ -27,10 +27,6 @@ int link_new(Manager *m, Link **ret, int ifindex) {
assert(m);
assert(ifindex > 0);
r = hashmap_ensure_allocated(&m->links, NULL);
if (r < 0)
return r;
l = new(Link, 1);
if (!l)
return -ENOMEM;
@ -48,7 +44,7 @@ int link_new(Manager *m, Link **ret, int ifindex) {
if (asprintf(&l->state_file, "/run/systemd/resolve/netif/%i", ifindex) < 0)
return -ENOMEM;
r = hashmap_put(m->links, INT_TO_PTR(ifindex), l);
r = hashmap_ensure_put(&m->links, NULL, INT_TO_PTR(ifindex), l);
if (r < 0)
return r;
@ -56,7 +52,7 @@ int link_new(Manager *m, Link **ret, int ifindex) {
if (ret)
*ret = l;
l = NULL;
TAKE_PTR(l);
return 0;
}

View File

@ -104,11 +104,7 @@ static int add_process(
if (r < 0)
return r;
r = hashmap_ensure_allocated(&cg->pids, &trivial_hash_ops);
if (r < 0)
return r;
return hashmap_put(cg->pids, PID_TO_PTR(pid), (void*) name);
return hashmap_ensure_put(&cg->pids, &trivial_hash_ops, PID_TO_PTR(pid), (void*) name);
}
static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {

View File

@ -2391,14 +2391,13 @@ int varlink_server_bind_method(VarlinkServer *s, const char *method, VarlinkMeth
if (startswith(method, "org.varlink.service."))
return log_debug_errno(SYNTHETIC_ERRNO(EEXIST), "Cannot bind server to '%s'.", method);
if (hashmap_ensure_allocated(&s->methods, &string_hash_ops) < 0)
return log_oom_debug();
m = strdup(method);
if (!m)
return log_oom_debug();
r = hashmap_put(s->methods, m, callback);
r = hashmap_ensure_put(&s->methods, &string_hash_ops, m, callback);
if (r == -ENOMEM)
return log_oom_debug();
if (r < 0)
return log_debug_errno(r, "Failed to register callback: %m");
if (r > 0)

View File

@ -1298,10 +1298,6 @@ static int add_implicit(void) {
if (!ordered_hashmap_get(users, *m)) {
_cleanup_(item_freep) Item *j = NULL;
r = ordered_hashmap_ensure_allocated(&users, &item_hash_ops);
if (r < 0)
return log_oom();
j = new0(Item, 1);
if (!j)
return log_oom();
@ -1311,22 +1307,20 @@ static int add_implicit(void) {
if (!j->name)
return log_oom();
r = ordered_hashmap_put(users, j->name, j);
if (r < 0)
r = ordered_hashmap_ensure_put(&users, &item_hash_ops, j->name, j);
if (r == -ENOMEM)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to add implicit user '%s': %m", j->name);
log_debug("Adding implicit user '%s' due to m line", j->name);
j = NULL;
TAKE_PTR(j);
}
if (!(ordered_hashmap_get(users, g) ||
ordered_hashmap_get(groups, g))) {
_cleanup_(item_freep) Item *j = NULL;
r = ordered_hashmap_ensure_allocated(&groups, &item_hash_ops);
if (r < 0)
return log_oom();
j = new0(Item, 1);
if (!j)
return log_oom();
@ -1336,12 +1330,14 @@ static int add_implicit(void) {
if (!j->name)
return log_oom();
r = ordered_hashmap_put(groups, j->name, j);
if (r < 0)
r = ordered_hashmap_ensure_put(&groups, &item_hash_ops, j->name, j);
if (r == -ENOMEM)
return log_oom();
if (r < 0)
return log_error_errno(r, "Failed to add implicit group '%s': %m", j->name);
log_debug("Adding implicit group '%s' due to m line", j->name);
j = NULL;
TAKE_PTR(j);
}
}