diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-11-12 07:24:11 +0100 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-11-12 08:32:11 +0100 |
commit | 3e5700428e768438bb1083e004be23201ede4e08 (patch) | |
tree | ca5bae7b03fe762e2c78c82b45140651c2a1c1b6 /src/network/networkd-manager.c | |
parent | network: do not assign unused value (diff) | |
download | systemd-3e5700428e768438bb1083e004be23201ede4e08.tar.xz systemd-3e5700428e768438bb1083e004be23201ede4e08.zip |
network: allocate hashmap objects when they are required
Diffstat (limited to 'src/network/networkd-manager.c')
-rw-r--r-- | src/network/networkd-manager.c | 63 |
1 files changed, 28 insertions, 35 deletions
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index ac649863b4..fce8f4610c 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -1213,7 +1213,6 @@ static int manager_dirty_handler(sd_event_source *s, void *userdata) { Link *manager_dhcp6_prefix_get(Manager *m, struct in6_addr *addr) { assert_return(m, NULL); - assert_return(m->dhcp6_prefixes, NULL); assert_return(addr, NULL); return hashmap_get(m->dhcp6_prefixes, addr); @@ -1232,13 +1231,31 @@ static int dhcp6_route_add_handler(sd_netlink *nl, sd_netlink_message *m, void * return 0; } +static void dhcp6_prefixes_hash_func(const void *p, struct siphash *state) { + const struct in6_addr *addr = p; + + assert(p); + + siphash24_compress(addr, sizeof(*addr), state); +} + +static int dhcp6_prefixes_compare_func(const void *_a, const void *_b) { + const struct in6_addr *a = _a, *b = _b; + + return memcmp(a, b, sizeof(*a)); +} + +static const struct hash_ops dhcp6_prefixes_hash_ops = { + .hash = dhcp6_prefixes_hash_func, + .compare = dhcp6_prefixes_compare_func, +}; + int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) { - int r; - Route *route; _cleanup_free_ char *buf = NULL; + Route *route; + int r; assert_return(m, -EINVAL); - assert_return(m->dhcp6_prefixes, -ENODATA); assert_return(addr, -EINVAL); r = route_add(link, AF_INET6, (union in_addr_union *) addr, 64, @@ -1253,6 +1270,10 @@ int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) { (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf); log_link_debug(link, "Adding prefix route %s/64", strnull(buf)); + r = hashmap_ensure_allocated(&m->dhcp6_prefixes, &dhcp6_prefixes_hash_ops); + if (r < 0) + return r; + return hashmap_put(m->dhcp6_prefixes, addr, link); } @@ -1270,13 +1291,12 @@ static int dhcp6_route_remove_handler(sd_netlink *nl, sd_netlink_message *m, voi } static int manager_dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) { + _cleanup_free_ char *buf = NULL; + Route *route; Link *l; int r; - Route *route; - _cleanup_free_ char *buf = NULL; assert_return(m, -EINVAL); - assert_return(m->dhcp6_prefixes, -ENODATA); assert_return(addr, -EINVAL); l = hashmap_remove(m->dhcp6_prefixes, addr); @@ -1300,9 +1320,9 @@ static int manager_dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) { } int manager_dhcp6_prefix_remove_all(Manager *m, Link *link) { + struct in6_addr *addr; Iterator i; Link *l; - struct in6_addr *addr; assert_return(m, -EINVAL); assert_return(link, -EINVAL); @@ -1317,25 +1337,6 @@ int manager_dhcp6_prefix_remove_all(Manager *m, Link *link) { return 0; } -static void dhcp6_prefixes_hash_func(const void *p, struct siphash *state) { - const struct in6_addr *addr = p; - - assert(p); - - siphash24_compress(addr, sizeof(*addr), state); -} - -static int dhcp6_prefixes_compare_func(const void *_a, const void *_b) { - const struct in6_addr *a = _a, *b = _b; - - return memcmp(a, b, sizeof(*a)); -} - -static const struct hash_ops dhcp6_prefixes_hash_ops = { - .hash = dhcp6_prefixes_hash_func, - .compare = dhcp6_prefixes_compare_func, -}; - int manager_new(Manager **ret) { _cleanup_(manager_freep) Manager *m = NULL; int r; @@ -1372,10 +1373,6 @@ int manager_new(Manager **ret) { if (r < 0) return r; - m->netdevs = hashmap_new(&string_hash_ops); - if (!m->netdevs) - return -ENOMEM; - LIST_HEAD_INIT(m->networks); r = sd_resolve_default(&m->resolve); @@ -1390,10 +1387,6 @@ int manager_new(Manager **ret) { if (r < 0) return r; - m->dhcp6_prefixes = hashmap_new(&dhcp6_prefixes_hash_ops); - if (!m->dhcp6_prefixes) - return -ENOMEM; - m->duid.type = DUID_TYPE_EN; (void) routing_policy_load_rules(m->state_file, &m->rules_saved); |