static int device_set_sysfs(Device *d, const char *sysfs) { Device *first; char *copy; int r; assert(d); if (streq_ptr(d->sysfs, sysfs)) return 0; r = hashmap_ensure_allocated(&UNIT(d)->manager->devices_by_sysfs, &string_hash_ops); if (r < 0) return r; copy = strdup(sysfs); if (!copy) return -ENOMEM; device_unset_sysfs(d); first = hashmap_get(UNIT(d)->manager->devices_by_sysfs, sysfs); LIST_PREPEND(same_sysfs, first, d); r = hashmap_replace(UNIT(d)->manager->devices_by_sysfs, copy, first); if (r < 0) { LIST_REMOVE(same_sysfs, first, d); free(copy); return r; } d->sysfs = copy; return 0; }
void cgroup_bonding_free(CGroupBonding *b, bool trim) { assert(b); if (b->unit) { CGroupBonding *f; LIST_REMOVE(CGroupBonding, by_unit, b->unit->cgroup_bondings, b); if (streq(b->controller, SYSTEMD_CGROUP_CONTROLLER)) { assert_se(f = hashmap_get(b->unit->manager->cgroup_bondings, b->path)); LIST_REMOVE(CGroupBonding, by_path, f, b); if (f) hashmap_replace(b->unit->manager->cgroup_bondings, b->path, f); else hashmap_remove(b->unit->manager->cgroup_bondings, b->path); } } if (b->realized && b->ours && trim) cg_trim(b->controller, b->path, false); free(b->controller); free(b->path); free(b); }
static void test_hashmap_replace(void) { Hashmap *m; char *val1, *val2, *val3, *val4, *val5, *r; log_info("%s", __func__); m = hashmap_new(&string_hash_ops); val1 = strdup("val1"); assert_se(val1); val2 = strdup("val2"); assert_se(val2); val3 = strdup("val3"); assert_se(val3); val4 = strdup("val4"); assert_se(val4); val5 = strdup("val5"); assert_se(val5); hashmap_put(m, "key 1", val1); hashmap_put(m, "key 2", val2); hashmap_put(m, "key 3", val3); hashmap_put(m, "key 4", val4); hashmap_replace(m, "key 3", val1); r = hashmap_get(m, "key 3"); assert_se(streq(r, "val1")); hashmap_replace(m, "key 5", val5); r = hashmap_get(m, "key 5"); assert_se(streq(r, "val5")); free(val1); free(val2); free(val3); free(val4); free(val5); hashmap_free(m); }
static int match_new(Client *c, struct bus_match_component *components, unsigned n_components, Match **_m) { Match *m, *first; int r; assert(c); assert(_m); r = hashmap_ensure_allocated(&c->matches, string_hash_func, string_compare_func); if (r < 0) return r; m = new0(Match, 1); if (!m) return -ENOMEM; m->match = bus_match_to_string(components, n_components); if (!m->match) { r = -ENOMEM; goto fail; } m->cookie = ++c->next_cookie; first = hashmap_get(c->matches, m->match); LIST_PREPEND(matches, first, m); r = hashmap_replace(c->matches, m->match, first); if (r < 0) { LIST_REMOVE(matches, first, m); goto fail; } m->client = c; c->n_matches++; *_m = m; m = NULL; return 0; fail: match_free(m); return r; }
static void dns_cache_item_unlink_and_free(DnsCache *c, DnsCacheItem *i) { DnsCacheItem *first; assert(c); if (!i) return; first = hashmap_get(c->by_key, i->key); LIST_REMOVE(by_key, first, i); if (first) assert_se(hashmap_replace(c->by_key, first->key, first) >= 0); else hashmap_remove(c->by_key, i->key); prioq_remove(c->by_expiry, i, &i->prioq_idx); dns_cache_item_free(i); }
static void match_free(Match *m) { if (!m) return; if (m->client) { Match *first; first = hashmap_get(m->client->matches, m->match); LIST_REMOVE(matches, first, m); if (first) assert_se(hashmap_replace(m->client->matches, first->match, first) >= 0); else hashmap_remove(m->client->matches, m->match); m->client->n_matches--; } free(m->match); free(m); }
int set_replace(Set *s, void *value) { return hashmap_replace(MAKE_HASHMAP(s), value, value); }