1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation.
3 * Copyright(c) 2016 6WIND S.A.
9 #include <rte_mempool.h>
10 #include <rte_errno.h>
13 /* indirect jump table to support external memory pools. */
14 struct rte_mempool_ops_table rte_mempool_ops_table = {
15 .sl = RTE_SPINLOCK_INITIALIZER,
19 /* add a new ops struct in rte_mempool_ops_table, return its index. */
21 rte_mempool_register_ops(const struct rte_mempool_ops *h)
23 struct rte_mempool_ops *ops;
26 rte_spinlock_lock(&rte_mempool_ops_table.sl);
28 if (rte_mempool_ops_table.num_ops >=
29 RTE_MEMPOOL_MAX_OPS_IDX) {
30 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
32 "Maximum number of mempool ops structs exceeded\n");
36 if (h->alloc == NULL || h->enqueue == NULL ||
37 h->dequeue == NULL || h->get_count == NULL) {
38 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
40 "Missing callback while registering mempool ops\n");
44 if (strlen(h->name) >= sizeof(ops->name) - 1) {
45 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
46 RTE_LOG(DEBUG, EAL, "%s(): mempool_ops <%s>: name too long\n",
52 ops_index = rte_mempool_ops_table.num_ops++;
53 ops = &rte_mempool_ops_table.ops[ops_index];
54 snprintf(ops->name, sizeof(ops->name), "%s", h->name);
55 ops->alloc = h->alloc;
57 ops->enqueue = h->enqueue;
58 ops->dequeue = h->dequeue;
59 ops->get_count = h->get_count;
60 ops->calc_mem_size = h->calc_mem_size;
61 ops->populate = h->populate;
62 ops->get_info = h->get_info;
63 ops->dequeue_contig_blocks = h->dequeue_contig_blocks;
65 rte_spinlock_unlock(&rte_mempool_ops_table.sl);
70 /* wrapper to allocate an external mempool's private (pool) data. */
72 rte_mempool_ops_alloc(struct rte_mempool *mp)
74 struct rte_mempool_ops *ops;
76 ops = rte_mempool_get_ops(mp->ops_index);
77 return ops->alloc(mp);
80 /* wrapper to free an external pool ops. */
82 rte_mempool_ops_free(struct rte_mempool *mp)
84 struct rte_mempool_ops *ops;
86 ops = rte_mempool_get_ops(mp->ops_index);
87 if (ops->free == NULL)
92 /* wrapper to get available objects in an external mempool. */
94 rte_mempool_ops_get_count(const struct rte_mempool *mp)
96 struct rte_mempool_ops *ops;
98 ops = rte_mempool_get_ops(mp->ops_index);
99 return ops->get_count(mp);
102 /* wrapper to notify new memory area to external mempool */
104 rte_mempool_ops_calc_mem_size(const struct rte_mempool *mp,
105 uint32_t obj_num, uint32_t pg_shift,
106 size_t *min_chunk_size, size_t *align)
108 struct rte_mempool_ops *ops;
110 ops = rte_mempool_get_ops(mp->ops_index);
112 if (ops->calc_mem_size == NULL)
113 return rte_mempool_op_calc_mem_size_default(mp, obj_num,
114 pg_shift, min_chunk_size, align);
116 return ops->calc_mem_size(mp, obj_num, pg_shift, min_chunk_size, align);
119 /* wrapper to populate memory pool objects using provided memory chunk */
121 rte_mempool_ops_populate(struct rte_mempool *mp, unsigned int max_objs,
122 void *vaddr, rte_iova_t iova, size_t len,
123 rte_mempool_populate_obj_cb_t *obj_cb,
126 struct rte_mempool_ops *ops;
128 ops = rte_mempool_get_ops(mp->ops_index);
130 if (ops->populate == NULL)
131 return rte_mempool_op_populate_default(mp, max_objs, vaddr,
135 return ops->populate(mp, max_objs, vaddr, iova, len, obj_cb,
139 /* wrapper to get additional mempool info */
141 rte_mempool_ops_get_info(const struct rte_mempool *mp,
142 struct rte_mempool_info *info)
144 struct rte_mempool_ops *ops;
146 ops = rte_mempool_get_ops(mp->ops_index);
148 RTE_FUNC_PTR_OR_ERR_RET(ops->get_info, -ENOTSUP);
149 return ops->get_info(mp, info);
153 /* sets mempool ops previously registered by rte_mempool_register_ops. */
155 rte_mempool_set_ops_byname(struct rte_mempool *mp, const char *name,
158 struct rte_mempool_ops *ops = NULL;
161 /* too late, the mempool is already populated. */
162 if (mp->flags & MEMPOOL_F_POOL_CREATED)
165 for (i = 0; i < rte_mempool_ops_table.num_ops; i++) {
167 rte_mempool_ops_table.ops[i].name)) {
168 ops = &rte_mempool_ops_table.ops[i];
177 mp->pool_config = pool_config;