2 * Copyright (c) 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 hash_name(const char *name)
25 return hash_string(name, 0);
29 shash_init(struct shash *sh)
35 shash_destroy(struct shash *sh)
43 shash_clear(struct shash *sh)
45 struct shash_node *node, *next;
47 HMAP_FOR_EACH_SAFE (node, next, struct shash_node, node, &sh->map) {
48 hmap_remove(&sh->map, &node->node);
54 /* It is the caller's responsible to avoid duplicate names, if that is
57 shash_add(struct shash *sh, const char *name, void *data)
59 struct shash_node *node = xmalloc(sizeof *node);
60 node->name = xstrdup(name);
62 hmap_insert(&sh->map, &node->node, hash_name(name));
66 shash_delete(struct shash *sh, struct shash_node *node)
68 hmap_remove(&sh->map, &node->node);
73 /* If there are duplicates, returns a random element. */
75 shash_find(const struct shash *sh, const char *name)
77 struct shash_node *node;
79 HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node,
80 hash_name(name), &sh->map) {
81 if (!strcmp(node->name, name)) {
89 shash_find_data(const struct shash *sh, const char *name)
91 struct shash_node *node = shash_find(sh, name);
92 return node ? node->data : NULL;