aGrUM  0.16.0
fixedAllocator.h
Go to the documentation of this file.
1 
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 {
59  // clang-format off
79  // clang-format on
80  struct __Chunk {
81  // ============================================================================
83  // ============================================================================
84  void __init(const std::size_t& blockSize, const unsigned char& numBlocks);
85 
86  // ============================================================================
88  // ============================================================================
89  void* __allocate(const std::size_t& blockSize);
90 
91  // ============================================================================
93  // ============================================================================
94  void __deallocat(void* p, const std::size_t& blockSize);
95 
96  // ============================================================================
98  // ============================================================================
99  void __release();
100 
101  // ============================================================================
103  // ============================================================================
104  unsigned char* __pData;
105 
106  // ============================================================================
108  // ============================================================================
109  unsigned char __firstAvailableBlock;
110 
111  // ============================================================================
113  // ============================================================================
114  unsigned char __blocksAvailable;
115  };
116 
117  public:
118  // ############################################################################
120  // ############################################################################
122 
123  // ============================================================================
131  // ============================================================================
132  FixedAllocator(const std::size_t& blockSize,
133  const unsigned char& numBlocks = UCHAR_MAX);
134 
135  // ============================================================================
137  // ============================================================================
138  ~FixedAllocator();
139 
141 
142  // ############################################################################
144  // ############################################################################
146 
147  // ============================================================================
149  // ============================================================================
150  void* allocate();
151 
152  // ============================================================================
154  // ============================================================================
155  void deallocate(void* pDeallocatedBlock);
156 
158 
159  // ============================================================================
161  // ============================================================================
162  const size_t& objectSize() { return __blockSize; }
163 
164  private:
165  // ============================================================================
167  // ============================================================================
168  std::size_t __blockSize;
169 
170  // ============================================================================
172  // ============================================================================
173  unsigned char __numBlocks;
174 
175  // ============================================================================
177  // ============================================================================
178  typedef std::vector< __Chunk > __Chunks;
179  __Chunks __chunks;
180 
181  // ============================================================================
183  // ============================================================================
184  __Chunks::iterator __allocChunk;
185 
186  // ============================================================================
188  // ============================================================================
189  __Chunks::iterator __deallocChunk;
190  };
191 
192 } // namespace gum
193 
194 #ifndef GUM_NO_INLINE
196 #endif
197 
198 #endif // FIXEDALLOCATOR_H
void * allocate()
Allocates a block.
void deallocate(void *pDeallocatedBlock)
Deallocates a block.
unsigned char __blocksAvailable
Number of blocks available in this chunck.
FixedAllocator(const std::size_t &blockSize, const unsigned char &numBlocks=UCHAR_MAX)
Constructor.
__Chunks::iterator __allocChunk
Last Chunk used for an allocation.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Allocates objects of one given size.
void __deallocat(void *p, const std::size_t &blockSize)
Deallocates a block of memory.
std::size_t __blockSize
Size of a memory block allocated.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
unsigned char __numBlocks
The maximum number of blocks a chunk can allocate.
void * __allocate(const std::size_t &blockSize)
Allocates a block of memory.
unsigned char * __pData
Pointer to the managed memory itself.
__Chunks::iterator __deallocChunk
Last Chunk used for a deallocation.
unsigned char __firstAvailableBlock
Holds the index of the first block available in this chunck.
std::vector< __Chunk > __Chunks
Vector of __Chunk objects.
Allocates objects of one given size.
const size_t & objectSize()
Returns the size of block allocated by this FixedAllocator.
~FixedAllocator()
Destructor.
void __init(const std::size_t &blockSize, const unsigned char &numBlocks)
Initializes a Chunk object.
void __release()
Releases the allocated memory.