Oops, it makes no sense to dispose the old value of a key when the function at the same time returns the value.
This patch fixes it. 2018-12-11 Bruno Haible <br...@clisp.org> omap: Don't dispose the old value when the function returns it. * lib/gl_array_omap.c (gl_array_remove_at): Don't invoke the vdispose_fn here. * lib/gl_avltree_omap.c (NODE_PAYLOAD_DISPOSE): Likewise. * lib/gl_rbtree_omap.c (NODE_PAYLOAD_DISPOSE): Likewise. * lib/gl_omap.h (gl_omap_nx_put, gl_omap_remove): Invoke the vdispose_fn here. diff --git a/lib/gl_array_omap.c b/lib/gl_array_omap.c index a2b21bc..3b65269 100644 --- a/lib/gl_array_omap.c +++ b/lib/gl_array_omap.c @@ -220,25 +220,6 @@ gl_array_nx_add_at (gl_omap_t map, size_t position, return 1; } -/* Remove the pair at the given position, - 0 <= position < gl_omap_size (map). */ -static void -gl_array_remove_at (gl_omap_t map, size_t position) -{ - size_t count = map->count; - struct pair *pairs; - size_t i; - - pairs = map->pairs; - if (map->base.vdispose_fn != NULL) - map->base.vdispose_fn (pairs[position].value); - if (map->base.kdispose_fn != NULL) - map->base.kdispose_fn (pairs[position].key); - for (i = position + 1; i < count; i++) - pairs[i - 1] = pairs[i]; - map->count = count - 1; -} - static int gl_array_nx_getput (gl_omap_t map, const void *key, const void *value, const void **oldvaluep) @@ -279,6 +260,23 @@ gl_array_nx_getput (gl_omap_t map, const void *key, const void *value, return gl_array_nx_add_at (map, low, key, value); } +/* Remove the pair at the given position, + 0 <= position < gl_omap_size (map). */ +static void +gl_array_remove_at (gl_omap_t map, size_t position) +{ + size_t count = map->count; + struct pair *pairs; + size_t i; + + pairs = map->pairs; + if (map->base.kdispose_fn != NULL) + map->base.kdispose_fn (pairs[position].key); + for (i = position + 1; i < count; i++) + pairs[i - 1] = pairs[i]; + map->count = count - 1; +} + static bool gl_array_getremove (gl_omap_t map, const void *key, const void **oldvaluep) { diff --git a/lib/gl_avltree_omap.c b/lib/gl_avltree_omap.c index f205eb5..27702d1 100644 --- a/lib/gl_avltree_omap.c +++ b/lib/gl_avltree_omap.c @@ -39,8 +39,6 @@ node->key = key; \ node->value = value; #define NODE_PAYLOAD_DISPOSE \ - if (container->base.vdispose_fn != NULL) \ - container->base.vdispose_fn (node->value); \ if (container->base.kdispose_fn != NULL) \ container->base.kdispose_fn (node->key); diff --git a/lib/gl_omap.h b/lib/gl_omap.h index d00a699..aa82e33 100644 --- a/lib/gl_omap.h +++ b/lib/gl_omap.h @@ -360,14 +360,30 @@ GL_OMAP_INLINE int gl_omap_nx_put (gl_omap_t map, const void *key, const void *value) { const void *oldvalue; - return gl_omap_nx_getput (map, key, value, &oldvalue); + int result = gl_omap_nx_getput (map, key, value, &oldvalue); + if (result == 0) + { + gl_mapvalue_dispose_fn vdispose_fn = + ((const struct gl_omap_impl_base *) map)->vdispose_fn; + if (vdispose_fn != NULL) + vdispose_fn (oldvalue); + } + return result; } GL_OMAP_INLINE bool gl_omap_remove (gl_omap_t map, const void *key) { const void *oldvalue; - return gl_omap_getremove (map, key, &oldvalue); + bool result = gl_omap_getremove (map, key, &oldvalue); + if (result) + { + gl_mapvalue_dispose_fn vdispose_fn = + ((const struct gl_omap_impl_base *) map)->vdispose_fn; + if (vdispose_fn != NULL) + vdispose_fn (oldvalue); + } + return result; } #ifdef __cplusplus diff --git a/lib/gl_rbtree_omap.c b/lib/gl_rbtree_omap.c index 11540aa..cd3fb1c 100644 --- a/lib/gl_rbtree_omap.c +++ b/lib/gl_rbtree_omap.c @@ -39,8 +39,6 @@ node->key = key; \ node->value = value; #define NODE_PAYLOAD_DISPOSE \ - if (container->base.vdispose_fn != NULL) \ - container->base.vdispose_fn (node->value); \ if (container->base.kdispose_fn != NULL) \ container->base.kdispose_fn (node->key);