From 4f2226487d3522654876885d769510b835c5f5ee Mon Sep 17 00:00:00 2001 From: Ben Pfaff 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 ec8a952b..635fe941 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 24c2a88f..c1eb5d04 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 8fd2eb18..cc45efb5 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 eab0af45..8a736e80 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 *); -- 2.30.2