1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2019 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <rte_mempool.h>
7 #include <rte_stack.h>
8 
9 static int
10 stack_alloc(struct rte_mempool *mp)
11 {
12 	char name[RTE_STACK_NAMESIZE];
13 	struct rte_stack *s;
14 	int ret;
15 
16 	ret = snprintf(name, sizeof(name),
17 		       RTE_MEMPOOL_MZ_FORMAT, mp->name);
18 	if (ret < 0 || ret >= (int)sizeof(name)) {
19 		rte_errno = ENAMETOOLONG;
20 		return -rte_errno;
21 	}
22 
23 	s = rte_stack_create(name, mp->size, mp->socket_id, 0);
24 	if (s == NULL)
25 		return -rte_errno;
26 
27 	mp->pool_data = s;
28 
29 	return 0;
30 }
31 
32 static int
33 stack_enqueue(struct rte_mempool *mp, void * const *obj_table,
34 	      unsigned int n)
35 {
36 	struct rte_stack *s = mp->pool_data;
37 
38 	return rte_stack_push(s, obj_table, n) == 0 ? -ENOBUFS : 0;
39 }
40 
41 static int
42 stack_dequeue(struct rte_mempool *mp, void **obj_table,
43 	      unsigned int n)
44 {
45 	struct rte_stack *s = mp->pool_data;
46 
47 	return rte_stack_pop(s, obj_table, n) == 0 ? -ENOBUFS : 0;
48 }
49 
50 static unsigned
51 stack_get_count(const struct rte_mempool *mp)
52 {
53 	struct rte_stack *s = mp->pool_data;
54 
55 	return rte_stack_count(s);
56 }
57 
58 static void
59 stack_free(struct rte_mempool *mp)
60 {
61 	struct rte_stack *s = mp->pool_data;
62 
63 	rte_stack_free(s);
64 }
65 
66 static struct rte_mempool_ops ops_stack = {
67 	.name = "stack",
68 	.alloc = stack_alloc,
69 	.free = stack_free,
70 	.enqueue = stack_enqueue,
71 	.dequeue = stack_dequeue,
72 	.get_count = stack_get_count
73 };
74 
75 MEMPOOL_REGISTER_OPS(ops_stack);
76