aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
fixedAllocator.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of gum::FixedAllocator
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  *
29  */
30 #ifndef GUM_FIXED_ALLOCATOR_H
31 #define GUM_FIXED_ALLOCATOR_H
32 
33 // ============================================================================
34 #include <mutex>
35 #include <thread>
36 // ============================================================================
37 #include <agrum/agrum.h>
38 // ============================================================================
39 
40 namespace gum {
41  /**
42  * @class FixedAllocator
43  * @headerfile fixedAllocator.h <agrum/tools/core/fixedAllocator.h>
44  *
45  * @brief Allocates objects of one given size
46  *
47  * Fixed allocator knows how to allocate and deallocate blocks of fixed size
48  * but is not limited to a chunck size. Its capacity is limited only by the
49  * available memory.
50  * To achieve this, FixedAllocator aggregates a vector of Chunk objects.
51  * Whenever an allocation request occurs, FixedAllocators looks for a Chunk
52  * that can accomodate the request.
53  * If all Chunks are filled up, FixedAllocator appends a new Chunk.
54  *
55  * @ingroup core
56  */
57 
59  // clang-format off
60  /**
61  * @struct Chunk__ fixedAllocator.h <agrum/tools/core/smallobjectallocator/fixedAllocator.h>
62  *
63  * @brief Allocates objects of one given size. Has a fixed limit of
64  * allocation
65  *
66  * Each object of type Chunk contains and manages a chunk of memory
67  * containing a
68  * amount of blocks. At construction time, you configure the block size and
69  * the
70  * number of blocks.
71  * A Chunk contains logic that allows you to allocate and deallocate memory
72  * blocks
73  * from that chunk of memory. When there are no more blocks available in the
74  * chunk,
75  * the allocation function returns zero.
76  *
77  * @ingroup core
78  */
79  // clang-format on
80  struct Chunk__ {
81  // ============================================================================
82  /// Initializes a Chunk object
83  // ============================================================================
84  void init__(const std::size_t& blockSize, const unsigned char& numBlocks);
85 
86  // ============================================================================
87  /// Allocates a block of memory
88  // ============================================================================
89  void* allocate__(const std::size_t& blockSize);
90 
91  // ============================================================================
92  /// Deallocates a block of memory
93  // ============================================================================
94  void deallocat__(void* p, const std::size_t& blockSize);
95 
96  // ============================================================================
97  /// Releases the allocated memory
98  // ============================================================================
99  void release__();
100 
101  // ============================================================================
102  /// Pointer to the managed memory itself
103  // ============================================================================
104  unsigned char* pData__;
105 
106  // ============================================================================
107  /// Holds the index of the first block available in this chunck
108  // ============================================================================
109  unsigned char firstAvailableBlock__;
110 
111  // ============================================================================
112  /// Number of blocks available in this chunck
113  // ============================================================================
114  unsigned char blocksAvailable__;
115  };
116 
117  public:
118  // ############################################################################
119  /// @name Constructors / Destructors
120  // ############################################################################
121  /// @{
122 
123  // ============================================================================
124  /**
125  * Constructor.
126  * @param blockSize is the size of an allocated block.
127  * @param numBlocks is the number of block allocated per chunk
128  * numBlock * blockSize is the size that a chunk allocates directly
129  * when it is created
130  */
131  // ============================================================================
132  FixedAllocator(const std::size_t& blockSize,
133  const unsigned char& numBlocks = UCHAR_MAX);
134 
135  // ============================================================================
136  /// Destructor.
137  // ============================================================================
138  ~FixedAllocator();
139 
140  /// @}
141 
142  // ############################################################################
143  /// @name Allocator / Deallocator
144  // ############################################################################
145  /// @{
146 
147  // ============================================================================
148  /// Allocates a block
149  // ============================================================================
150  void* allocate();
151 
152  // ============================================================================
153  /// Deallocates a block
154  // ============================================================================
155  void deallocate(void* pDeallocatedBlock);
156 
157  /// @}
158 
159  // ============================================================================
160  /// Returns the size of block allocated by this FixedAllocator
161  // ============================================================================
162  const size_t& objectSize() { return blockSize__; }
163 
164  private:
165  // ============================================================================
166  /// Size of a memory block allocated
167  // ============================================================================
168  std::size_t blockSize__;
169 
170  // ============================================================================
171  /// The maximum number of blocks a chunk can allocate
172  // ============================================================================
173  unsigned char numBlocks__;
174 
175  // ============================================================================
176  /// Vector of Chunk__ objects
177  // ============================================================================
178  typedef std::vector< Chunk__ > Chunks__;
180 
181  // ============================================================================
182  /// Last Chunk used for an allocation
183  // ============================================================================
185 
186  // ============================================================================
187  /// Last Chunk used for a deallocation
188  // ============================================================================
190  };
191 
192 } // namespace gum
193 
194 #ifndef GUM_NO_INLINE
195 # include <agrum/tools/core/smallobjectallocator/fixedAllocator_inl.h>
196 #endif
197 
198 #endif // FIXEDALLOCATOR_H
void * allocate()
Allocates a block.
void deallocate(void *pDeallocatedBlock)
Deallocates a block.
unsigned char firstAvailableBlock__
Holds the index of the first block available in this chunck.
FixedAllocator(const std::size_t &blockSize, const unsigned char &numBlocks=UCHAR_MAX)
Constructor.
void init__(const std::size_t &blockSize, const unsigned char &numBlocks)
Initializes a Chunk object.
void deallocat__(void *p, const std::size_t &blockSize)
Deallocates a block of memory.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
unsigned char blocksAvailable__
Number of blocks available in this chunck.
Allocates objects of one given size.
void release__()
Releases the allocated memory.
Chunks__::iterator deallocChunk__
Last Chunk used for a deallocation.
unsigned char * pData__
Pointer to the managed memory itself.
void * allocate__(const std::size_t &blockSize)
Allocates a block of memory.
Allocates objects of one given size.
const size_t & objectSize()
Returns the size of block allocated by this FixedAllocator.
std::size_t blockSize__
Size of a memory block allocated.
~FixedAllocator()
Destructor.
std::vector< Chunk__ > Chunks__
Vector of Chunk__ objects.
Chunks__::iterator allocChunk__
Last Chunk used for an allocation.
unsigned char numBlocks__
The maximum number of blocks a chunk can allocate.