]> Savannah Git Hosting - gnulib.git/commitdiff
omap: Don't dispose the old value when the function returns it.
authorBruno Haible <bruno@clisp.org>
Wed, 12 Dec 2018 00:14:34 +0000 (01:14 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 12 Dec 2018 00:14:34 +0000 (01:14 +0100)
* 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
lib/gl_array_omap.c
lib/gl_avltree_omap.c
lib/gl_omap.h
lib/gl_rbtree_omap.c

index 2b95de209dd782ba060947aa9805a0af6d17f768..69a62b5d6960f77555d429b9d0674721b00b48f2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 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.
index a2b21bc3ad86c768f60c7bc045173f9bb8a494f3..3b65269c0ebf0e18eb72901082393af2e21493d2 100644 (file)
@@ -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)
 {
index f205eb5e7c29923fee4fa20dceb1b8839460d4cf..27702d1000575e8fbefa0b225c4d2429c7476bc1 100644 (file)
@@ -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);
 
index d00a699b4f6c241b7a8a7a30b0021412fc7beda2..aa82e332b69aae60bf3de35de8d46502d802715f 100644 (file)
@@ -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
index 11540aa6d1462a733b863968befa1bc92b700100..cd3fb1ce7bfbfbc2ec2d91b397f2788e0b60d342 100644 (file)
@@ -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);