EM-ODP 4.4.0
Event Machine on ODP
Loading...
Searching...
No Matches
objpool.c
1/*
2 * Copyright (c) 2015-2026, Nokia Solutions and Networks
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
32#include <objpool.h>
33#include <list.h>
34
35static inline objpool_elem_t *
36objpool_node2elem(list_node_t *const list_node);
37
38int objpool_init(objpool_t *const objpool, uint32_t nbr_subpools)
39{
40 if (unlikely(!objpool || nbr_subpools == 0))
41 return -1;
42
43 if (nbr_subpools > OBJSUBPOOLS_MAX)
44 nbr_subpools = OBJSUBPOOLS_MAX;
45
46 objpool->nbr_subpools = nbr_subpools;
47
48 for (uint32_t i = 0; i < nbr_subpools; i++) {
49 objsubpool_t *const subpool = &objpool->subpool[i];
50
51 odp_ticketlock_init(&subpool->lock);
52 list_init(&subpool->list_head);
53 }
54
55 return 0;
56}
57
58uint32_t objpool_subpools(const objpool_t *const objpool)
59{
60 return objpool->nbr_subpools;
61}
62
63void objpool_add(objpool_t *const objpool, uint32_t subpool_idx,
64 objpool_elem_t *const elem)
65{
66 const uint32_t idx = subpool_idx % objpool->nbr_subpools;
67 objsubpool_t *const subpool = &objpool->subpool[idx];
68
69 elem->subpool_idx = idx;
70
71 odp_ticketlock_lock(&subpool->lock);
72 list_add(&subpool->list_head, &elem->list_node);
73 elem->in_pool = 1; /* true */
74 odp_ticketlock_unlock(&subpool->lock);
75}
76
78objpool_rem(objpool_t *const objpool, uint32_t subpool_idx)
79{
80 objpool_elem_t *elem = NULL;
81
82 for (uint32_t i = 0; i < objpool->nbr_subpools; i++) {
83 const uint32_t idx = (subpool_idx + i) % objpool->nbr_subpools;
84 objsubpool_t *const subpool = &objpool->subpool[idx];
85
86 odp_ticketlock_lock(&subpool->lock);
87
88 list_node_t *const node = list_rem_first(&subpool->list_head);
89
90 if (node != NULL) {
91 elem = objpool_node2elem(node);
92 elem->in_pool = 0; /* false */
93 }
94
95 odp_ticketlock_unlock(&subpool->lock);
96
97 if (node != NULL)
98 return elem;
99 }
100
101 return NULL;
102}
103
104int objpool_rem_elem(objpool_t *const objpool, objpool_elem_t *const elem)
105{
106 const uint32_t idx = elem->subpool_idx;
107 objsubpool_t *const subpool = &objpool->subpool[idx];
108 int ret = -1;
109
110 odp_ticketlock_lock(&subpool->lock);
111 if (elem->in_pool) {
112 list_rem(&subpool->list_head, &elem->list_node);
113 elem->in_pool = 0; /* false */
114 ret = 0;
115 }
116 odp_ticketlock_unlock(&subpool->lock);
117
118 return ret;
119}
120
121static inline objpool_elem_t *
122objpool_node2elem(list_node_t *const node)
123{
124 return (objpool_elem_t *)((uintptr_t)node - offsetof(objpool_elem_t, list_node));
125}
#define OBJSUBPOOLS_MAX
Definition objpool.h:62
uint32_t subpool_idx
Definition objpool.h:74
uint32_t in_pool
Definition objpool.h:76
list_node_t list_node
Definition objpool.h:72
uint32_t nbr_subpools
Definition objpool.h:99
objsubpool_t subpool[OBJSUBPOOLS_MAX]
Definition objpool.h:97
list_node_t list_head
Definition objpool.h:87
odp_ticketlock_t lock
Definition objpool.h:85