aGrUM  0.14.2
fixedAllocator.h
Go to the documentation of this file.
1 
2 /***************************************************************************
3  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
4  * {prenom.nom}_at_lip6.fr *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program 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 General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20  ***************************************************************************/
28 #ifndef GUM_FIXED_ALLOCATOR_H
29 #define GUM_FIXED_ALLOCATOR_H
30 
31 // ============================================================================
32 #include <mutex>
33 #include <thread>
34 // ============================================================================
35 #include <agrum/agrum.h>
36 // ============================================================================
37 
38 namespace gum {
57  // clang-format off
77  // clang-format on
78  struct __Chunk {
79  // ============================================================================
81  // ============================================================================
82  void __init(const std::size_t& blockSize, const unsigned char& numBlocks);
83 
84  // ============================================================================
86  // ============================================================================
87  void* __allocate(const std::size_t& blockSize);
88 
89  // ============================================================================
91  // ============================================================================
92  void __deallocat(void* p, const std::size_t& blockSize);
93 
94  // ============================================================================
96  // ============================================================================
97  void __release();
98 
99  // ============================================================================
101  // ============================================================================
102  unsigned char* __pData;
103 
104  // ============================================================================
106  // ============================================================================
107  unsigned char __firstAvailableBlock;
108 
109  // ============================================================================
111  // ============================================================================
112  unsigned char __blocksAvailable;
113  };
114 
115  public:
116  // ############################################################################
118  // ############################################################################
120 
121  // ============================================================================
129  // ============================================================================
130  FixedAllocator(const std::size_t& blockSize,
131  const unsigned char& numBlocks = UCHAR_MAX);
132 
133  // ============================================================================
135  // ============================================================================
136  ~FixedAllocator();
137 
139 
140  // ############################################################################
142  // ############################################################################
144 
145  // ============================================================================
147  // ============================================================================
148  void* allocate();
149 
150  // ============================================================================
152  // ============================================================================
153  void deallocate(void* pDeallocatedBlock);
154 
156 
157  // ============================================================================
159  // ============================================================================
160  const size_t& objectSize() { return __blockSize; }
161 
162  private:
163  // ============================================================================
165  // ============================================================================
166  std::size_t __blockSize;
167 
168  // ============================================================================
170  // ============================================================================
171  unsigned char __numBlocks;
172 
173  // ============================================================================
175  // ============================================================================
176  typedef std::vector< __Chunk > __Chunks;
177  __Chunks __chunks;
178 
179  // ============================================================================
181  // ============================================================================
182  __Chunks::iterator __allocChunk;
183 
184  // ============================================================================
186  // ============================================================================
187  __Chunks::iterator __deallocChunk;
188  };
189 
190 } // namespace gum
191 
192 #ifndef GUM_NO_INLINE
194 #endif
195 
196 #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.
Inlines of gum::FixedAllocator.
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.
gum is the global namespace for all aGrUM entities
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.