6ac4b9b6a3b171e5baae411cdfbebdfb0202e0fb
[dpdk.git] / lib / librte_eal / common / include / rte_tailq.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_TAILQ_H_
35 #define _RTE_TAILQ_H_
36
37 /**
38  * @file
39  *  Here defines rte_tailq APIs for only internal use
40  *
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include <sys/queue.h>
48 #include <stdio.h>
49
50 /** dummy structure type used by the rte_tailq APIs */
51 struct rte_tailq_entry {
52         TAILQ_ENTRY(rte_tailq_entry) next; /**< Pointer entries for a tailq list */
53         void *data; /**< Pointer to the data referenced by this tailq entry */
54 };
55 /** dummy */
56 TAILQ_HEAD(rte_tailq_entry_head, rte_tailq_entry);
57
58 #define RTE_TAILQ_NAMESIZE 32
59
60 /**
61  * The structure defining a tailq header entry for storing
62  * in the rte_config structure in shared memory. Each tailq
63  * is identified by name.
64  * Any library storing a set of objects e.g. rings, mempools, hash-tables,
65  * is recommended to use an entry here, so as to make it easy for
66  * a multi-process app to find already-created elements in shared memory.
67  */
68 struct rte_tailq_head {
69         struct rte_tailq_entry_head tailq_head; /**< NOTE: must be first element */
70 };
71
72 /**
73  * Return the first tailq entry casted to the right struct.
74  */
75 #define RTE_TAILQ_CAST(tailq_entry, struct_name) \
76         (struct struct_name *)&(tailq_entry)->tailq_head
77
78 /**
79  * Utility macro to make reserving a tailqueue for a particular struct easier.
80  *
81  * @param name
82  *   The name to be given to the tailq - used by lookup to find it later
83  *
84  * @param struct_name
85  *   The name of the list type we are using. (Generally this is the same as the
86  *   first parameter passed to TAILQ_HEAD macro)
87  *
88  * @return
89  *   The return value from rte_eal_tailq_reserve, typecast to the appropriate
90  *   structure pointer type.
91  *   NULL on error, since the tailq_head is the first
92  *   element in the rte_tailq_head structure.
93  */
94 #define RTE_TAILQ_RESERVE(name, struct_name) \
95         RTE_TAILQ_CAST(rte_eal_tailq_reserve(name), struct_name)
96
97 /**
98  * Utility macro to make reserving a tailqueue for a particular struct easier.
99  *
100  * @param idx
101  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
102  *       - used by lookup to find it later
103  *
104  * @param struct_name
105  *   The name of the list type we are using. (Generally this is the same as the
106  *   first parameter passed to TAILQ_HEAD macro)
107  *
108  * @return
109  *   The return value from rte_eal_tailq_reserve, typecast to the appropriate
110  *   structure pointer type.
111  *   NULL on error, since the tailq_head is the first
112  *   element in the rte_tailq_head structure.
113  */
114 #define RTE_TAILQ_RESERVE_BY_IDX(idx, struct_name) \
115         RTE_TAILQ_CAST(rte_eal_tailq_reserve_by_idx(idx), struct_name)
116
117 /**
118  * Utility macro to make looking up a tailqueue for a particular struct easier.
119  *
120  * @param name
121  *   The name of tailq
122  *
123  * @param struct_name
124  *   The name of the list type we are using. (Generally this is the same as the
125  *   first parameter passed to TAILQ_HEAD macro)
126  *
127  * @return
128  *   The return value from rte_eal_tailq_lookup, typecast to the appropriate
129  *   structure pointer type.
130  *   NULL on error, since the tailq_head is the first
131  *   element in the rte_tailq_head structure.
132  */
133 #define RTE_TAILQ_LOOKUP(name, struct_name) \
134         RTE_TAILQ_CAST(rte_eal_tailq_lookup(name), struct_name)
135
136 /**
137  * Utility macro to make looking up a tailqueue for a particular struct easier.
138  *
139  * @param idx
140  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
141  *
142  * @param struct_name
143  *   The name of the list type we are using. (Generally this is the same as the
144  *   first parameter passed to TAILQ_HEAD macro)
145  *
146  * @return
147  *   The return value from rte_eal_tailq_lookup, typecast to the appropriate
148  *   structure pointer type.
149  *   NULL on error, since the tailq_head is the first
150  *   element in the rte_tailq_head structure.
151  */
152 #define RTE_TAILQ_LOOKUP_BY_IDX(idx, struct_name) \
153         RTE_TAILQ_CAST(rte_eal_tailq_lookup_by_idx(idx), struct_name)
154
155 /**
156  * Reserve a slot in the tailq list for a particular tailq header
157  * Note: this function, along with rte_tailq_lookup, is not multi-thread safe,
158  * and both these functions should only be called from a single thread at a time
159  *
160  * @param name
161  *   The name to be given to the tail queue.
162  * @return
163  *   A pointer to the newly reserved tailq entry
164  */
165 struct rte_tailq_head *rte_eal_tailq_reserve(const char *name);
166
167 /**
168  * Reserve a slot in the tailq list for a particular tailq header
169  * Note: this function, along with rte_tailq_lookup, is not multi-thread safe,
170  * and both these functions should only be called from a single thread at a time
171  *
172  * @param idx
173  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
174  * @return
175  *   A pointer to the newly reserved tailq entry
176  */
177 struct rte_tailq_head *rte_eal_tailq_reserve_by_idx(const unsigned idx);
178
179 /**
180  * Dump tail queues to the console.
181  *
182  * @param f
183  *   A pointer to a file for output
184  */
185 void rte_dump_tailq(FILE *f);
186
187 /**
188  * Lookup for a tail queue.
189  *
190  * Get a pointer to a tail queue header of an already reserved tail
191  * queue identified by the name given as an argument.
192  * Note: this function, along with rte_tailq_reserve, is not multi-thread safe,
193  * and both these functions should only be called from a single thread at a time
194  *
195  * @param name
196  *   The name of the queue.
197  * @return
198  *   A pointer to the tail queue head structure.
199  */
200 struct rte_tailq_head *rte_eal_tailq_lookup(const char *name);
201
202 /**
203  * Lookup for a tail queue.
204  *
205  * Get a pointer to a tail queue header of an already reserved tail
206  * queue identified by the name given as an argument.
207  * Note: this function, along with rte_tailq_reserve, is not multi-thread safe,
208  * and both these functions should only be called from a single thread at a time
209  *
210  * @param idx
211  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
212  * @return
213  *   A pointer to the tail queue head structure.
214  */
215 struct rte_tailq_head *rte_eal_tailq_lookup_by_idx(const unsigned idx);
216
217 #ifdef __cplusplus
218 }
219 #endif
220
221 #endif /* _RTE_TAILQ_H_ */