1d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2*4418919fSjohnjiang  * Copyright(c) 2016-2019 Intel Corporation
32bfe3f2eSlogwang  */
42bfe3f2eSlogwang 
52bfe3f2eSlogwang #include <stdio.h>
62bfe3f2eSlogwang #include <rte_mempool.h>
7*4418919fSjohnjiang #include <rte_stack.h>
82bfe3f2eSlogwang 
92bfe3f2eSlogwang static int
__stack_alloc(struct rte_mempool * mp,uint32_t flags)10*4418919fSjohnjiang __stack_alloc(struct rte_mempool *mp, uint32_t flags)
112bfe3f2eSlogwang {
12*4418919fSjohnjiang 	char name[RTE_STACK_NAMESIZE];
13*4418919fSjohnjiang 	struct rte_stack *s;
14*4418919fSjohnjiang 	int ret;
152bfe3f2eSlogwang 
16*4418919fSjohnjiang 	ret = snprintf(name, sizeof(name),
17*4418919fSjohnjiang 		       RTE_MEMPOOL_MZ_FORMAT, mp->name);
18*4418919fSjohnjiang 	if (ret < 0 || ret >= (int)sizeof(name)) {
19*4418919fSjohnjiang 		rte_errno = ENAMETOOLONG;
20*4418919fSjohnjiang 		return -rte_errno;
212bfe3f2eSlogwang 	}
222bfe3f2eSlogwang 
23*4418919fSjohnjiang 	s = rte_stack_create(name, mp->size, mp->socket_id, flags);
24*4418919fSjohnjiang 	if (s == NULL)
25*4418919fSjohnjiang 		return -rte_errno;
262bfe3f2eSlogwang 
272bfe3f2eSlogwang 	mp->pool_data = s;
282bfe3f2eSlogwang 
292bfe3f2eSlogwang 	return 0;
302bfe3f2eSlogwang }
312bfe3f2eSlogwang 
322bfe3f2eSlogwang static int
stack_alloc(struct rte_mempool * mp)33*4418919fSjohnjiang stack_alloc(struct rte_mempool *mp)
342bfe3f2eSlogwang {
35*4418919fSjohnjiang 	return __stack_alloc(mp, 0);
362bfe3f2eSlogwang }
372bfe3f2eSlogwang 
38*4418919fSjohnjiang static int
lf_stack_alloc(struct rte_mempool * mp)39*4418919fSjohnjiang lf_stack_alloc(struct rte_mempool *mp)
40*4418919fSjohnjiang {
41*4418919fSjohnjiang 	return __stack_alloc(mp, RTE_STACK_F_LF);
42*4418919fSjohnjiang }
432bfe3f2eSlogwang 
44*4418919fSjohnjiang static int
stack_enqueue(struct rte_mempool * mp,void * const * obj_table,unsigned int n)45*4418919fSjohnjiang stack_enqueue(struct rte_mempool *mp, void * const *obj_table,
46*4418919fSjohnjiang 	      unsigned int n)
47*4418919fSjohnjiang {
48*4418919fSjohnjiang 	struct rte_stack *s = mp->pool_data;
492bfe3f2eSlogwang 
50*4418919fSjohnjiang 	return rte_stack_push(s, obj_table, n) == 0 ? -ENOBUFS : 0;
512bfe3f2eSlogwang }
522bfe3f2eSlogwang 
532bfe3f2eSlogwang static int
stack_dequeue(struct rte_mempool * mp,void ** obj_table,unsigned int n)542bfe3f2eSlogwang stack_dequeue(struct rte_mempool *mp, void **obj_table,
55*4418919fSjohnjiang 	      unsigned int n)
562bfe3f2eSlogwang {
57*4418919fSjohnjiang 	struct rte_stack *s = mp->pool_data;
582bfe3f2eSlogwang 
59*4418919fSjohnjiang 	return rte_stack_pop(s, obj_table, n) == 0 ? -ENOBUFS : 0;
602bfe3f2eSlogwang }
612bfe3f2eSlogwang 
622bfe3f2eSlogwang static unsigned
stack_get_count(const struct rte_mempool * mp)632bfe3f2eSlogwang stack_get_count(const struct rte_mempool *mp)
642bfe3f2eSlogwang {
65*4418919fSjohnjiang 	struct rte_stack *s = mp->pool_data;
662bfe3f2eSlogwang 
67*4418919fSjohnjiang 	return rte_stack_count(s);
682bfe3f2eSlogwang }
692bfe3f2eSlogwang 
702bfe3f2eSlogwang static void
stack_free(struct rte_mempool * mp)712bfe3f2eSlogwang stack_free(struct rte_mempool *mp)
722bfe3f2eSlogwang {
73*4418919fSjohnjiang 	struct rte_stack *s = mp->pool_data;
74*4418919fSjohnjiang 
75*4418919fSjohnjiang 	rte_stack_free(s);
762bfe3f2eSlogwang }
772bfe3f2eSlogwang 
782bfe3f2eSlogwang static struct rte_mempool_ops ops_stack = {
792bfe3f2eSlogwang 	.name = "stack",
802bfe3f2eSlogwang 	.alloc = stack_alloc,
812bfe3f2eSlogwang 	.free = stack_free,
822bfe3f2eSlogwang 	.enqueue = stack_enqueue,
832bfe3f2eSlogwang 	.dequeue = stack_dequeue,
842bfe3f2eSlogwang 	.get_count = stack_get_count
852bfe3f2eSlogwang };
862bfe3f2eSlogwang 
87*4418919fSjohnjiang static struct rte_mempool_ops ops_lf_stack = {
88*4418919fSjohnjiang 	.name = "lf_stack",
89*4418919fSjohnjiang 	.alloc = lf_stack_alloc,
90*4418919fSjohnjiang 	.free = stack_free,
91*4418919fSjohnjiang 	.enqueue = stack_enqueue,
92*4418919fSjohnjiang 	.dequeue = stack_dequeue,
93*4418919fSjohnjiang 	.get_count = stack_get_count
94*4418919fSjohnjiang };
95*4418919fSjohnjiang 
962bfe3f2eSlogwang MEMPOOL_REGISTER_OPS(ops_stack);
97*4418919fSjohnjiang MEMPOOL_REGISTER_OPS(ops_lf_stack);
98