chiark / gitweb /
binfmt,tmpfiles,modules-load,sysctl: rework the various early-boot services that...
[elogind.git] / src / shared / hashmap.c
index be37a3659d17a1d73bd4084d1f7af685c7050d41..9f7db3439728b7e89ed409ef7acfa2861f5d8e36 100644 (file)
@@ -103,7 +103,7 @@ static void deallocate_tile(void **first_tile, void *p) {
         *first_tile = p;
 }
 
-#ifndef __OPTIMIZE__
+#ifdef VALGRIND
 
 static void drop_pool(struct pool *p) {
         while (p) {
@@ -147,6 +147,25 @@ int trivial_compare_func(const void *a, const void *b) {
         return a < b ? -1 : (a > b ? 1 : 0);
 }
 
+unsigned uint64_hash_func(const void *p) {
+        uint64_t u;
+
+        assert_cc(sizeof(uint64_t) == 2*sizeof(unsigned));
+
+        u = *(const uint64_t*) p;
+
+        return (unsigned) ((u >> 32) ^ u);
+}
+
+int uint64_compare_func(const void *_a, const void *_b) {
+        uint64_t a, b;
+
+        a = *(const uint64_t*) _a;
+        b = *(const uint64_t*) _b;
+
+        return a < b ? -1 : (a > b ? 1 : 0);
+}
+
 Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) {
         bool b;
         Hashmap *h;
@@ -265,6 +284,8 @@ static void remove_entry(Hashmap *h, struct hashmap_entry *e) {
 
 void hashmap_free(Hashmap*h) {
 
+        /* Free the hashmap, but nothing in it */
+
         if (!h)
                 return;
 
@@ -277,6 +298,10 @@ void hashmap_free(Hashmap*h) {
 }
 
 void hashmap_free_free(Hashmap *h) {
+
+        /* Free the hashmap and all data objects in it, but not the
+         * keys */
+
         if (!h)
                 return;
 
@@ -284,6 +309,17 @@ void hashmap_free_free(Hashmap *h) {
         hashmap_free(h);
 }
 
+void hashmap_free_free_free(Hashmap *h) {
+
+        /* Free the hashmap and all data and key objects in it */
+
+        if (!h)
+                return;
+
+        hashmap_clear_free_free(h);
+        hashmap_free(h);
+}
+
 void hashmap_clear(Hashmap *h) {
         if (!h)
                 return;
@@ -302,6 +338,22 @@ void hashmap_clear_free(Hashmap *h) {
                 free(p);
 }
 
+void hashmap_clear_free_free(Hashmap *h) {
+        if (!h)
+                return;
+
+        while (h->iterate_list_head) {
+                void *a, *b;
+
+                a = h->iterate_list_head->value;
+                b = (void*) h->iterate_list_head->key;
+                remove_entry(h, h->iterate_list_head);
+                free(a);
+                free(b);
+        }
+}
+
+
 static struct hashmap_entry *hash_scan(Hashmap *h, unsigned hash, const void *key) {
         struct hashmap_entry *e;
         assert(h);
@@ -322,7 +374,8 @@ int hashmap_put(Hashmap *h, const void *key, void *value) {
 
         hash = h->hash_func(key) % NBUCKETS;
 
-        if ((e = hash_scan(h, hash, key))) {
+        e = hash_scan(h, hash, key);
+        if (e) {
 
                 if (e->value == value)
                         return 0;
@@ -353,8 +406,8 @@ int hashmap_replace(Hashmap *h, const void *key, void *value) {
         assert(h);
 
         hash = h->hash_func(key) % NBUCKETS;
-
-        if ((e = hash_scan(h, hash, key))) {
+        e = hash_scan(h, hash, key);
+        if (e) {
                 e->key = key;
                 e->value = value;
                 return 0;
@@ -363,6 +416,21 @@ int hashmap_replace(Hashmap *h, const void *key, void *value) {
         return hashmap_put(h, key, value);
 }
 
+int hashmap_update(Hashmap *h, const void *key, void *value) {
+        struct hashmap_entry *e;
+        unsigned hash;
+
+        assert(h);
+
+        hash = h->hash_func(key) % NBUCKETS;
+        e = hash_scan(h, hash, key);
+        if (!e)
+                return -ENOENT;
+
+        e->value = value;
+        return 0;
+}
+
 void* hashmap_get(Hashmap *h, const void *key) {
         unsigned hash;
         struct hashmap_entry *e;
@@ -371,23 +439,40 @@ void* hashmap_get(Hashmap *h, const void *key) {
                 return NULL;
 
         hash = h->hash_func(key) % NBUCKETS;
+        e = hash_scan(h, hash, key);
+        if (!e)
+                return NULL;
 
-        if (!(e = hash_scan(h, hash, key)))
+        return e->value;
+}
+
+void* hashmap_get2(Hashmap *h, const void *key, void **key2) {
+        unsigned hash;
+        struct hashmap_entry *e;
+
+        if (!h)
+                return NULL;
+
+        hash = h->hash_func(key) % NBUCKETS;
+        e = hash_scan(h, hash, key);
+        if (!e)
                 return NULL;
 
+        if (key2)
+                *key2 = (void*) e->key;
+
         return e->value;
 }
 
 bool hashmap_contains(Hashmap *h, const void *key) {
         unsigned hash;
-        struct hashmap_entry *e;
 
         if (!h)
                 return false;
 
         hash = h->hash_func(key) % NBUCKETS;
 
-        if (!(e = hash_scan(h, hash, key)))
+        if (!hash_scan(h, hash, key))
                 return false;
 
         return true;
@@ -753,3 +838,25 @@ char **hashmap_get_strv(Hashmap *h) {
 
         return sv;
 }
+
+void *hashmap_next(Hashmap *h, const void *key) {
+        unsigned hash;
+        struct hashmap_entry *e;
+
+        assert(h);
+        assert(key);
+
+        if (!h)
+                return NULL;
+
+        hash = h->hash_func(key) % NBUCKETS;
+        e = hash_scan(h, hash, key);
+        if (!e)
+                return NULL;
+
+        e = e->iterate_next;
+        if (!e)
+                return NULL;
+
+        return e->value;
+}