DPDK 24.11.1
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_"
28#define RTE_STACK_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
29 sizeof(RTE_STACK_MZ_PREFIX) + 1)
30
31struct rte_stack_lf_elem {
32 void *data;
33 struct rte_stack_lf_elem *next;
34};
35
36struct rte_stack_lf_head {
37 struct rte_stack_lf_elem *top;
38 uint64_t cnt;
39};
40
41struct 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 */
51struct 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 */
63struct 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 */
72struct __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
95extern "C" {
96#endif
97
110static __rte_always_inline unsigned int
111rte_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
134static __rte_always_inline unsigned int
135rte_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
154static __rte_always_inline unsigned int
155rte_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
173static __rte_always_inline unsigned int
174rte_stack_free_count(struct rte_stack *s)
175{
176 RTE_ASSERT(s != NULL);
177
178 return s->capacity - rte_stack_count(s);
179}
180
209struct rte_stack *
210rte_stack_create(const char *name, unsigned int count, int socket_id,
211 uint32_t flags);
212
220void
221rte_stack_free(struct rte_stack *s);
222
234struct rte_stack *
235rte_stack_lookup(const char *name);
236
237#ifdef __cplusplus
238}
239#endif
240
241#endif /* _RTE_STACK_H_ */
#define __rte_cache_aligned
Definition: rte_common.h:627
#define __rte_always_inline
Definition: rte_common.h:413
#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)
#define RTE_STACK_NAMESIZE
Definition: rte_stack.h:28
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:155
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
struct rte_stack * rte_stack_lookup(const char *name)
uint32_t flags
Definition: rte_memzone.h:64