DPDK  21.02.0
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 
15 #ifndef _RTE_STACK_H_
16 #define _RTE_STACK_H_
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <rte_atomic.h>
23 #include <rte_compat.h>
24 #include <rte_debug.h>
25 #include <rte_errno.h>
26 #include <rte_memzone.h>
27 #include <rte_spinlock.h>
28 
29 #define RTE_TAILQ_STACK_NAME "RTE_STACK"
30 #define RTE_STACK_MZ_PREFIX "STK_"
31 
32 #define RTE_STACK_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
33  sizeof(RTE_STACK_MZ_PREFIX) + 1)
34 
35 struct rte_stack_lf_elem {
36  void *data;
37  struct rte_stack_lf_elem *next;
38 };
39 
40 struct rte_stack_lf_head {
41  struct rte_stack_lf_elem *top;
42  uint64_t cnt;
43 };
44 
45 struct rte_stack_lf_list {
47  struct rte_stack_lf_head head __rte_aligned(16);
49  uint64_t len;
50 };
51 
52 /* Structure containing two lock-free LIFO lists: the stack itself and a list
53  * of free linked-list elements.
54  */
55 struct rte_stack_lf {
57  struct rte_stack_lf_list used __rte_cache_aligned;
59  struct rte_stack_lf_list free __rte_cache_aligned;
61  struct rte_stack_lf_elem elems[] __rte_cache_aligned;
62 };
63 
64 /* Structure containing the LIFO, its current length, and a lock for mutual
65  * exclusion.
66  */
67 struct rte_stack_std {
68  rte_spinlock_t lock;
69  uint32_t len;
70  void *objs[];
71 };
72 
73 /* The RTE stack structure contains the LIFO structure itself, plus metadata
74  * such as its name and memzone pointer.
75  */
76 struct rte_stack {
80  const struct rte_memzone *memzone;
81  uint32_t capacity;
82  uint32_t flags;
84  union {
85  struct rte_stack_lf stack_lf;
86  struct rte_stack_std stack_std;
87  };
89 
94 #define RTE_STACK_F_LF 0x0001
95 
96 #include "rte_stack_std.h"
97 #include "rte_stack_lf.h"
98 
111 static __rte_always_inline unsigned int
112 rte_stack_push(struct rte_stack *s, void * const *obj_table, unsigned int n)
113 {
114  RTE_ASSERT(s != NULL);
115  RTE_ASSERT(obj_table != NULL);
116 
117  if (s->flags & RTE_STACK_F_LF)
118  return __rte_stack_lf_push(s, obj_table, n);
119  else
120  return __rte_stack_std_push(s, obj_table, n);
121 }
122 
135 static __rte_always_inline unsigned int
136 rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
137 {
138  RTE_ASSERT(s != NULL);
139  RTE_ASSERT(obj_table != NULL);
140 
141  if (s->flags & RTE_STACK_F_LF)
142  return __rte_stack_lf_pop(s, obj_table, n);
143  else
144  return __rte_stack_std_pop(s, obj_table, n);
145 }
146 
155 static __rte_always_inline unsigned int
156 rte_stack_count(struct rte_stack *s)
157 {
158  RTE_ASSERT(s != NULL);
159 
160  if (s->flags & RTE_STACK_F_LF)
161  return __rte_stack_lf_count(s);
162  else
163  return __rte_stack_std_count(s);
164 }
165 
174 static __rte_always_inline unsigned int
175 rte_stack_free_count(struct rte_stack *s)
176 {
177  RTE_ASSERT(s != NULL);
178 
179  return s->capacity - rte_stack_count(s);
180 }
181 
209 struct rte_stack *
210 rte_stack_create(const char *name, unsigned int count, int socket_id,
211  uint32_t flags);
212 
219 void
220 rte_stack_free(struct rte_stack *s);
221 
233 struct rte_stack *
234 rte_stack_lookup(const char *name);
235 
236 #ifdef __cplusplus
237 }
238 #endif
239 
240 #endif /* _RTE_STACK_H_ */
void rte_stack_free(struct rte_stack *s)
#define __rte_always_inline
Definition: rte_common.h:226
uint32_t flags
Definition: rte_memzone.h:66
struct rte_stack * rte_stack_lookup(const char *name)
static __rte_always_inline unsigned int rte_stack_count(struct rte_stack *s)
Definition: rte_stack.h:156
#define RTE_STACK_F_LF
Definition: rte_stack.h:94
struct rte_stack * rte_stack_create(const char *name, unsigned int count, int socket_id, uint32_t flags)
static __rte_always_inline unsigned int rte_stack_push(struct rte_stack *s, void *const *obj_table, unsigned int n)
Definition: rte_stack.h:112
static __rte_always_inline unsigned int rte_stack_free_count(struct rte_stack *s)
Definition: rte_stack.h:175
#define __rte_cache_aligned
Definition: rte_common.h:400
#define RTE_STD_C11
Definition: rte_common.h:40
static __rte_always_inline unsigned int rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
Definition: rte_stack.h:136
#define RTE_STACK_NAMESIZE
Definition: rte_stack.h:32