aGrUM 3.1.1
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-2026 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-2026 *
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
96 // clang-format on
97
98 struct _Chunk_ {
99 // ============================================================================
101 // ============================================================================
102 void _init_(const std::size_t& blockSize, const unsigned char& numBlocks);
103
104 // ============================================================================
106 // ============================================================================
107 void* _allocate_(const std::size_t& blockSize);
108
109 // ============================================================================
111 // ============================================================================
112 void _deallocat_(void* p, const std::size_t& blockSize);
113
114 // ============================================================================
116 // ============================================================================
117 void _release_();
118
119 // ============================================================================
121 // ============================================================================
122 unsigned char* _pData_;
123
124 // ============================================================================
126 // ============================================================================
128
129 // ============================================================================
131 // ============================================================================
132 unsigned char _blocksAvailable_;
133 };
134
135 public:
136 // ############################################################################
138 // ############################################################################
140
141 // ============================================================================
149 // ============================================================================
150 FixedAllocator(const std::size_t& blockSize, const unsigned char& numBlocks = UCHAR_MAX);
151
152 // ============================================================================
154 // ============================================================================
156
158
159 // ############################################################################
161 // ############################################################################
163
164 // ============================================================================
166 // ============================================================================
167 void* allocate();
168
169 // ============================================================================
171 // ============================================================================
172 void deallocate(void* pDeallocatedBlock);
173
175
176 // ============================================================================
178 // ============================================================================
179 const size_t& objectSize();
180
181 private:
182 // ============================================================================
184 // ============================================================================
185 std::size_t _blockSize_;
186
187 // ============================================================================
189 // ============================================================================
190 unsigned char _numBlocks_;
191
192 // ============================================================================
194 // ============================================================================
195 using _Chunks_ = std::vector< _Chunk_ >;
197
198 // ============================================================================
200 // ============================================================================
201 _Chunks_::iterator _allocChunk_;
202
203 // ============================================================================
205 // ============================================================================
206 _Chunks_::iterator _deallocChunk_;
207 };
208
209} // namespace gum
210
211#ifndef GUM_NO_INLINE
213#endif
214
215#endif // FIXEDALLOCATOR_H
FixedAllocator(const std::size_t &blockSize, const unsigned char &numBlocks=UCHAR_MAX)
Constructor.
_Chunks_::iterator _deallocChunk_
Last Chunk used for a deallocation.
void deallocate(void *pDeallocatedBlock)
Deallocates a block.
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.
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 * _allocate_(const std::size_t &blockSize)
Allocates a block of memory.
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.