xref: /dpdk/drivers/mempool/stack/rte_mempool_stack.c (revision cb77b060ebecb9a14c5f7c7622535a1f6e71e2fa)
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
__stack_alloc(struct rte_mempool * mp,uint32_t flags)10 __stack_alloc(struct rte_mempool *mp, uint32_t flags)
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, flags);
24 	if (s == NULL)
25 		return -rte_errno;
26 
27 	mp->pool_data = s;
28 
29 	return 0;
30 }
31 
32 static int
stack_alloc(struct rte_mempool * mp)33 stack_alloc(struct rte_mempool *mp)
34 {
35 	return __stack_alloc(mp, 0);
36 }
37 
38 static int
lf_stack_alloc(struct rte_mempool * mp)39 lf_stack_alloc(struct rte_mempool *mp)
40 {
41 	return __stack_alloc(mp, RTE_STACK_F_LF);
42 }
43 
44 static int
stack_enqueue(struct rte_mempool * mp,void * const * obj_table,unsigned int n)45 stack_enqueue(struct rte_mempool *mp, void * const *obj_table,
46 	      unsigned int n)
47 {
48 	struct rte_stack *s = mp->pool_data;
49 
50 	return rte_stack_push(s, obj_table, n) == 0 ? -ENOBUFS : 0;
51 }
52 
53 static int
stack_dequeue(struct rte_mempool * mp,void ** obj_table,unsigned int n)54 stack_dequeue(struct rte_mempool *mp, void **obj_table,
55 	      unsigned int n)
56 {
57 	struct rte_stack *s = mp->pool_data;
58 
59 	return rte_stack_pop(s, obj_table, n) == 0 ? -ENOBUFS : 0;
60 }
61 
62 static unsigned
stack_get_count(const struct rte_mempool * mp)63 stack_get_count(const struct rte_mempool *mp)
64 {
65 	struct rte_stack *s = mp->pool_data;
66 
67 	return rte_stack_count(s);
68 }
69 
70 static void
stack_free(struct rte_mempool * mp)71 stack_free(struct rte_mempool *mp)
72 {
73 	struct rte_stack *s = mp->pool_data;
74 
75 	rte_stack_free(s);
76 }
77 
78 static struct rte_mempool_ops ops_stack = {
79 	.name = "stack",
80 	.alloc = stack_alloc,
81 	.free = stack_free,
82 	.enqueue = stack_enqueue,
83 	.dequeue = stack_dequeue,
84 	.get_count = stack_get_count
85 };
86 
87 static struct rte_mempool_ops ops_lf_stack = {
88 	.name = "lf_stack",
89 	.alloc = lf_stack_alloc,
90 	.free = stack_free,
91 	.enqueue = stack_enqueue,
92 	.dequeue = stack_dequeue,
93 	.get_count = stack_get_count
94 };
95 
96 RTE_MEMPOOL_REGISTER_OPS(ops_stack);
97 RTE_MEMPOOL_REGISTER_OPS(ops_lf_stack);
98