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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2017-11-28 21:30:30 +01:00
parent ec1d290903
commit 224b0e7ad0
19 changed files with 106 additions and 114 deletions
+23
View File
@@ -329,6 +329,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);