netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / hmapx.h
1 /*
2  * Copyright (c) 2011, 2016 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 #ifndef HMAPX_H
18 #define HMAPX_H
19
20 #include "hmap.h"
21
22 struct hmapx_node {
23     struct hmap_node hmap_node;
24     void *data;
25 };
26
27 /* A set of "void *" pointers. */
28 struct hmapx {
29     struct hmap map;
30 };
31
32 #define HMAPX_INITIALIZER(HMAPX) { HMAP_INITIALIZER(&(HMAPX)->map) }
33
34 /* Basics. */
35 void hmapx_init(struct hmapx *);
36 void hmapx_destroy(struct hmapx *);
37 void hmapx_clone(struct hmapx *, const struct hmapx *);
38 void hmapx_swap(struct hmapx *, struct hmapx *);
39 void hmapx_moved(struct hmapx *);
40
41 /* Count. */
42 bool hmapx_is_empty(const struct hmapx *);
43 size_t hmapx_count(const struct hmapx *);
44
45 /* Insertion. */
46 struct hmapx_node *hmapx_add(struct hmapx *, void *);
47 void hmapx_add_assert(struct hmapx *, void *);
48
49 /* Deletion. */
50 void hmapx_clear(struct hmapx *);
51 void hmapx_delete(struct hmapx *, struct hmapx_node *);
52 bool hmapx_find_and_delete(struct hmapx *, const void *);
53 void hmapx_find_and_delete_assert(struct hmapx *, const void *);
54
55 /* Search. */
56 struct hmapx_node *hmapx_find(const struct hmapx *, const void *);
57 bool hmapx_contains(const struct hmapx *, const void *);
58 bool hmapx_equals(const struct hmapx *, const struct hmapx *);
59
60 /* Iteration. */
61
62 /* Iterates through every hmapx_node in HMAPX. */
63 #define HMAPX_FOR_EACH(NODE, HMAPX)                                     \
64     HMAP_FOR_EACH_INIT(NODE, hmap_node, &(HMAPX)->map,                  \
65                        BUILD_ASSERT_TYPE(NODE, struct hmapx_node *),    \
66                        BUILD_ASSERT_TYPE(HMAPX, struct hmapx *))
67
68 /* Safe when NODE may be freed (not needed when NODE may be removed from the
69  * hash map but its members remain accessible and intact). */
70 #define HMAPX_FOR_EACH_SAFE(NODE, NEXT, HMAPX)                          \
71     HMAP_FOR_EACH_SAFE_INIT(NODE, NEXT, hmap_node, &(HMAPX)->map,       \
72                             BUILD_ASSERT_TYPE(NODE, struct hmapx_node *), \
73                             BUILD_ASSERT_TYPE(NEXT, struct hmapx_node *), \
74                             BUILD_ASSERT_TYPE(HMAPX, struct hmapx *))
75
76 #endif /* hmapx.h */