dpdk/drivers/mempool/stack/rte_mempool_stack.c
<<
>>
Prefs
   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
   9static int
  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
  32static int
  33stack_alloc(struct rte_mempool *mp)
  34{
  35        return __stack_alloc(mp, 0);
  36}
  37
  38static int
  39lf_stack_alloc(struct rte_mempool *mp)
  40{
  41        return __stack_alloc(mp, RTE_STACK_F_LF);
  42}
  43
  44static int
  45stack_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
  53static int
  54stack_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
  62static unsigned
  63stack_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
  70static void
  71stack_free(struct rte_mempool *mp)
  72{
  73        struct rte_stack *s = mp->pool_data;
  74
  75        rte_stack_free(s);
  76}
  77
  78static 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
  87static 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
  96MEMPOOL_REGISTER_OPS(ops_stack);
  97MEMPOOL_REGISTER_OPS(ops_lf_stack);
  98