From 1a833ed0ddb6e482b121f813e00e5e2b5e79a8d2 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 12 Dec 2018 01:14:34 +0100 Subject: [PATCH] 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. --- ChangeLog | 8 ++++++++ lib/gl_array_omap.c | 36 +++++++++++++++++------------------- lib/gl_avltree_omap.c | 2 -- lib/gl_omap.h | 20 ++++++++++++++++++-- lib/gl_rbtree_omap.c | 2 -- 5 files changed, 43 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2b95de209d..69a62b5d69 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2018-12-11 Bruno Haible + 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. + array-omap, avltree-omap, rbtree-omap: Tweak style. * lib/gl_anytree_omap.h (gl_tree_nx_getput): Return 1 or 0, not true or false. diff --git a/lib/gl_array_omap.c b/lib/gl_array_omap.c index a2b21bc3ad..3b65269c0e 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 f205eb5e7c..27702d1000 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 d00a699b4f..aa82e332b6 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 11540aa6d1..cd3fb1ce7b 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); -- 2.39.5