Coverage Report

Created: 2026-04-12 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/shash.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3
 *
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:
7
 *
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.
15
 */
16
17
#include <config.h>
18
#include "openvswitch/shash.h"
19
#include "hash.h"
20
#include "util.h"
21
22
static struct shash_node *shash_find__(const struct shash *,
23
                                       const char *name, size_t name_len,
24
                                       size_t hash);
25
26
static size_t
27
hash_name(const char *name)
28
52.3k
{
29
52.3k
    return hash_string(name, 0);
30
52.3k
}
31
32
void
33
shash_init(struct shash *sh)
34
1.30M
{
35
1.30M
    hmap_init(&sh->map);
36
1.30M
}
37
38
void
39
shash_destroy(struct shash *sh)
40
1.30M
{
41
1.30M
    if (sh) {
42
1.30M
        shash_clear(sh);
43
1.30M
        hmap_destroy(&sh->map);
44
1.30M
    }
45
1.30M
}
46
47
/* Like shash_destroy(), but also free() each node's 'data'. */
48
void
49
shash_destroy_free_data(struct shash *sh)
50
0
{
51
0
    if (sh) {
52
0
        shash_clear_free_data(sh);
53
0
        hmap_destroy(&sh->map);
54
0
    }
55
0
}
56
57
void
58
shash_swap(struct shash *a, struct shash *b)
59
0
{
60
0
    hmap_swap(&a->map, &b->map);
61
0
}
62
63
void
64
shash_moved(struct shash *sh)
65
0
{
66
0
    hmap_moved(&sh->map);
67
0
}
68
69
void
70
shash_clear(struct shash *sh)
71
1.30M
{
72
1.30M
    struct shash_node *node;
73
74
1.30M
    SHASH_FOR_EACH_SAFE (node, sh) {
75
0
        hmap_remove(&sh->map, &node->node);
76
0
        free(node->name);
77
0
        free(node);
78
0
    }
79
1.30M
}
80
81
/* Like shash_clear(), but also free() each node's 'data'. */
82
void
83
shash_clear_free_data(struct shash *sh)
84
0
{
85
0
    struct shash_node *node;
86
87
0
    SHASH_FOR_EACH_SAFE (node, sh) {
88
0
        hmap_remove(&sh->map, &node->node);
89
0
        free(node->data);
90
0
        free(node->name);
91
0
        free(node);
92
0
    }
93
0
}
94
95
bool
96
shash_is_empty(const struct shash *shash)
97
1.30M
{
98
1.30M
    return hmap_is_empty(&shash->map);
99
1.30M
}
100
101
size_t
102
shash_count(const struct shash *shash)
103
1.31M
{
104
1.31M
    ovs_assert(shash);
105
1.31M
    return hmap_count(&shash->map);
106
1.31M
}
107
108
static struct shash_node *
109
shash_add_nocopy__(struct shash *sh, char *name, const void *data, size_t hash)
110
46.1k
{
111
46.1k
    struct shash_node *node = xmalloc(sizeof *node);
112
46.1k
    node->name = name;
113
46.1k
    node->data = CONST_CAST(void *, data);
114
46.1k
    hmap_insert(&sh->map, &node->node, hash);
115
46.1k
    return node;
116
46.1k
}
117
118
/* It is the caller's responsibility to avoid duplicate names, if that is
119
 * desirable. */
120
struct shash_node *
121
shash_add_nocopy(struct shash *sh, char *name, const void *data)
122
0
{
123
0
    return shash_add_nocopy__(sh, name, data, hash_name(name));
124
0
}
125
126
/* It is the caller's responsibility to avoid duplicate names, if that is
127
 * desirable. */
128
struct shash_node *
129
shash_add(struct shash *sh, const char *name, const void *data)
130
0
{
131
0
    return shash_add_nocopy(sh, xstrdup(name), data);
132
0
}
133
134
bool
135
shash_add_once(struct shash *sh, const char *name, const void *data)
136
0
{
137
0
    if (!shash_find(sh, name)) {
138
0
        shash_add(sh, name, data);
139
0
        return true;
140
0
    } else {
141
0
        return false;
142
0
    }
143
0
}
144
145
void
146
shash_add_assert(struct shash *sh, const char *name, const void *data)
147
0
{
148
0
    ovs_assert(shash_add_once(sh, name, data));
149
0
}
150
151
/* Searches for 'name' in 'sh'.  If it does not already exist, adds it along
152
 * with 'data' and returns NULL.  If it does already exist, replaces its data
153
 * by 'data' and returns the data that it formerly contained. */
154
void *
155
shash_replace(struct shash *sh, const char *name, const void *data)
156
717
{
157
717
    size_t hash = hash_name(name);
158
717
    struct shash_node *node;
159
160
717
    node = shash_find__(sh, name, strlen(name), hash);
161
717
    if (!node) {
162
717
        shash_add_nocopy__(sh, xstrdup(name), data, hash);
163
717
        return NULL;
164
717
    } else {
165
0
        void *old_data = node->data;
166
0
        node->data = CONST_CAST(void *, data);
167
0
        return old_data;
168
0
    }
169
717
}
170
171
/* Searches for 'name' in 'sh'.  If it does not already exist, adds it along
172
 * with 'data' and returns NULL.  If it does already exist, replaces its data
173
 * by 'data' and returns the data that it formerly contained.
174
 *
175
 * Takes ownership of 'name'. */
176
void *
177
shash_replace_nocopy(struct shash *sh, char *name, const void *data)
178
48.0k
{
179
48.0k
    size_t hash = hash_name(name);
180
48.0k
    struct shash_node *node;
181
182
48.0k
    node = shash_find__(sh, name, strlen(name), hash);
183
48.0k
    if (!node) {
184
45.4k
        shash_add_nocopy__(sh, name, data, hash);
185
45.4k
        return NULL;
186
45.4k
    } else {
187
2.61k
        free(name);
188
2.61k
        void *old_data = node->data;
189
2.61k
        node->data = CONST_CAST(void *, data);
190
2.61k
        return old_data;
191
2.61k
    }
192
48.0k
}
193
194
/* Deletes 'node' from 'sh' and frees the node's name.  The caller is still
195
 * responsible for freeing the node's data, if necessary. */
196
void
197
shash_delete(struct shash *sh, struct shash_node *node)
198
46.1k
{
199
46.1k
    free(shash_steal(sh, node));
200
46.1k
}
201
202
/* Deletes 'node' from 'sh'.  Neither the node's name nor its data is freed;
203
 * instead, ownership is transferred to the caller.  Returns the node's
204
 * name. */
205
char *
206
shash_steal(struct shash *sh, struct shash_node *node)
207
46.1k
{
208
46.1k
    if (!node) {
209
0
        return NULL;
210
0
    }
211
212
46.1k
    char *name = node->name;
213
214
46.1k
    hmap_remove(&sh->map, &node->node);
215
46.1k
    free(node);
216
46.1k
    return name;
217
46.1k
}
218
219
static struct shash_node *
220
shash_find__(const struct shash *sh, const char *name, size_t name_len,
221
             size_t hash)
222
52.3k
{
223
52.3k
    struct shash_node *node;
224
225
52.3k
    HMAP_FOR_EACH_WITH_HASH (node, node, hash, &sh->map) {
226
4.18k
        if (!strncmp(node->name, name, name_len) && !node->name[name_len]) {
227
3.18k
            return node;
228
3.18k
        }
229
4.18k
    }
230
49.1k
    return NULL;
231
52.3k
}
232
233
/* If there are duplicates, returns a random element. */
234
struct shash_node *
235
shash_find(const struct shash *sh, const char *name)
236
3.60k
{
237
3.60k
    return shash_find__(sh, name, strlen(name), hash_name(name));
238
3.60k
}
239
240
/* Finds and returns a shash_node within 'sh' that has the given 'name' that is
241
 * exactly 'len' bytes long.  Returns NULL if no node in 'sh' has that name. */
242
struct shash_node *
243
shash_find_len(const struct shash *sh, const char *name, size_t len)
244
0
{
245
0
    return shash_find__(sh, name, len, hash_bytes(name, len, 0));
246
0
}
247
248
void *
249
shash_find_data(const struct shash *sh, const char *name)
250
0
{
251
0
    struct shash_node *node = shash_find(sh, name);
252
0
    return node ? node->data : NULL;
253
0
}
254
255
void *
256
shash_find_and_delete(struct shash *sh, const char *name)
257
3.60k
{
258
3.60k
    struct shash_node *node = shash_find(sh, name);
259
3.60k
    if (node) {
260
572
        void *data = node->data;
261
572
        shash_delete(sh, node);
262
572
        return data;
263
3.03k
    } else {
264
3.03k
        return NULL;
265
3.03k
    }
266
3.60k
}
267
268
void *
269
shash_find_and_delete_assert(struct shash *sh, const char *name)
270
0
{
271
0
    void *data = shash_find_and_delete(sh, name);
272
0
    ovs_assert(data);
273
0
    return data;
274
0
}
275
276
struct shash_node *
277
shash_first(const struct shash *shash)
278
453
{
279
453
    struct hmap_node *node = hmap_first(&shash->map);
280
453
    return node ? CONTAINER_OF(node, struct shash_node, node) : NULL;
281
453
}
282
283
static int
284
compare_nodes_by_name(const void *a_, const void *b_)
285
69.7k
{
286
69.7k
    const struct shash_node *const *a = a_;
287
69.7k
    const struct shash_node *const *b = b_;
288
69.7k
    return strcmp((*a)->name, (*b)->name);
289
69.7k
}
290
291
const struct shash_node **
292
shash_sort(const struct shash *sh)
293
1.30M
{
294
1.30M
    if (shash_is_empty(sh)) {
295
1.29M
        return NULL;
296
1.29M
    } else {
297
10.0k
        const struct shash_node **nodes;
298
10.0k
        struct shash_node *node;
299
10.0k
        size_t i, n;
300
301
10.0k
        n = shash_count(sh);
302
10.0k
        nodes = xmalloc(n * sizeof *nodes);
303
10.0k
        i = 0;
304
38.6k
        SHASH_FOR_EACH (node, sh) {
305
38.6k
            nodes[i++] = node;
306
38.6k
        }
307
10.0k
        ovs_assert(i == n);
308
309
10.0k
        qsort(nodes, n, sizeof *nodes, compare_nodes_by_name);
310
311
10.0k
        return nodes;
312
10.0k
    }
313
1.30M
}
314
315
/* Returns true if 'a' and 'b' contain the same keys (regardless of their
316
 * values), false otherwise. */
317
bool
318
shash_equal_keys(const struct shash *a, const struct shash *b)
319
0
{
320
0
    struct shash_node *node;
321
322
0
    if (hmap_count(&a->map) != hmap_count(&b->map)) {
323
0
        return false;
324
0
    }
325
0
    SHASH_FOR_EACH (node, a) {
326
0
        if (!shash_find(b, node->name)) {
327
0
            return false;
328
0
        }
329
0
    }
330
0
    return true;
331
0
}
332
333
/* Chooses and returns a randomly selected node from 'sh', which must not be
334
 * empty.
335
 *
336
 * I wouldn't depend on this algorithm to be fair, since I haven't analyzed it.
337
 * But it does at least ensure that any node in 'sh' can be chosen. */
338
struct shash_node *
339
shash_random_node(struct shash *sh)
340
0
{
341
    return CONTAINER_OF(hmap_random_node(&sh->map), struct shash_node, node);
342
0
}