netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / smap.h
1 /* Copyright (c) 2012, 2014, 2015, 2016 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.  */
14
15 #ifndef SMAP_H
16 #define SMAP_H 1
17
18 #include "hmap.h"
19
20 struct json;
21 struct uuid;
22
23 /* A map from string to string. */
24 struct smap {
25     struct hmap map;           /* Contains "struct smap_node"s. */
26 };
27
28 struct smap_node {
29     struct hmap_node node;     /* In struct smap's 'map' hmap. */
30     char *key;
31     char *value;
32 };
33
34 #define SMAP_INITIALIZER(SMAP) { HMAP_INITIALIZER(&(SMAP)->map) }
35
36 #define SMAP_FOR_EACH(SMAP_NODE, SMAP)                                  \
37     HMAP_FOR_EACH_INIT (SMAP_NODE, node, &(SMAP)->map,                  \
38                         BUILD_ASSERT_TYPE(SMAP_NODE, struct smap_node *), \
39                         BUILD_ASSERT_TYPE(SMAP, struct smap *))
40
41 #define SMAP_FOR_EACH_SAFE(SMAP_NODE, NEXT, SMAP)           \
42     HMAP_FOR_EACH_SAFE_INIT (                               \
43         SMAP_NODE, NEXT, node, &(SMAP)->map,                \
44         BUILD_ASSERT_TYPE(SMAP_NODE, struct smap_node *),   \
45         BUILD_ASSERT_TYPE(NEXT, struct smap_node *),        \
46         BUILD_ASSERT_TYPE(SMAP, struct smap *))
47
48 /* Initializer for an immutable struct smap 'SMAP' that contains a single
49  * 'KEY'-'VALUE' pair, e.g.
50  *
51  *     const struct smap smap = SMAP1_CONST1(&smap, "key", "value");
52  *
53  * An smap initialized this way must not be modified or destroyed.
54  *
55  * The 'KEY' argument is evaluated multiple times.
56  */
57 #define SMAP_CONST1(SMAP, KEY, VALUE) {                                 \
58         HMAP_CONST1(&(SMAP)->map,                                       \
59                    (&(struct smap_node) SMAP_NODE_INIT(KEY, VALUE).node)) \
60             }
61 #define SMAP_NODE_INIT(KEY, VALUE) {                \
62         HMAP_NODE_INIT(hash_string(KEY, 0)),        \
63                        CONST_CAST(char *, KEY),     \
64                        CONST_CAST(char *, VALUE) }
65
66
67 void smap_init(struct smap *);
68 void smap_destroy(struct smap *);
69
70 struct smap_node *smap_add(struct smap *, const char *, const char *);
71 struct smap_node *smap_add_nocopy(struct smap *, char *, char *);
72 bool smap_add_once(struct smap *, const char *, const char *);
73 void smap_add_format(struct smap *, const char *key, const char *, ...)
74     OVS_PRINTF_FORMAT(3, 4);
75 void smap_add_ipv6(struct smap *, const char *, struct in6_addr *);
76 void smap_replace(struct smap *, const char *, const char *);
77
78 void smap_remove(struct smap *, const char *);
79 void smap_remove_node(struct smap *, struct smap_node *);
80 void smap_steal(struct smap *, struct smap_node *, char **keyp, char **valuep);
81 void smap_clear(struct smap *);
82
83 const char *smap_get(const struct smap *, const char *);
84 struct smap_node *smap_get_node(const struct smap *, const char *);
85 bool smap_get_bool(const struct smap *smap, const char *key, bool def);
86 int smap_get_int(const struct smap *smap, const char *key, int def);
87 bool smap_get_uuid(const struct smap *, const char *key, struct uuid *);
88
89 bool smap_is_empty(const struct smap *);
90 size_t smap_count(const struct smap *);
91
92 void smap_clone(struct smap *dst, const struct smap *src);
93 const struct smap_node **smap_sort(const struct smap *);
94
95 void smap_from_json(struct smap *, const struct json *);
96 struct json *smap_to_json(const struct smap *);
97
98 bool smap_equal(const struct smap *, const struct smap *);
99
100 #endif /* smap.h */