2018-12-11 Bruno Haible <bruno@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.
+
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.
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)
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)
{
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);
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
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);