aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
fixedAllocator.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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, const unsigned char& numBlocks = UCHAR_MAX);
133 
134  // ============================================================================
135  /// Destructor.
136  // ============================================================================
137  ~FixedAllocator();
138 
139  /// @}
140 
141  // ############################################################################
142  /// @name Allocator / Deallocator
143  // ############################################################################
144  /// @{
145 
146  // ============================================================================
147  /// Allocates a block
148  // ============================================================================
149  void* allocate();
150 
151  // ============================================================================
152  /// Deallocates a block
153  // ============================================================================
154  void deallocate(void* pDeallocatedBlock);
155 
156  /// @}
157 
158  // ============================================================================
159  /// Returns the size of block allocated by this FixedAllocator
160  // ============================================================================
161  const size_t& objectSize() { return _blockSize_; }
162 
163  private:
164  // ============================================================================
165  /// Size of a memory block allocated
166  // ============================================================================
167  std::size_t _blockSize_;
168 
169  // ============================================================================
170  /// The maximum number of blocks a chunk can allocate
171  // ============================================================================
172  unsigned char _numBlocks_;
173 
174  // ============================================================================
175  /// Vector of _Chunk_ objects
176  // ============================================================================
177  typedef std::vector< _Chunk_ > _Chunks_;
179 
180  // ============================================================================
181  /// Last Chunk used for an allocation
182  // ============================================================================
184 
185  // ============================================================================
186  /// Last Chunk used for a deallocation
187  // ============================================================================
189  };
190 
191 } // namespace gum
192 
193 #ifndef GUM_NO_INLINE
194 # include <agrum/tools/core/smallobjectallocator/fixedAllocator_inl.h>
195 #endif
196 
197 #endif // FIXEDALLOCATOR_H
void * allocate()
Allocates a block.
void deallocate(void *pDeallocatedBlock)
Deallocates a block.
FixedAllocator(const std::size_t &blockSize, const unsigned char &numBlocks=UCHAR_MAX)
Constructor.
unsigned char _firstAvailableBlock_
Holds the index of the first block available in this chunck.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
unsigned char * _pData_
Pointer to the managed memory itself.
std::vector< _Chunk_ > _Chunks_
Vector of Chunk objects.
unsigned char _blocksAvailable_
Number of blocks available in this chunck.
_Chunks_::iterator _allocChunk_
Last Chunk used for an allocation.
void _release_()
Releases the allocated memory.
Allocates objects of one given size.
void * _allocate_(const std::size_t &blockSize)
Allocates a block of memory.
unsigned char _numBlocks_
The maximum number of blocks a chunk can allocate.
void _init_(const std::size_t &blockSize, const unsigned char &numBlocks)
Initializes a Chunk object.
Allocates objects of one given size.
const size_t & objectSize()
Returns the size of block allocated by this FixedAllocator.
~FixedAllocator()
Destructor.
_Chunks_::iterator _deallocChunk_
Last Chunk used for a deallocation.
void _deallocat_(void *p, const std::size_t &blockSize)
Deallocates a block of memory.
std::size_t _blockSize_
Size of a memory block allocated.