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

Ben Pfaff blp at nicira.com
Thu Sep 23 16:43:10 UTC 2010


On Wed, Sep 22, 2010 at 11:17:27PM -0700, Justin Pettit wrote:
> Interesting.  I'm tingly with anticipation with how you will use this...

Oh, I forgot to look around for existing potential users.  I found a
few.  Here's the updated commit.

--8<--------------------------cut here-------------------------->8--

From: Ben Pfaff <blp at nicira.com>
Date: Thu, 23 Sep 2010 09:42:30 -0700
Subject: [PATCH] shash: New function shash_steal().

---
 lib/dpif-linux.c |    3 +--
 lib/netdev.c     |    3 +--
 lib/shash.c      |   15 ++++++++++++++-
 lib/shash.h      |    1 +
 4 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/dpif-linux.c b/lib/dpif-linux.c
index ec8a952..635fe94 100644
--- a/lib/dpif-linux.c
+++ b/lib/dpif-linux.c
@@ -338,8 +338,7 @@ dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
         return ENOBUFS;
     } else if (!shash_is_empty(&dpif->changed_ports)) {
         struct shash_node *node = shash_first(&dpif->changed_ports);
-        *devnamep = xstrdup(node->name);
-        shash_delete(&dpif->changed_ports, node);
+        *devnamep = shash_steal(&dpif->changed_ports, node);
         return 0;
     } else {
         return EAGAIN;
diff --git a/lib/netdev.c b/lib/netdev.c
index 24c2a88..c1eb5d0 100644
--- a/lib/netdev.c
+++ b/lib/netdev.c
@@ -1553,8 +1553,7 @@ netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
         *devnamep = NULL;
         return EAGAIN;
     } else {
-        *devnamep = xstrdup(node->name);
-        shash_delete(&monitor->changed_netdevs, node);
+        *devnamep = shash_steal(&monitor->changed_netdevs, node);
         return 0;
     }
 }
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