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