aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
fixedAllocator.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
50#ifndef GUM_FIXED_ALLOCATOR_H
51#define GUM_FIXED_ALLOCATOR_H
52
53// ============================================================================
54#include <mutex>
55#include <thread>
56// ============================================================================
57#include <agrum/agrum.h>
58
59// ============================================================================
60
61namespace gum {
78
80 // clang-format off
100 // clang-format on
101
102 struct _Chunk_ {
103 // ============================================================================
105 // ============================================================================
106 void _init_(const std::size_t& blockSize, const unsigned char& numBlocks);
107
108 // ============================================================================
110 // ============================================================================
111 void* _allocate_(const std::size_t& blockSize);
112
113 // ============================================================================
115 // ============================================================================
116 void _deallocat_(void* p, const std::size_t& blockSize);
117
118 // ============================================================================
120 // ============================================================================
121 void _release_();
122
123 // ============================================================================
125 // ============================================================================
126 unsigned char* _pData_;
127
128 // ============================================================================
130 // ============================================================================
132
133 // ============================================================================
135 // ============================================================================
136 unsigned char _blocksAvailable_;
137 };
138
139 public:
140 // ############################################################################
142 // ############################################################################
144
145 // ============================================================================
153 // ============================================================================
154 FixedAllocator(const std::size_t& blockSize, const unsigned char& numBlocks = UCHAR_MAX);
155
156 // ============================================================================
158 // ============================================================================
160
162
163 // ############################################################################
165 // ############################################################################
167
168 // ============================================================================
170 // ============================================================================
171 void* allocate();
172
173 // ============================================================================
175 // ============================================================================
176 void deallocate(void* pDeallocatedBlock);
177
179
180 // ============================================================================
182 // ============================================================================
183 const size_t& objectSize() { return _blockSize_; }
184
185 private:
186 // ============================================================================
188 // ============================================================================
189 std::size_t _blockSize_;
190
191 // ============================================================================
193 // ============================================================================
194 unsigned char _numBlocks_;
195
196 // ============================================================================
198 // ============================================================================
199 using _Chunks_ = std::vector< _Chunk_ >;
201
202 // ============================================================================
204 // ============================================================================
205 _Chunks_::iterator _allocChunk_;
206
207 // ============================================================================
209 // ============================================================================
210 _Chunks_::iterator _deallocChunk_;
211 };
212
213} // namespace gum
214
215#ifndef GUM_NO_INLINE
217#endif
218
219#endif // FIXEDALLOCATOR_H
void deallocate(void *pDeallocatedBlock)
Deallocates a block.
FixedAllocator(const std::size_t &blockSize, const unsigned char &numBlocks=UCHAR_MAX)
Constructor.
_Chunks_::iterator _deallocChunk_
Last Chunk used for a deallocation.
unsigned char _numBlocks_
The maximum number of blocks a chunk can allocate.
std::size_t _blockSize_
Size of a memory block allocated.
std::vector< _Chunk_ > _Chunks_
Vector of Chunk objects.
_Chunks_::iterator _allocChunk_
Last Chunk used for an allocation.
const size_t & objectSize()
Returns the size of block allocated by this FixedAllocator.
void * allocate()
Allocates a block.
Inlines of gum::FixedAllocator.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Allocates objects of one given size.
void * _allocate_(const std::size_t &blockSize)
Allocates a block of memory.
unsigned char _firstAvailableBlock_
Holds the index of the first block available in this chunck.
void _deallocat_(void *p, const std::size_t &blockSize)
Deallocates a block of memory.
unsigned char * _pData_
Pointer to the managed memory itself.
void _init_(const std::size_t &blockSize, const unsigned char &numBlocks)
Initializes a Chunk object.
void _release_()
Releases the allocated memory.
unsigned char _blocksAvailable_
Number of blocks available in this chunck.