EM-ODP 4.4.0
Event Machine on ODP
Loading...
Searching...
No Matches
objpool.h
Go to the documentation of this file.
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
31#ifndef OBJPOOL_H_
32#define OBJPOOL_H_
33
34/**
35 * @file
36 * Simple object pool for managing collections of objects.
37 *
38 * Objects stored in the pool must embed an objpool_elem_t member that is used
39 * internally to link the object into the pool. The pool is divided into
40 * independently locked subpools to reduce contention in multithreaded use.
41 */
42
43#include <stdint.h>
44
46
47#include "list.h"
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/**
54 * Maximum number of subpools in an object pool.
55 *
56 * This compile-time limit is set to 8 as a balance between reducing lock
57 * contention (more subpools) and keeping the objpool_t structure small enough
58 * to preserve cache locality. Increasing this value is possible if a
59 * particular workload benefits from more independently locked subpools, but
60 * doing so will grow the size of objpool_t proportionally.
61 */
62#define OBJSUBPOOLS_MAX 8
63
64/**
65 * Object pool element, embedded in each object stored in the pool.
66 *
67 * Must be included as a member of any struct that is to be managed by
68 * an objpool_t. The containing struct can be recovered with offsetof().
69 */
70typedef struct {
71 /** node for linking into a subpool list */
73 /** subpool index: 0 .. objpool_t::nbr_subpools - 1 */
74 uint32_t subpool_idx;
75 /** in pool: 1 = yes, 0 = no */
76 uint32_t in_pool;
78
79/** A single independently locked subpool within an object pool */
80typedef union {
81 /** Padding to half a cache line */
82 uint8_t u8[ENV_CACHE_LINE_SIZE / 2];
83 struct {
84 /** Lock protecting this subpool */
85 odp_ticketlock_t lock;
86 /** Head of the free-element list */
88 };
90
91COMPILE_TIME_ASSERT(2 * sizeof(objsubpool_t) == ENV_CACHE_LINE_SIZE,
92 OBJSUBPOOL_T_SIZE_ERROR);
93
94/** Object pool consisting of independently locked subpools */
95typedef struct {
96 /** Array of subpools */
98 /** Number of subpools in use: 1 .. OBJSUBPOOLS_MAX */
99 uint32_t nbr_subpools;
100} objpool_t;
101
102/**
103 * Initialize an object pool.
104 *
105 * @param objpool Object pool to initialize.
106 * @param nbr_subpools Number of subpools, clamped to OBJSUBPOOLS_MAX.
107 *
108 * @return 0 on success.
109 * @retval <0 on failure (invalid parameters).
110 */
111int objpool_init(objpool_t *const objpool, uint32_t nbr_subpools);
112
113/**
114 * Return the number of subpools in use by the object pool.
115 *
116 * @param objpool Object pool.
117 *
118 * @return Number of subpools.
119 */
120uint32_t objpool_subpools(const objpool_t *const objpool);
121
122/**
123 * Add an object to the object pool.
124 *
125 * @param objpool Object pool.
126 * @param subpool_idx Index of the subpool to add the element to
127 * (modulo nbr_subpools).
128 * @param elem Object pool element embedded in the object to add.
129 */
130void objpool_add(objpool_t *const objpool, uint32_t subpool_idx,
131 objpool_elem_t *const elem);
132
133/**
134 * Remove and return an object from the object pool.
135 *
136 * Tries the given subpool first, then searches the remaining subpools
137 * round-robin until a free object is found.
138 *
139 * @param objpool Object pool.
140 * @param subpool_idx Preferred subpool index to remove from first.
141 *
142 * @return Pointer to the removed element, or NULL if the pool is empty.
143 */
145objpool_rem(objpool_t *const objpool, uint32_t subpool_idx);
146
147/**
148 * Remove a specific object from the object pool.
149 *
150 * @param objpool Object pool.
151 * @param elem Object pool element to remove.
152 *
153 * @return 0 on success, -1 if the element was not in the pool.
154 */
155int objpool_rem_elem(objpool_t *const objpool, objpool_elem_t *const elem);
156
157/**
158 * Check whether an object is currently stored in an object pool.
159 *
160 * @param elem Object pool element to check.
161 *
162 * @return 1 if in pool, 0 if not.
163 */
164static inline int
165objpool_in_pool(const objpool_elem_t *elem)
166{
167 return elem->in_pool;
168}
169
170#ifdef __cplusplus
171}
172#endif
173
174#endif /* OBJPOOL_H_ */
int objpool_rem_elem(objpool_t *const objpool, objpool_elem_t *const elem)
Definition objpool.c:104
objpool_elem_t * objpool_rem(objpool_t *const objpool, uint32_t subpool_idx)
Definition objpool.c:78
#define OBJSUBPOOLS_MAX
Definition objpool.h:62
uint32_t objpool_subpools(const objpool_t *const objpool)
Definition objpool.c:58
void objpool_add(objpool_t *const objpool, uint32_t subpool_idx, objpool_elem_t *const elem)
Definition objpool.c:63
int objpool_init(objpool_t *const objpool, uint32_t nbr_subpools)
Definition objpool.c:38
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
list_node_t list_head
Definition objpool.h:87
odp_ticketlock_t lock
Definition objpool.h:85