# HG changeset patch # User Mike Becker # Date 1444914110 -7200 # Node ID 08adb4f87160b65821b1115323e65087a28cc239 # Parent 6db9c5d7d7ffc4663acf3f539060156841892b42 more updates to ucx diff -r 6db9c5d7d7ff -r 08adb4f87160 ucx/map.c --- a/ucx/map.c Thu Oct 15 12:46:09 2015 +0200 +++ b/ucx/map.c Thu Oct 15 15:01:50 2015 +0200 @@ -82,6 +82,14 @@ alfree(map->allocator, map); } +void ucx_map_free_content(UcxMap *map, ucx_destructor destr) { + UcxMapIterator iter = ucx_map_iterator(map); + void *val; + UCX_MAP_FOREACH(key, val, iter) { + destr(val); + } +} + void ucx_map_clear(UcxMap *map) { if (map->count == 0) { return; // nothing to do diff -r 6db9c5d7d7ff -r 08adb4f87160 ucx/map.h --- a/ucx/map.h Thu Oct 15 12:46:09 2015 +0200 +++ b/ucx/map.h Thu Oct 15 15:01:50 2015 +0200 @@ -146,22 +146,44 @@ /** * Frees a hash map. * - * Note: the contents are not freed, use an UcxMempool for that - * purpose. + * Note: the contents are not freed, use ucx_map_free_content() + * before calling this function to achieve that. * * @param map the map to be freed + * @see ucx_map_free_content() */ void ucx_map_free(UcxMap *map); /** + * Frees the contents of a hash map. + * + * This is a convenience function that iterates over the map and passes all + * values to the specified destructor function (e.g. stdlib free()). + * + * You must ensure, that it is valid to pass each value in the map to the same + * destructor function. + * + * You should free or clear the map afterwards, as the contents will be invalid. + * + * @param map for which the contents shall be freed + * @param destr pointer to the destructor function + * @see ucx_map_free() + * @see ucx_map_clear() + */ +void ucx_map_free_content(UcxMap *map, ucx_destructor destr); + +/** * Clears a hash map. * - * Note: the contents are not freed. + * Note: the contents are not freed, use ucx_map_free_content() + * before calling this function to achieve that. * - * @param map the map to be freed + * @param map the map to be cleared + * @see ucx_map_free_content() */ void ucx_map_clear(UcxMap *map); + /** * Copies contents from a map to another map using a copy function. * diff -r 6db9c5d7d7ff -r 08adb4f87160 ucx/mempool.h --- a/ucx/mempool.h Thu Oct 15 12:46:09 2015 +0200 +++ b/ucx/mempool.h Thu Oct 15 15:01:50 2015 +0200 @@ -47,13 +47,6 @@ #endif /** - * A function pointer to a destructor function. - * @see ucx_mempool_setdestr() - * @see ucx_mempool_regdestr() - */ -typedef void(*ucx_destructor)(void*); - -/** * UCX mempool structure. */ typedef struct { diff -r 6db9c5d7d7ff -r 08adb4f87160 ucx/ucx.h --- a/ucx/ucx.h Thu Oct 15 12:46:09 2015 +0200 +++ b/ucx/ucx.h Thu Oct 15 15:01:50 2015 +0200 @@ -70,6 +70,14 @@ /** Pointless in C. */ #define UCX_EXTERN #endif + + +/** + * A function pointer to a destructor function. + * @see ucx_mempool_setdestr() + * @see ucx_mempool_regdestr() + */ +typedef void(*ucx_destructor)(void*); /** * Function pointer to a compare function.