shash: New function shash_steal().
authorBen Pfaff <blp@nicira.com>
Thu, 23 Sep 2010 16:42:30 +0000 (09:42 -0700)
committerBen Pfaff <blp@nicira.com>
Thu, 23 Sep 2010 18:45:34 +0000 (11:45 -0700)
lib/dpif-linux.c
lib/netdev.c
lib/shash.c
lib/shash.h

index ec8a952..635fe94 100644 (file)
@@ -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;
index 24c2a88..c1eb5d0 100644 (file)
@@ -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;
     }
 }
index 8fd2eb1..cc45efb 100644 (file)
@@ -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 *
index eab0af4..8a736e8 100644 (file)
@@ -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 *);