1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
7 * @b EXPERIMENTAL: this API may change without prior notice
11 * librte_stack provides an API for configuration and use of a bounded stack of
12 * pointers. Push and pop operations are MT-safe, allowing concurrent access,
13 * and the interface supports pushing and popping multiple pointers at a time.
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>
29 #define RTE_TAILQ_STACK_NAME "RTE_STACK"
30 #define RTE_STACK_MZ_PREFIX "STK_"
31 /** The maximum length of a stack name. */
32 #define RTE_STACK_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
33 sizeof(RTE_STACK_MZ_PREFIX) + 1)
35 /* Structure containing the LIFO, its current length, and a lock for mutual
38 struct rte_stack_std {
39 rte_spinlock_t lock; /**< LIFO lock */
40 uint32_t len; /**< LIFO len */
41 void *objs[]; /**< LIFO pointer table */
44 /* The RTE stack structure contains the LIFO structure itself, plus metadata
45 * such as its name and memzone pointer.
48 /** Name of the stack. */
49 char name[RTE_STACK_NAMESIZE] __rte_cache_aligned;
50 /** Memzone containing the rte_stack structure. */
51 const struct rte_memzone *memzone;
52 uint32_t capacity; /**< Usable size of the stack. */
53 uint32_t flags; /**< Flags supplied at creation. */
54 struct rte_stack_std stack_std; /**< LIFO structure. */
55 } __rte_cache_aligned;
57 #include "rte_stack_std.h"
61 * @b EXPERIMENTAL: this API may change without prior notice
63 * Push several objects on the stack (MT-safe).
66 * A pointer to the stack structure.
68 * A pointer to a table of void * pointers (objects).
70 * The number of objects to push on the stack from the obj_table.
72 * Actual number of objects pushed (either 0 or *n*).
74 static __rte_always_inline unsigned int __rte_experimental
75 rte_stack_push(struct rte_stack *s, void * const *obj_table, unsigned int n)
77 RTE_ASSERT(s != NULL);
78 RTE_ASSERT(obj_table != NULL);
80 return __rte_stack_std_push(s, obj_table, n);
85 * @b EXPERIMENTAL: this API may change without prior notice
87 * Pop several objects from the stack (MT-safe).
90 * A pointer to the stack structure.
92 * A pointer to a table of void * pointers (objects).
94 * The number of objects to pull from the stack.
96 * Actual number of objects popped (either 0 or *n*).
98 static __rte_always_inline unsigned int __rte_experimental
99 rte_stack_pop(struct rte_stack *s, void **obj_table, unsigned int n)
101 RTE_ASSERT(s != NULL);
102 RTE_ASSERT(obj_table != NULL);
104 return __rte_stack_std_pop(s, obj_table, n);
109 * @b EXPERIMENTAL: this API may change without prior notice
111 * Return the number of used entries in a stack.
114 * A pointer to the stack structure.
116 * The number of used entries in the stack.
118 static __rte_always_inline unsigned int __rte_experimental
119 rte_stack_count(struct rte_stack *s)
121 RTE_ASSERT(s != NULL);
123 return __rte_stack_std_count(s);
128 * @b EXPERIMENTAL: this API may change without prior notice
130 * Return the number of free entries in a stack.
133 * A pointer to the stack structure.
135 * The number of free entries in the stack.
137 static __rte_always_inline unsigned int __rte_experimental
138 rte_stack_free_count(struct rte_stack *s)
140 RTE_ASSERT(s != NULL);
142 return s->capacity - rte_stack_count(s);
147 * @b EXPERIMENTAL: this API may change without prior notice
149 * Create a new stack named *name* in memory.
151 * This function uses ``memzone_reserve()`` to allocate memory for a stack of
152 * size *count*. The behavior of the stack is controlled by the *flags*.
155 * The name of the stack.
157 * The size of the stack.
159 * The *socket_id* argument is the socket identifier in case of
160 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
161 * constraint for the reserved zone.
163 * Reserved for future use.
165 * On success, the pointer to the new allocated stack. NULL on error with
166 * rte_errno set appropriately. Possible errno values include:
167 * - ENOSPC - the maximum number of memzones has already been allocated
168 * - EEXIST - a stack with the same name already exists
169 * - ENOMEM - insufficient memory to create the stack
170 * - ENAMETOOLONG - name size exceeds RTE_STACK_NAMESIZE
172 struct rte_stack *__rte_experimental
173 rte_stack_create(const char *name, unsigned int count, int socket_id,
178 * @b EXPERIMENTAL: this API may change without prior notice
180 * Free all memory used by the stack.
185 void __rte_experimental
186 rte_stack_free(struct rte_stack *s);
190 * @b EXPERIMENTAL: this API may change without prior notice
192 * Lookup a stack by its name.
195 * The name of the stack.
197 * The pointer to the stack matching the name, or NULL if not found,
198 * with rte_errno set appropriately. Possible rte_errno values include:
199 * - ENOENT - Stack with name *name* not found.
200 * - EINVAL - *name* pointer is NULL.
202 struct rte_stack * __rte_experimental
203 rte_stack_lookup(const char *name);
209 #endif /* _RTE_STACK_H_ */