libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_pool_allocator.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblPoolAllocator arena-backed, pool-based allocator
3 * \ingroup allocators
4 * \copydoc GblPoolAllocator
5 *
6 *
7 *
8 * \author Falco Girgis
9 */
10
11#ifndef GIMBAL_POOL_ALLOCATOR_H
12#define GIMBAL_POOL_ALLOCATOR_H
13
15
16#define GBL_SELF_TYPE GblPoolAllocator
17
19
20/*! \brief Pool allocator for ultra-fast fixed-size allocations
21 *
22 * GblPoolAllocator is a custsom allocator providing
23 * extremely efficient dynamic allocations and deallocations
24 * for data of a fixed size. Allocations are done in bulk
25 * as single pages which are then split up into individual
26 * allocation entries. Since the pool allocator is built
27 * upon an arena allocator, when the initial page chunk
28 * overflows, the allocator will gracefully allocate another,
29 * so it's still able to dynamically accomodate arbitrary
30 * numbers of allocations.
31 *
32 * The pool allocator constructs and maintains a free list
33 * of deleted entries (embedded within the unused allocations
34 * themselves), so a previously freed entry can be immediately
35 * recycled when a new allocation is requested.
36 *
37 * The allocator also supports enforcing custom alignment
38 * constraints on each requested allocation.
39 * \ingroup allocators
40 * \sa GblArenaAllocator
41 */
42typedef struct GblPoolAllocator {
43 GblArenaAllocator arena; //!< Arena allocator instance providing backing allocations
44 GblLinkedListNode freeList; //!< Link list of deleted entries for efficient recycling
45 size_t entrySize; //!< Base struct size of each entry
46 size_t entryAlign; //!< Alignment requirement for each entry
47 size_t activeEntries; //!< Number of allocated, used entries
48} GblPoolAllocator;
49
50// ===== Public methods =====
51GBL_EXPORT GBL_RESULT GblPoolAllocator_construct (GBL_SELF,
52 size_t entrySize,
53 size_t entriesPerPage,
54 size_t entryAlign,
55 GblArenaAllocatorPage* pInitialPage,
56 GblContext* pCtx) GBL_NOEXCEPT;
57
58GBL_EXPORT GBL_RESULT GblPoolAllocator_destruct (GBL_SELF) GBL_NOEXCEPT;
59
60GBL_EXPORT size_t GblPoolAllocator_freeListSize (GBL_CSELF) GBL_NOEXCEPT;
61
62GBL_EXPORT void* GblPoolAllocator_new (GBL_SELF) GBL_NOEXCEPT;
63GBL_EXPORT GBL_RESULT GblPoolAllocator_delete (GBL_SELF, void* pEntry) GBL_NOEXCEPT;
64
65// ===== Macro Overloads =====
66#define GblPoolAllocator_construct(...) GblPoolAllocator_constructDefault_(__VA_ARGS__)
67
68// ===== IMPL =====
69
70///\cond
71#define GblPoolAllocator_constructDefault_(...)
72 GblPoolAllocator_constructDefault__(__VA_ARGS__, 0, GBL_NULL, GBL_NULL)
73#define GblPoolAllocator_constructDefault__(self, size, perPage, align, initial, ctx, ...)
74 (GblPoolAllocator_construct)(self, size, perPage, align, initial, ctx)
75///\endcond
77
78#undef GBL_SELF_TYPE
79
80#endif // GIMBAL_POOL_ALLOCATOR_H
#define GBL_NULL
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_EXPORT
#define GblPoolAllocator_construct(...)
Pool allocator for ultra-fast fixed-size allocations.
GblLinkedListNode freeList
Link list of deleted entries for efficient recycling.
size_t activeEntries
Number of allocated, used entries.
size_t entryAlign
Alignment requirement for each entry.
size_t entrySize
Base struct size of each entry.
GblArenaAllocator arena
Arena allocator instance providing backing allocations.