A device detection library that is used natively or by 51Degrees products

pool.h

1/* *********************************************************************
2 * This Original Work is copyright of 51 Degrees Mobile Experts Limited.
3 * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
4 * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
5 *
6 * This Original Work is licensed under the European Union Public Licence
7 * (EUPL) v.1.2 and is subject to its terms as set out below.
8 *
9 * If a copy of the EUPL was not distributed with this file, You can obtain
10 * one at https://opensource.org/licenses/EUPL-1.2.
11 *
12 * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
13 * amended by the European Commission) shall be deemed incompatible for
14 * the purposes of the Work and the provisions of the compatibility
15 * clause in Article 5 of the EUPL shall not apply.
16 *
17 * If using the Work as, or as part of, a network application, by
18 * including the attribution notice(s) required under Article 5 of the EUPL
19 * in the end user terms of the application under an appropriate heading,
20 * such notice(s) shall fulfill the requirements of that article.
21 * ********************************************************************* */
22
23#ifndef FIFTYONE_DEGREES_POOL_H_INCLUDED
24#define FIFTYONE_DEGREES_POOL_H_INCLUDED
25
26#ifdef FIFTYONE_DEGREES_IPI_SYMBOLS
27#include "ipiSymbols.h"
28#endif
29
122
123#include <stdio.h>
124#include <errno.h>
125#include <stdint.h>
126#ifdef _MSC_VER
127#pragma warning (push)
128#pragma warning (disable: 5105)
129#include <windows.h>
130#pragma warning (default: 5105)
131#pragma warning (pop)
132#endif
133#include <assert.h>
134#include <limits.h>
135#include "data.h"
136#include "exceptions.h"
137#include "memory.h"
138#ifndef FIFTYONE_DEGREES_NO_THREADING
139#include "threading.h"
140#endif
141#include "common.h"
142
144typedef struct fiftyone_degrees_pool_item_t fiftyoneDegreesPoolItem;
145typedef struct fiftyone_degrees_pool_t fiftyoneDegreesPool;
147
156typedef void*(*fiftyoneDegreesPoolResourceCreate)(
158 void *state,
159 fiftyoneDegreesException *exception);
160
168typedef size_t(*fiftyoneDegreesPoolResourceSize)(void *state);
169
176 fiftyoneDegreesPool *pool,
177 void *resource);
178
182typedef struct fiftyone_degrees_pool_item_t {
183 void *resource;
184 uint16_t next;
187
191typedef union fiftyone_degrees_pool_head_t {
192 volatile long exchange;
193 struct {
194 uint16_t index;
195 uint16_t aba;
196 } values;
198
202typedef struct fiftyone_degrees_pool_t {
206 uint16_t count;
209
229 uint16_t concurrency,
230 void *state,
233 fiftyoneDegreesException *exception);
234
246 fiftyoneDegreesException *exception);
247
254
261
267
271
272#endif
void fiftyoneDegreesPoolFree(fiftyoneDegreesPool *pool)
Releases the items used by the pool freeing the resources used by each item by calling the resourceFr...
fiftyoneDegreesPoolItem * fiftyoneDegreesPoolItemGet(fiftyoneDegreesPool *pool, fiftyoneDegreesException *exception)
Gets the next free item from the pool for exclusive use by the caller.
void fiftyoneDegreesPoolItemRelease(fiftyoneDegreesPoolItem *item)
Releases the item back to the pool it belongs ready to be reused by another operation.
void(* fiftyoneDegreesPoolResourceFree)(fiftyoneDegreesPool *pool, void *resource)
Frees a resource previously created with fiftyoneDegreesPoolResourceCreate.
Definition pool.h:175
size_t(* fiftyoneDegreesPoolResourceSize)(void *state)
Used to determine the additional size beyond the pointer used for each resource added to the pool.
Definition pool.h:168
void *(* fiftyoneDegreesPoolResourceCreate)(fiftyoneDegreesPool *pool, void *state, fiftyoneDegreesException *exception)
Used to create a new resource for use in the pool.
Definition pool.h:156
void fiftyoneDegreesPoolReset(fiftyoneDegreesPool *pool)
Resets the pool without releasing any resources.
fiftyoneDegreesPool * fiftyoneDegreesPoolInit(fiftyoneDegreesPool *pool, uint16_t concurrency, void *state, fiftyoneDegreesPoolResourceCreate resourceCreate, fiftyoneDegreesPoolResourceFree resourceFree, fiftyoneDegreesException *exception)
Initialises a pool data structure to support the number of concurrent requests that can be made to th...
Structure used to represent a 51Degrees exception and passed into methods that might generate excepti...
Definition exceptions.h:115
Pool of resources stored as items in a stack.
Definition pool.h:202
fiftyoneDegreesPoolItem * stack
Pointer to the memory used by the stack.
Definition pool.h:203
fiftyoneDegreesPoolResourceFree resourceFree
Frees a resource.
Definition pool.h:207
uint16_t count
Number of resource items that stack can hold.
Definition pool.h:206
fiftyoneDegreesPoolHead head
Head of the stack.
Definition pool.h:205
Pool item node in the stack of items.
Definition pool.h:182
void * resource
Pointer to the resource in the pool.
Definition pool.h:183
uint16_t next
The next item in the stack.
Definition pool.h:184
fiftyoneDegreesPool * pool
Reader the handle belongs to.
Definition pool.h:185
The head of the stack used for pop and push operations.
Definition pool.h:191
volatile long exchange
Number used in the compare exchange operation.
Definition pool.h:192
uint16_t aba
ABA value used to ensure proper operation.
Definition pool.h:195
uint16_t index
Index of the item in the linked list.
Definition pool.h:194