1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
6 #include <rte_mempool.h>
7 #include <rte_malloc.h>
9 struct rte_mempool_stack {
18 stack_alloc(struct rte_mempool *mp)
20 struct rte_mempool_stack *s;
21 unsigned n = mp->size;
22 int size = sizeof(*s) + (n+16)*sizeof(void *);
24 /* Allocate our local memory structure */
25 s = rte_zmalloc_socket("mempool-stack",
30 RTE_LOG(ERR, MEMPOOL, "Cannot allocate stack!\n");
34 rte_spinlock_init(&s->sl);
43 stack_enqueue(struct rte_mempool *mp, void * const *obj_table,
46 struct rte_mempool_stack *s = mp->pool_data;
50 rte_spinlock_lock(&s->sl);
51 cache_objs = &s->objs[s->len];
53 /* Is there sufficient space in the stack ? */
54 if ((s->len + n) > s->size) {
55 rte_spinlock_unlock(&s->sl);
59 /* Add elements back into the cache */
60 for (index = 0; index < n; ++index, obj_table++)
61 cache_objs[index] = *obj_table;
65 rte_spinlock_unlock(&s->sl);
70 stack_dequeue(struct rte_mempool *mp, void **obj_table,
73 struct rte_mempool_stack *s = mp->pool_data;
77 rte_spinlock_lock(&s->sl);
79 if (unlikely(n > s->len)) {
80 rte_spinlock_unlock(&s->sl);
86 for (index = 0, len = s->len - 1; index < n;
87 ++index, len--, obj_table++)
88 *obj_table = cache_objs[len];
91 rte_spinlock_unlock(&s->sl);
96 stack_get_count(const struct rte_mempool *mp)
98 struct rte_mempool_stack *s = mp->pool_data;
104 stack_free(struct rte_mempool *mp)
106 rte_free((void *)(mp->pool_data));
109 static struct rte_mempool_ops ops_stack = {
111 .alloc = stack_alloc,
113 .enqueue = stack_enqueue,
114 .dequeue = stack_dequeue,
115 .get_count = stack_get_count
118 MEMPOOL_REGISTER_OPS(ops_stack);