]> git.proxmox.com Git - mirror_ovs.git/blame - lib/shash.c
dpctl: Fix dpctl process command parameter error.
[mirror_ovs.git] / lib / shash.c
CommitLineData
064af421 1/*
ebc56baa 2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16
17#include <config.h>
ee89ea7b 18#include "openvswitch/shash.h"
064af421
BP
19#include "hash.h"
20
1c6d8802 21static struct shash_node *shash_find__(const struct shash *,
e74a239d
BP
22 const char *name, size_t name_len,
23 size_t hash);
1c6d8802 24
064af421
BP
25static size_t
26hash_name(const char *name)
27{
28 return hash_string(name, 0);
29}
30
31void
32shash_init(struct shash *sh)
33{
34 hmap_init(&sh->map);
35}
36
37void
38shash_destroy(struct shash *sh)
39{
40 if (sh) {
41 shash_clear(sh);
d855aead 42 hmap_destroy(&sh->map);
064af421
BP
43 }
44}
45
a90b56b7
BP
46/* Like shash_destroy(), but also free() each node's 'data'. */
47void
48shash_destroy_free_data(struct shash *sh)
49{
50 if (sh) {
51 shash_clear_free_data(sh);
52 hmap_destroy(&sh->map);
53 }
54}
55
37e7f427
BP
56void
57shash_swap(struct shash *a, struct shash *b)
58{
59 hmap_swap(&a->map, &b->map);
60}
61
baa8f41b
BP
62void
63shash_moved(struct shash *sh)
64{
65 hmap_moved(&sh->map);
66}
67
064af421
BP
68void
69shash_clear(struct shash *sh)
70{
71 struct shash_node *node, *next;
72
fd30311c 73 SHASH_FOR_EACH_SAFE (node, next, sh) {
064af421
BP
74 hmap_remove(&sh->map, &node->node);
75 free(node->name);
76 free(node);
77 }
78}
79
a90b56b7
BP
80/* Like shash_clear(), but also free() each node's 'data'. */
81void
82shash_clear_free_data(struct shash *sh)
83{
84 struct shash_node *node, *next;
85
86 SHASH_FOR_EACH_SAFE (node, next, sh) {
87 hmap_remove(&sh->map, &node->node);
88 free(node->data);
89 free(node->name);
90 free(node);
91 }
92}
93
732dcb37
BP
94bool
95shash_is_empty(const struct shash *shash)
96{
97 return hmap_is_empty(&shash->map);
98}
99
c01da229
BP
100size_t
101shash_count(const struct shash *shash)
102{
103 return hmap_count(&shash->map);
104}
105
1c6d8802
BP
106static struct shash_node *
107shash_add_nocopy__(struct shash *sh, char *name, const void *data, size_t hash)
064af421
BP
108{
109 struct shash_node *node = xmalloc(sizeof *node);
ce5a3e38 110 node->name = name;
ebc56baa 111 node->data = CONST_CAST(void *, data);
1c6d8802 112 hmap_insert(&sh->map, &node->node, hash);
78299b0a 113 return node;
064af421
BP
114}
115
1c6d8802
BP
116/* It is the caller's responsibility to avoid duplicate names, if that is
117 * desirable. */
118struct shash_node *
119shash_add_nocopy(struct shash *sh, char *name, const void *data)
120{
121 return shash_add_nocopy__(sh, name, data, hash_name(name));
122}
123
ce5a3e38
BP
124/* It is the caller's responsibility to avoid duplicate names, if that is
125 * desirable. */
126struct shash_node *
127shash_add(struct shash *sh, const char *name, const void *data)
128{
129 return shash_add_nocopy(sh, xstrdup(name), data);
130}
131
76343538
BP
132bool
133shash_add_once(struct shash *sh, const char *name, const void *data)
134{
135 if (!shash_find(sh, name)) {
136 shash_add(sh, name, data);
137 return true;
138 } else {
139 return false;
140 }
141}
142
4a1ee6ae
BP
143void
144shash_add_assert(struct shash *sh, const char *name, const void *data)
145{
500db308 146 ovs_assert(shash_add_once(sh, name, data));
4a1ee6ae
BP
147}
148
79903dd1
BP
149/* Searches for 'name' in 'sh'. If it does not already exist, adds it along
150 * with 'data' and returns NULL. If it does already exist, replaces its data
151 * by 'data' and returns the data that it formerly contained. */
152void *
153shash_replace(struct shash *sh, const char *name, const void *data)
154{
155 size_t hash = hash_name(name);
156 struct shash_node *node;
157
e74a239d 158 node = shash_find__(sh, name, strlen(name), hash);
79903dd1
BP
159 if (!node) {
160 shash_add_nocopy__(sh, xstrdup(name), data, hash);
161 return NULL;
162 } else {
163 void *old_data = node->data;
ebc56baa 164 node->data = CONST_CAST(void *, data);
79903dd1
BP
165 return old_data;
166 }
167}
168
828129d9
BP
169/* Searches for 'name' in 'sh'. If it does not already exist, adds it along
170 * with 'data' and returns NULL. If it does already exist, replaces its data
171 * by 'data' and returns the data that it formerly contained.
172 *
173 * Takes ownership of 'name'. */
174void *
175shash_replace_nocopy(struct shash *sh, char *name, const void *data)
176{
177 size_t hash = hash_name(name);
178 struct shash_node *node;
179
180 node = shash_find__(sh, name, strlen(name), hash);
181 if (!node) {
182 shash_add_nocopy__(sh, name, data, hash);
183 return NULL;
184 } else {
185 free(name);
186 void *old_data = node->data;
187 node->data = CONST_CAST(void *, data);
188 return old_data;
189 }
190}
191
4f222648
BP
192/* Deletes 'node' from 'sh' and frees the node's name. The caller is still
193 * responsible for freeing the node's data, if necessary. */
064af421
BP
194void
195shash_delete(struct shash *sh, struct shash_node *node)
196{
4f222648
BP
197 free(shash_steal(sh, node));
198}
199
200/* Deletes 'node' from 'sh'. Neither the node's name nor its data is freed;
201 * instead, ownership is transferred to the caller. Returns the node's
202 * name. */
203char *
204shash_steal(struct shash *sh, struct shash_node *node)
205{
206 char *name = node->name;
207
064af421 208 hmap_remove(&sh->map, &node->node);
064af421 209 free(node);
4f222648 210 return name;
064af421
BP
211}
212
1c6d8802 213static struct shash_node *
e74a239d
BP
214shash_find__(const struct shash *sh, const char *name, size_t name_len,
215 size_t hash)
064af421
BP
216{
217 struct shash_node *node;
218
4e8e4213 219 HMAP_FOR_EACH_WITH_HASH (node, node, hash, &sh->map) {
e74a239d 220 if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
064af421
BP
221 return node;
222 }
223 }
224 return NULL;
225}
226
1c6d8802
BP
227/* If there are duplicates, returns a random element. */
228struct shash_node *
229shash_find(const struct shash *sh, const char *name)
230{
e74a239d
BP
231 return shash_find__(sh, name, strlen(name), hash_name(name));
232}
233
234/* Finds and returns a shash_node within 'sh' that has the given 'name' that is
235 * exactly 'len' bytes long. Returns NULL if no node in 'sh' has that name. */
236struct shash_node *
237shash_find_len(const struct shash *sh, const char *name, size_t len)
238{
239 return shash_find__(sh, name, len, hash_bytes(name, len, 0));
1c6d8802
BP
240}
241
064af421
BP
242void *
243shash_find_data(const struct shash *sh, const char *name)
244{
245 struct shash_node *node = shash_find(sh, name);
246 return node ? node->data : NULL;
247}
7efc68eb 248
837e8097
BP
249void *
250shash_find_and_delete(struct shash *sh, const char *name)
251{
252 struct shash_node *node = shash_find(sh, name);
253 if (node) {
254 void *data = node->data;
255 shash_delete(sh, node);
256 return data;
257 } else {
258 return NULL;
259 }
260}
261
4a1ee6ae
BP
262void *
263shash_find_and_delete_assert(struct shash *sh, const char *name)
264{
265 void *data = shash_find_and_delete(sh, name);
cb22974d 266 ovs_assert(data != NULL);
4a1ee6ae
BP
267 return data;
268}
269
7efc68eb
BP
270struct shash_node *
271shash_first(const struct shash *shash)
272{
273 struct hmap_node *node = hmap_first(&shash->map);
274 return node ? CONTAINER_OF(node, struct shash_node, node) : NULL;
275}
276
07423999
BP
277static int
278compare_nodes_by_name(const void *a_, const void *b_)
279{
280 const struct shash_node *const *a = a_;
281 const struct shash_node *const *b = b_;
282 return strcmp((*a)->name, (*b)->name);
283}
284
285const struct shash_node **
286shash_sort(const struct shash *sh)
287{
288 if (shash_is_empty(sh)) {
289 return NULL;
290 } else {
291 const struct shash_node **nodes;
292 struct shash_node *node;
293 size_t i, n;
294
295 n = shash_count(sh);
296 nodes = xmalloc(n * sizeof *nodes);
297 i = 0;
298 SHASH_FOR_EACH (node, sh) {
299 nodes[i++] = node;
300 }
cb22974d 301 ovs_assert(i == n);
07423999
BP
302
303 qsort(nodes, n, sizeof *nodes, compare_nodes_by_name);
304
305 return nodes;
306 }
307}
37e7f427
BP
308
309/* Returns true if 'a' and 'b' contain the same keys (regardless of their
310 * values), false otherwise. */
311bool
312shash_equal_keys(const struct shash *a, const struct shash *b)
313{
314 struct shash_node *node;
315
316 if (hmap_count(&a->map) != hmap_count(&b->map)) {
317 return false;
318 }
319 SHASH_FOR_EACH (node, a) {
320 if (!shash_find(b, node->name)) {
321 return false;
322 }
323 }
324 return true;
325}
f2f7be86
BP
326
327/* Chooses and returns a randomly selected node from 'sh', which must not be
328 * empty.
329 *
330 * I wouldn't depend on this algorithm to be fair, since I haven't analyzed it.
331 * But it does at least ensure that any node in 'sh' can be chosen. */
332struct shash_node *
333shash_random_node(struct shash *sh)
334{
335 return CONTAINER_OF(hmap_random_node(&sh->map), struct shash_node, node);
336}