New upstream version 18.02
[deb_dpdk.git] / drivers / mempool / stack / rte_mempool_stack.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <rte_mempool.h>
7 #include <rte_malloc.h>
8
9 struct rte_mempool_stack {
10         rte_spinlock_t sl;
11
12         uint32_t size;
13         uint32_t len;
14         void *objs[];
15 };
16
17 static int
18 stack_alloc(struct rte_mempool *mp)
19 {
20         struct rte_mempool_stack *s;
21         unsigned n = mp->size;
22         int size = sizeof(*s) + (n+16)*sizeof(void *);
23
24         /* Allocate our local memory structure */
25         s = rte_zmalloc_socket("mempool-stack",
26                         size,
27                         RTE_CACHE_LINE_SIZE,
28                         mp->socket_id);
29         if (s == NULL) {
30                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate stack!\n");
31                 return -ENOMEM;
32         }
33
34         rte_spinlock_init(&s->sl);
35
36         s->size = n;
37         mp->pool_data = s;
38
39         return 0;
40 }
41
42 static int
43 stack_enqueue(struct rte_mempool *mp, void * const *obj_table,
44                 unsigned n)
45 {
46         struct rte_mempool_stack *s = mp->pool_data;
47         void **cache_objs;
48         unsigned index;
49
50         rte_spinlock_lock(&s->sl);
51         cache_objs = &s->objs[s->len];
52
53         /* Is there sufficient space in the stack ? */
54         if ((s->len + n) > s->size) {
55                 rte_spinlock_unlock(&s->sl);
56                 return -ENOBUFS;
57         }
58
59         /* Add elements back into the cache */
60         for (index = 0; index < n; ++index, obj_table++)
61                 cache_objs[index] = *obj_table;
62
63         s->len += n;
64
65         rte_spinlock_unlock(&s->sl);
66         return 0;
67 }
68
69 static int
70 stack_dequeue(struct rte_mempool *mp, void **obj_table,
71                 unsigned n)
72 {
73         struct rte_mempool_stack *s = mp->pool_data;
74         void **cache_objs;
75         unsigned index, len;
76
77         rte_spinlock_lock(&s->sl);
78
79         if (unlikely(n > s->len)) {
80                 rte_spinlock_unlock(&s->sl);
81                 return -ENOENT;
82         }
83
84         cache_objs = s->objs;
85
86         for (index = 0, len = s->len - 1; index < n;
87                         ++index, len--, obj_table++)
88                 *obj_table = cache_objs[len];
89
90         s->len -= n;
91         rte_spinlock_unlock(&s->sl);
92         return 0;
93 }
94
95 static unsigned
96 stack_get_count(const struct rte_mempool *mp)
97 {
98         struct rte_mempool_stack *s = mp->pool_data;
99
100         return s->len;
101 }
102
103 static void
104 stack_free(struct rte_mempool *mp)
105 {
106         rte_free((void *)(mp->pool_data));
107 }
108
109 static struct rte_mempool_ops ops_stack = {
110         .name = "stack",
111         .alloc = stack_alloc,
112         .free = stack_free,
113         .enqueue = stack_enqueue,
114         .dequeue = stack_dequeue,
115         .get_count = stack_get_count
116 };
117
118 MEMPOOL_REGISTER_OPS(ops_stack);