DPDK  24.03.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 <stdalign.h>
23 
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  alignas(16) struct rte_stack_lf_head head;
49  RTE_ATOMIC(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  alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_list used;
59  alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_list free;
61  alignas(RTE_CACHE_LINE_SIZE) struct rte_stack_lf_elem elems[];
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_cache_aligned rte_stack {
78  alignas(RTE_CACHE_LINE_SIZE) char name[RTE_STACK_NAMESIZE];
80  const struct rte_memzone *memzone;
81  uint32_t capacity;
82  uint32_t flags;
83  union {
84  struct rte_stack_lf stack_lf;
85  struct rte_stack_std stack_std;
86  };
87 };
88 
93 #define RTE_STACK_F_LF 0x0001
94 
95 #include "rte_stack_std.h"
96 #include "rte_stack_lf.h"
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:355
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:553
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:93
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:32