summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-11-28 12:35:49 +0100
committerSven Eden <yamakuzure@gmx.net>2017-11-28 12:35:49 +0100
commit6c66e1744f4b338d6b2f003920356d63bc6259f3 (patch)
treef29ad59ac6bf74b0a3c6fe3675e3525d7ee557af /src/basic
parent84dc26bac24fa3a916147d375d28f8bc41e7b3b5 (diff)
Add set/hashmap helpers for non-trivial freeing and use where straighforward
A macro is needed because otherwise we couldn't ensure type safety. Some simple tests are included. No functional change intended.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/hashmap.h23
-rw-r--r--src/basic/set.h12
2 files changed, 35 insertions, 0 deletions
diff --git a/src/basic/hashmap.h b/src/basic/hashmap.h
index c1089652d..57809f50b 100644
--- a/src/basic/hashmap.h
+++ b/src/basic/hashmap.h
@@ -328,6 +328,29 @@ static inline void *ordered_hashmap_first(OrderedHashmap *h) {
return internal_hashmap_first(HASHMAP_BASE(h));
}
+#define hashmap_clear_with_destructor(_s, _f) \
+ ({ \
+ void *_item; \
+ while ((_item = hashmap_steal_first(_s))) \
+ _f(_item); \
+ })
+#define hashmap_free_with_destructor(_s, _f) \
+ ({ \
+ hashmap_clear_with_destructor(_s, _f); \
+ hashmap_free(_s); \
+ })
+#define ordered_hashmap_clear_with_destructor(_s, _f) \
+ ({ \
+ void *_item; \
+ while ((_item = ordered_hashmap_steal_first(_s))) \
+ _f(_item); \
+ })
+#define ordered_hashmap_free_with_destructor(_s, _f) \
+ ({ \
+ ordered_hashmap_clear_with_destructor(_s, _f); \
+ ordered_hashmap_free(_s); \
+ })
+
/* no hashmap_next */
void *ordered_hashmap_next(OrderedHashmap *h, const void *key);
diff --git a/src/basic/set.h b/src/basic/set.h
index 08b7c4a72..b424f3423 100644
--- a/src/basic/set.h
+++ b/src/basic/set.h
@@ -109,6 +109,18 @@ static inline void *set_steal_first(Set *s) {
return internal_hashmap_steal_first(HASHMAP_BASE(s));
}
+#define set_clear_with_destructor(_s, _f) \
+ ({ \
+ void *_item; \
+ while ((_item = set_steal_first(_s))) \
+ _f(_item); \
+ })
+#define set_free_with_destructor(_s, _f) \
+ ({ \
+ set_clear_with_destructor(_s, _f); \
+ set_free(_s); \
+ })
+
/* no set_steal_first_key */
/* no set_first_key */