DPDK  19.11.14
rte_stack.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4 
19 #ifndef _RTE_STACK_H_
20 #define _RTE_STACK_H_
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #include <rte_atomic.h>
27 #include <rte_compat.h>
28 #include <rte_debug.h>
29 #include <rte_errno.h>
30 #include <rte_memzone.h>
31 #include <rte_spinlock.h>
32 
33 #define RTE_TAILQ_STACK_NAME "RTE_STACK"
34 #define RTE_STACK_MZ_PREFIX "STK_"
35 
36 #define RTE_STACK_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
37  sizeof(RTE_STACK_MZ_PREFIX) + 1)
38 
39 struct rte_stack_lf_elem {
40  void *data;
41  struct rte_stack_lf_elem *next;
42 };
43 
44 struct rte_stack_lf_head {
45  struct rte_stack_lf_elem *top;
46  uint64_t cnt;
47 };
48 
49 struct rte_stack_lf_list {
51  struct rte_stack_lf_head head __rte_aligned(16);
53  uint64_t len;
54 };
55 
56 /* Structure containing two lock-free LIFO lists: the stack itself and a list
57  * of free linked-list elements.
58  */
59 struct rte_stack_lf {
61  struct rte_stack_lf_list used __rte_cache_aligned;
63  struct rte_stack_lf_list free __rte_cache_aligned;
65  struct rte_stack_lf_elem elems[] __rte_cache_aligned;
66 };
67 
68 /* Structure containing the LIFO, its current length, and a lock for mutual
69  * exclusion.
70  */
71 struct rte_stack_std {
72  rte_spinlock_t lock;
73  uint32_t len;
74  void *objs[];
75 };
76 
77 /* The RTE stack structure contains the LIFO structure itself, plus metadata
78  * such as its name and memzone pointer.
79  */
80 struct rte_stack {
84  const struct rte_memzone *memzone;
85  uint32_t capacity;
86  uint32_t flags;
88  union {
89  struct rte_stack_lf stack_lf;
90  struct rte_stack_std stack_std;
91  };
93 
98 #define RTE_STACK_F_LF 0x0001
99 
100 #include "rte_stack_std.h"
101 #include "rte_stack_lf.h"
102 
118 __rte_experimental
119 static __rte_always_inline unsigned int
120 rte_stack_push(struct rte_stack *s, void * const *obj_table, unsigned int n)
121 {
122  RTE_ASSERT(s != NULL);
123  RTE_ASSERT(obj_table != NULL);
124 
125  if (s->flags & RTE_STACK_F_LF)
126  return __rte_stack_lf_push(s, obj_table, n);
127  else
128  return __rte_stack_std_push(s, obj_table, n);
129 }
130 
146 __rte_experimental
147 static __rte_always_inline unsigned int
148 rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
149 {
150  RTE_ASSERT(s != NULL);
151  RTE_ASSERT(obj_table != NULL);
152 
153  if (s->flags & RTE_STACK_F_LF)
154  return __rte_stack_lf_pop(s, obj_table, n);
155  else
156  return __rte_stack_std_pop(s, obj_table, n);
157 }
158 
170 __rte_experimental
171 static __rte_always_inline unsigned int
172 rte_stack_count(struct rte_stack *s)
173 {
174  RTE_ASSERT(s != NULL);
175 
176  if (s->flags & RTE_STACK_F_LF)
177  return __rte_stack_lf_count(s);
178  else
179  return __rte_stack_std_count(s);
180 }
181 
193 __rte_experimental
194 static __rte_always_inline unsigned int
195 rte_stack_free_count(struct rte_stack *s)
196 {
197  RTE_ASSERT(s != NULL);
198 
199  return s->capacity - rte_stack_count(s);
200 }
201 
233 __rte_experimental
234 struct rte_stack *
235 rte_stack_create(const char *name, unsigned int count, int socket_id,
236  uint32_t flags);
237 
247 __rte_experimental
248 void
249 rte_stack_free(struct rte_stack *s);
250 
265 __rte_experimental
266 struct rte_stack *
267 rte_stack_lookup(const char *name);
268 
269 #ifdef __cplusplus
270 }
271 #endif
272 
273 #endif /* _RTE_STACK_H_ */
#define __rte_always_inline
Definition: rte_common.h:158
__rte_experimental struct rte_stack * rte_stack_lookup(const char *name)
uint32_t flags
Definition: rte_memzone.h:70
__rte_experimental void rte_stack_free(struct rte_stack *s)
#define RTE_STACK_F_LF
Definition: rte_stack.h:98
static __rte_experimental __rte_always_inline unsigned int rte_stack_free_count(struct rte_stack *s)
Definition: rte_stack.h:195
static __rte_experimental __rte_always_inline unsigned int rte_stack_count(struct rte_stack *s)
Definition: rte_stack.h:172
static __rte_experimental __rte_always_inline unsigned int rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
Definition: rte_stack.h:148
__rte_experimental struct rte_stack * rte_stack_create(const char *name, unsigned int count, int socket_id, uint32_t flags)
#define RTE_STD_C11
Definition: rte_common.h:40
#define __rte_cache_aligned
Definition: rte_common.h:322
#define RTE_STACK_NAMESIZE
Definition: rte_stack.h:36
static __rte_experimental __rte_always_inline unsigned int rte_stack_push(struct rte_stack *s, void *const *obj_table, unsigned int n)
Definition: rte_stack.h:120