DPDK  23.11.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_debug.h>
23 #include <rte_errno.h>
24 #include <rte_memzone.h>
25 #include <rte_spinlock.h>
26 
27 #define RTE_TAILQ_STACK_NAME "RTE_STACK"
28 #define RTE_STACK_MZ_PREFIX "STK_"
30 #define RTE_STACK_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
31  sizeof(RTE_STACK_MZ_PREFIX) + 1)
32 
33 struct rte_stack_lf_elem {
34  void *data;
35  struct rte_stack_lf_elem *next;
36 };
37 
38 struct rte_stack_lf_head {
39  struct rte_stack_lf_elem *top;
40  uint64_t cnt;
41 };
42 
43 struct rte_stack_lf_list {
45  struct rte_stack_lf_head head __rte_aligned(16);
47  RTE_ATOMIC(uint64_t) len;
48 };
49 
50 /* Structure containing two lock-free LIFO lists: the stack itself and a list
51  * of free linked-list elements.
52  */
53 struct rte_stack_lf {
55  struct rte_stack_lf_list used __rte_cache_aligned;
57  struct rte_stack_lf_list free __rte_cache_aligned;
59  struct rte_stack_lf_elem elems[] __rte_cache_aligned;
60 };
61 
62 /* Structure containing the LIFO, its current length, and a lock for mutual
63  * exclusion.
64  */
65 struct rte_stack_std {
66  rte_spinlock_t lock;
67  uint32_t len;
68  void *objs[];
69 };
70 
71 /* The RTE stack structure contains the LIFO structure itself, plus metadata
72  * such as its name and memzone pointer.
73  */
74 struct rte_stack {
78  const struct rte_memzone *memzone;
79  uint32_t capacity;
80  uint32_t flags;
81  union {
82  struct rte_stack_lf stack_lf;
83  struct rte_stack_std stack_std;
84  };
86 
91 #define RTE_STACK_F_LF 0x0001
92 
93 #include "rte_stack_std.h"
94 #include "rte_stack_lf.h"
95 
108 static __rte_always_inline unsigned int
109 rte_stack_push(struct rte_stack *s, void * const *obj_table, unsigned int n)
110 {
111  RTE_ASSERT(s != NULL);
112  RTE_ASSERT(obj_table != NULL);
113 
114  if (s->flags & RTE_STACK_F_LF)
115  return __rte_stack_lf_push(s, obj_table, n);
116  else
117  return __rte_stack_std_push(s, obj_table, n);
118 }
119 
132 static __rte_always_inline unsigned int
133 rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
134 {
135  RTE_ASSERT(s != NULL);
136  RTE_ASSERT(obj_table != NULL);
137 
138  if (s->flags & RTE_STACK_F_LF)
139  return __rte_stack_lf_pop(s, obj_table, n);
140  else
141  return __rte_stack_std_pop(s, obj_table, n);
142 }
143 
152 static __rte_always_inline unsigned int
153 rte_stack_count(struct rte_stack *s)
154 {
155  RTE_ASSERT(s != NULL);
156 
157  if (s->flags & RTE_STACK_F_LF)
158  return __rte_stack_lf_count(s);
159  else
160  return __rte_stack_std_count(s);
161 }
162 
171 static __rte_always_inline unsigned int
172 rte_stack_free_count(struct rte_stack *s)
173 {
174  RTE_ASSERT(s != NULL);
175 
176  return s->capacity - rte_stack_count(s);
177 }
178 
207 struct rte_stack *
208 rte_stack_create(const char *name, unsigned int count, int socket_id,
209  uint32_t flags);
210 
218 void
219 rte_stack_free(struct rte_stack *s);
220 
232 struct rte_stack *
233 rte_stack_lookup(const char *name);
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 #endif /* _RTE_STACK_H_ */
#define __rte_cache_aligned
Definition: rte_common.h:524
#define __rte_always_inline
Definition: rte_common.h:331
#define RTE_STACK_F_LF
Definition: rte_stack.h:91
#define RTE_STACK_NAMESIZE
Definition: rte_stack.h:30
void rte_stack_free(struct rte_stack *s)
static __rte_always_inline unsigned int rte_stack_count(struct rte_stack *s)
Definition: rte_stack.h:153
struct rte_stack * rte_stack_create(const char *name, unsigned int count, int socket_id, uint32_t flags)
struct rte_stack * rte_stack_lookup(const char *name)
static __rte_always_inline unsigned int rte_stack_push(struct rte_stack *s, void *const *obj_table, unsigned int n)
Definition: rte_stack.h:109
static __rte_always_inline unsigned int rte_stack_free_count(struct rte_stack *s)
Definition: rte_stack.h:172
static __rte_always_inline unsigned int rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
Definition: rte_stack.h:133
uint32_t flags
Definition: rte_memzone.h:64