[ovs-dev] [PATCH 4/7] shash: New function shash_steal().

Ben Pfaff blp at nicira.com
Wed Sep 22 23:45:39 UTC 2010


---
 lib/shash.c |   15 ++++++++++++++-
 lib/shash.h |    1 +
 2 files changed, 15 insertions(+), 1 deletions(-)

diff --git a/lib/shash.c b/lib/shash.c
index 8fd2eb1..cc45efb 100644
--- a/lib/shash.c
+++ b/lib/shash.c
@@ -167,12 +167,25 @@ shash_replace(struct shash *sh, const char *name, const void *data)
     }
 }
 
+/* Deletes 'node' from 'sh' and frees the node's name.  The caller is still
+ * responsible for freeing the node's data, if necessary. */
 void
 shash_delete(struct shash *sh, struct shash_node *node)
 {
+    free(shash_steal(sh, node));
+}
+
+/* Deletes 'node' from 'sh'.  Neither the node's name nor its data is freed;
+ * instead, ownership is transferred to the caller.  Returns the node's
+ * name. */
+char *
+shash_steal(struct shash *sh, struct shash_node *node)
+{
+    char *name = node->name;
+
     hmap_remove(&sh->map, &node->node);
-    free(node->name);
     free(node);
+    return name;
 }
 
 static struct shash_node *
diff --git a/lib/shash.h b/lib/shash.h
index eab0af4..8a736e8 100644
--- a/lib/shash.h
+++ b/lib/shash.h
@@ -57,6 +57,7 @@ bool shash_add_once(struct shash *, const char *, const void *);
 void shash_add_assert(struct shash *, const char *, const void *);
 void *shash_replace(struct shash *, const char *, const void *data);
 void shash_delete(struct shash *, struct shash_node *);
+char *shash_steal(struct shash *, struct shash_node *);
 struct shash_node *shash_find(const struct shash *, const char *);
 void *shash_find_data(const struct shash *, const char *);
 void *shash_find_and_delete(struct shash *, const char *);
-- 
1.7.1





More information about the dev mailing list