summaryrefslogtreecommitdiff
path: root/lib/shash.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2010-06-30 13:26:42 -0700
committerBen Pfaff <blp@nicira.com>2010-06-30 16:49:01 -0700
commit79903dd171cd7bdbb52710b98dbaa5de1537de87 (patch)
tree2dbdee72f39de7eaace8fb01e54cd6f92bfc42a4 /lib/shash.c
parent1c6d8802c23c64a9439be43fa2064aca8083bfb2 (diff)
downloadopenvswitch-79903dd171cd7bdbb52710b98dbaa5de1537de87.tar.gz
shash: New function shash_replace().
To be used in an upcoming commit.
Diffstat (limited to 'lib/shash.c')
-rw-r--r--lib/shash.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/shash.c b/lib/shash.c
index e2b1fe2c7..1664baf62 100644
--- a/lib/shash.c
+++ b/lib/shash.c
@@ -147,6 +147,26 @@ shash_add_assert(struct shash *sh, const char *name, const void *data)
assert(added);
}
+/* Searches for 'name' in 'sh'. If it does not already exist, adds it along
+ * with 'data' and returns NULL. If it does already exist, replaces its data
+ * by 'data' and returns the data that it formerly contained. */
+void *
+shash_replace(struct shash *sh, const char *name, const void *data)
+{
+ size_t hash = hash_name(name);
+ struct shash_node *node;
+
+ node = shash_find__(sh, name, hash);
+ if (!node) {
+ shash_add_nocopy__(sh, xstrdup(name), data, hash);
+ return NULL;
+ } else {
+ void *old_data = node->data;
+ node->data = (void *) data;
+ return old_data;
+ }
+}
+
void
shash_delete(struct shash *sh, struct shash_node *node)
{