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

Merge pull request #18243 from ssahani/ensure-put-use

tree-wide: introduce hashmap_ensure_put() and use it
This commit is contained in:
Yu Watanabe 2021-01-17 15:39:41 +09:00 committed by GitHub
commit 16ed53658a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 38 additions and 97 deletions

View File

@ -845,6 +845,16 @@ int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops HASHMAP_DEBU
return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS); return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
} }
int _hashmap_ensure_put(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS) {
int r;
r = _hashmap_ensure_allocated(h, hash_ops HASHMAP_DEBUG_PASS_ARGS);
if (r < 0)
return r;
return hashmap_put(*h, key, value);
}
int _ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS) { int _ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS) {
int r; int r;

View File

@ -133,8 +133,11 @@ HashmapBase* _hashmap_copy(HashmapBase *h HASHMAP_DEBUG_PARAMS);
#define ordered_hashmap_copy(h) ((OrderedHashmap*) _hashmap_copy(HASHMAP_BASE(h) HASHMAP_DEBUG_SRC_ARGS)) #define ordered_hashmap_copy(h) ((OrderedHashmap*) _hashmap_copy(HASHMAP_BASE(h) HASHMAP_DEBUG_SRC_ARGS))
int _hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS); int _hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
int _hashmap_ensure_put(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS);
int _ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS); int _ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
#define hashmap_ensure_allocated(h, ops) _hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS) #define hashmap_ensure_allocated(h, ops) _hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS)
#define hashmap_ensure_put(s, ops, key, value) _hashmap_ensure_put(s, ops, key, value HASHMAP_DEBUG_SRC_ARGS)
#define ordered_hashmap_ensure_allocated(h, ops) _ordered_hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS) #define ordered_hashmap_ensure_allocated(h, ops) _ordered_hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS)
int _ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS); int _ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS);

View File

@ -2656,11 +2656,7 @@ int home_schedule_operation(Home *h, Operation *o, sd_bus_error *error) {
if (ordered_set_size(h->pending_operations) >= PENDING_OPERATIONS_MAX) if (ordered_set_size(h->pending_operations) >= PENDING_OPERATIONS_MAX)
return sd_bus_error_setf(error, BUS_ERROR_TOO_MANY_OPERATIONS, "Too many client operations requested"); return sd_bus_error_setf(error, BUS_ERROR_TOO_MANY_OPERATIONS, "Too many client operations requested");
r = ordered_set_ensure_allocated(&h->pending_operations, &operation_hash_ops); r = ordered_set_ensure_put(&h->pending_operations, &operation_hash_ops, o);
if (r < 0)
return r;
r = ordered_set_put(h->pending_operations, o);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -190,11 +190,7 @@ static int network_new(Context *context, const char *name, Network **ret) {
.dhcp_use_dns = -1, .dhcp_use_dns = -1,
}; };
r = hashmap_ensure_allocated(&context->networks_by_name, &string_hash_ops); r = hashmap_ensure_put(&context->networks_by_name, &string_hash_ops, network->ifname, network);
if (r < 0)
return r;
r = hashmap_put(context->networks_by_name, network->ifname, network);
if (r < 0) if (r < 0)
return r; return r;
@ -247,11 +243,7 @@ static int netdev_new(Context *context, const char *_kind, const char *_ifname,
.ifname = TAKE_PTR(ifname), .ifname = TAKE_PTR(ifname),
}; };
r = hashmap_ensure_allocated(&context->netdevs_by_name, &string_hash_ops); r = hashmap_ensure_put(&context->netdevs_by_name, &string_hash_ops, netdev->ifname, netdev);
if (r < 0)
return r;
r = hashmap_put(context->netdevs_by_name, netdev->ifname, netdev);
if (r < 0) if (r < 0)
return r; return r;
@ -299,11 +291,7 @@ static int link_new(Context *context, const char *name, struct ether_addr *mac,
.mac = *mac, .mac = *mac,
}; };
r = hashmap_ensure_allocated(&context->links_by_name, &string_hash_ops); r = hashmap_ensure_put(&context->links_by_name, &string_hash_ops, link->ifname, link);
if (r < 0)
return r;
r = hashmap_put(context->links_by_name, link->ifname, link);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -756,11 +756,9 @@ int netdev_load_one(Manager *manager, const char *filename) {
netdev->ifname); netdev->ifname);
} }
r = hashmap_ensure_allocated(&netdev->manager->netdevs, &string_hash_ops); r = hashmap_ensure_put(&netdev->manager->netdevs, &string_hash_ops, netdev->ifname, netdev);
if (r < 0) if (r == -ENOMEM)
return r; return log_oom();
r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev);
if (r == -EEXIST) { if (r == -EEXIST) {
NetDev *n = hashmap_get(netdev->manager->netdevs, netdev->ifname); NetDev *n = hashmap_get(netdev->manager->netdevs, netdev->ifname);

View File

@ -91,11 +91,7 @@ static int wireguard_peer_new_static(Wireguard *w, const char *filename, unsigne
LIST_PREPEND(peers, w->peers, peer); LIST_PREPEND(peers, w->peers, peer);
r = hashmap_ensure_allocated(&w->peers_by_section, &network_config_hash_ops); r = hashmap_ensure_put(&w->peers_by_section, &network_config_hash_ops, peer->section, peer);
if (r < 0)
return r;
r = hashmap_put(w->peers_by_section, peer->section, peer);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -55,11 +55,7 @@ static int address_label_new_static(Network *network, const char *filename, unsi
.section = TAKE_PTR(n), .section = TAKE_PTR(n),
}; };
r = hashmap_ensure_allocated(&network->address_labels_by_section, &network_config_hash_ops); r = hashmap_ensure_put(&network->address_labels_by_section, &network_config_hash_ops, label->section, label);
if (r < 0)
return r;
r = hashmap_put(network->address_labels_by_section, label->section, label);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -309,11 +309,9 @@ static int dhcp6_set_pd_route(Link *link, const union in_addr_union *prefix, con
.link = link_ref(link), .link = link_ref(link),
}; };
r = hashmap_ensure_allocated(&link->manager->dhcp6_prefixes, &in6_addr_hash_ops); r = hashmap_ensure_put(&link->manager->dhcp6_prefixes, &in6_addr_hash_ops, &pd->prefix, pd);
if (r < 0) if (r == -ENOMEM)
return log_oom(); return log_oom();
r = hashmap_put(link->manager->dhcp6_prefixes, &pd->prefix, pd);
if (r < 0) if (r < 0)
return log_link_error_errno(link, r, "Failed to store DHCPv6 prefix route at manager: %m"); return log_link_error_errno(link, r, "Failed to store DHCPv6 prefix route at manager: %m");

View File

@ -79,11 +79,7 @@ static int fdb_entry_new_static(
.fdb_ntf_flags = NEIGHBOR_CACHE_ENTRY_FLAGS_SELF, .fdb_ntf_flags = NEIGHBOR_CACHE_ENTRY_FLAGS_SELF,
}; };
r = hashmap_ensure_allocated(&network->fdb_entries_by_section, &network_config_hash_ops); r = hashmap_ensure_put(&network->fdb_entries_by_section, &network_config_hash_ops, fdb_entry->section, fdb_entry);
if (r < 0)
return r;
r = hashmap_put(network->fdb_entries_by_section, fdb_entry->section, fdb_entry);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -449,11 +449,7 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0) if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0)
return -ENOMEM; return -ENOMEM;
r = hashmap_ensure_allocated(&manager->links, NULL); r = hashmap_ensure_put(&manager->links, NULL, INT_TO_PTR(link->ifindex), link);
if (r < 0)
return r;
r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link);
if (r < 0) if (r < 0)
return r; return r;
@ -1605,11 +1601,7 @@ static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
return 0; return 0;
r = hashmap_ensure_allocated(h, NULL); r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
if (r < 0)
return r;
r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -70,11 +70,7 @@ static int mdb_entry_new_static(
.section = TAKE_PTR(n), .section = TAKE_PTR(n),
}; };
r = hashmap_ensure_allocated(&network->mdb_entries_by_section, &network_config_hash_ops); r = hashmap_ensure_put(&network->mdb_entries_by_section, &network_config_hash_ops, mdb_entry->section, mdb_entry);
if (r < 0)
return r;
r = hashmap_put(network->mdb_entries_by_section, mdb_entry->section, mdb_entry);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -60,11 +60,7 @@ static int neighbor_new_static(Network *network, const char *filename, unsigned
.section = TAKE_PTR(n), .section = TAKE_PTR(n),
}; };
r = hashmap_ensure_allocated(&network->neighbors_by_section, &network_config_hash_ops); r = hashmap_ensure_put(&network->neighbors_by_section, &network_config_hash_ops, neighbor->section, neighbor);
if (r < 0)
return r;
r = hashmap_put(network->neighbors_by_section, neighbor->section, neighbor);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -142,11 +142,9 @@ static int network_resolve_stacked_netdevs(Network *network) {
if (r <= 0) if (r <= 0)
continue; continue;
r = hashmap_ensure_allocated(&network->stacked_netdevs, &string_hash_ops); r = hashmap_ensure_put(&network->stacked_netdevs, &string_hash_ops, netdev->ifname, netdev);
if (r < 0) if (r == -ENOMEM)
return log_oom(); return log_oom();
r = hashmap_put(network->stacked_netdevs, netdev->ifname, netdev);
if (r < 0) if (r < 0)
return log_error_errno(r, "%s: Failed to add NetDev '%s' to network: %m", return log_error_errno(r, "%s: Failed to add NetDev '%s' to network: %m",
network->filename, (const char *) name); network->filename, (const char *) name);

View File

@ -79,11 +79,7 @@ static int nexthop_new_static(Network *network, const char *filename, unsigned s
nexthop->network = network; nexthop->network = network;
nexthop->section = TAKE_PTR(n); nexthop->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->nexthops_by_section, &network_config_hash_ops); r = hashmap_ensure_put(&network->nexthops_by_section, &network_config_hash_ops, nexthop->section, nexthop);
if (r < 0)
return r;
r = hashmap_put(network->nexthops_by_section, nexthop->section, nexthop);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -75,11 +75,7 @@ static int prefix_new_static(Network *network, const char *filename, unsigned se
prefix->network = network; prefix->network = network;
prefix->section = TAKE_PTR(n); prefix->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->prefixes_by_section, &network_config_hash_ops); r = hashmap_ensure_put(&network->prefixes_by_section, &network_config_hash_ops, prefix->section, prefix);
if (r < 0)
return r;
r = hashmap_put(network->prefixes_by_section, prefix->section, prefix);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -227,11 +227,7 @@ static int route_new_static(Network *network, const char *filename, unsigned sec
route->network = network; route->network = network;
route->section = TAKE_PTR(n); route->section = TAKE_PTR(n);
r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops); r = hashmap_ensure_put(&network->routes_by_section, &network_config_hash_ops, route->section, route);
if (r < 0)
return r;
r = hashmap_put(network->routes_by_section, route->section, route);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -100,11 +100,7 @@ static int routing_policy_rule_new_static(Network *network, const char *filename
rule->section = TAKE_PTR(n); rule->section = TAKE_PTR(n);
rule->protocol = RTPROT_STATIC; rule->protocol = RTPROT_STATIC;
r = hashmap_ensure_allocated(&network->rules_by_section, &network_config_hash_ops); r = hashmap_ensure_put(&network->rules_by_section, &network_config_hash_ops, rule->section, rule);
if (r < 0)
return r;
r = hashmap_put(network->rules_by_section, rule->section, rule);
if (r < 0) if (r < 0)
return r; return r;

View File

@ -83,11 +83,9 @@ static int parse_interface_with_operstate_range(const char *str) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid interface name '%s'", ifname); "Invalid interface name '%s'", ifname);
r = hashmap_ensure_allocated(&arg_interfaces, &string_hash_ops); r = hashmap_ensure_put(&arg_interfaces, &string_hash_ops, ifname, TAKE_PTR(range));
if (r < 0) if (r == -ENOMEM)
return log_oom(); return log_oom();
r = hashmap_put(arg_interfaces, ifname, TAKE_PTR(range));
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to store interface name: %m"); return log_error_errno(r, "Failed to store interface name: %m");
if (r == 0) if (r == 0)

View File

@ -211,11 +211,7 @@ static int worker_new(struct worker **ret, Manager *manager, sd_device_monitor *
.pid = pid, .pid = pid,
}; };
r = hashmap_ensure_allocated(&manager->workers, &worker_hash_op); r = hashmap_ensure_put(&manager->workers, &worker_hash_op, PID_TO_PTR(pid), worker);
if (r < 0)
return r;
r = hashmap_put(manager->workers, PID_TO_PTR(pid), worker);
if (r < 0) if (r < 0)
return r; return r;