Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JLANG::JAllocator Class Reference

Memory management for small objects. More...

#include <JAllocator.hh>

Inheritance diagram for JLANG::JAllocator:
std::vector< JAllocatorBuffer >

Public Member Functions

 JAllocator (const std::size_t block_size, const JBlock_t number_of_blocks=std::numeric_limits< JBlock_t >::max())
 Constructor.
 
long long int getTotalRAM ()
 Get total used RAM.
 
long long int getFreeRAM ()
 Get total free RAM.
 
void * allocate ()
 Allocate memory.
 
void free (void *p)
 Deallocate memory.
 

Private Types

typedef JAllocatorBuffer::JBlock_t JBlock_t
 

Private Attributes

std::size_t BLOCK_SIZE
 
JBlock_t numberOfBlocks
 
JAllocatorBufferpAlloc
 
JAllocatorBufferpDealloc
 

Detailed Description

Memory management for small objects.

This object allocator consitutes a comprimise between speed and memory overhead.

Source code is taken from reference: A. Alexandrescu, Modern C++ Design, Addison Wesley.

Definition at line 116 of file JAllocator.hh.

Member Typedef Documentation

◆ JBlock_t

Constructor & Destructor Documentation

◆ JAllocator()

JLANG::JAllocator::JAllocator ( const std::size_t block_size,
const JBlock_t number_of_blocks = std::numeric_limits<JBlock_t>::max() )
inline

Constructor.

Parameters
block_sizenumber of bytes
number_of_blocksnumber of blocks

Definition at line 136 of file JAllocator.hh.

137 :
139 BLOCK_SIZE (block_size),
140 numberOfBlocks(number_of_blocks),
141 pAlloc (NULL),
142 pDealloc(NULL)
143 {}
JAllocatorBuffer * pDealloc
std::size_t BLOCK_SIZE
JBlock_t numberOfBlocks
JAllocatorBuffer * pAlloc

Member Function Documentation

◆ getTotalRAM()

long long int JLANG::JAllocator::getTotalRAM ( )
inline

Get total used RAM.

Returns
number of bytes

Definition at line 151 of file JAllocator.hh.

152 {
153 return this->size() * BLOCK_SIZE * numberOfBlocks;
154 }

◆ getFreeRAM()

long long int JLANG::JAllocator::getFreeRAM ( )
inline

Get total free RAM.

Returns
number of bytes

Definition at line 162 of file JAllocator.hh.

163 {
164 long long int n = 0;
165
166 for (const_iterator i = this->begin(); i != this->end(); ++i) {
167 n += i->numberOfFreeBlocks;
168 }
169
170 return n * BLOCK_SIZE;
171 }
const int n
Definition JPolint.hh:791

◆ allocate()

void * JLANG::JAllocator::allocate ( )
inline

Allocate memory.

Returns
pointer to avaible memory

Definition at line 179 of file JAllocator.hh.

180 {
181 if (pAlloc == NULL || pAlloc->numberOfFreeBlocks == 0) {
182
183 for (iterator i = this->begin(); ; ++i) {
184
185 if (i == this->end()) {
186
187 this->push_back(JAllocatorBuffer(BLOCK_SIZE, numberOfBlocks));
188
189 pAlloc = &this->back();
191 break;
192 }
193
194 if (i->numberOfFreeBlocks != 0) {
195
196 pAlloc = &(*i);
197 break;
198 }
199 }
200
201 if (pAlloc == NULL) {
202 THROW(JException, "JAllocator::allocate() no buffer available.");
203 }
204
205 if (pAlloc->numberOfFreeBlocks == 0) {
206 THROW(JException, "JAllocator::allocate() no buffer available.");
207 }
208 }
209
210 return pAlloc->allocate(BLOCK_SIZE);
211 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
void * allocate(const std::size_t block_size)
Allocate memory.
Definition JAllocator.hh:65

◆ free()

void JLANG::JAllocator::free ( void * p)
inline

Deallocate memory.

Parameters
ppointer to memory to be freed

Definition at line 219 of file JAllocator.hh.

220 {
221 if (this->size() != 1) {
222
223 if (p < pDealloc->buffer ||
225
226 for (std::vector<JAllocatorBuffer>::iterator i = this->begin(); i != this->end(); ++i) {
227
228 if (p >= i->buffer &&
229 p < i->buffer + numberOfBlocks * BLOCK_SIZE) {
230 pDealloc = &(*i);
231 break;
232 }
233 }
234 }
235 }
236
237
239
240
241 if (this->size() != 1 && pDealloc->numberOfFreeBlocks == numberOfBlocks) {
242
244
245 for ( ; &(*i) != pDealloc && i->numberOfFreeBlocks == numberOfBlocks; ++i) {}
246
247 if (&(*i) != pDealloc) {
248
249 std::swap(*pDealloc, *i);
250
251 if (i != this->rend()) {
252
253 delete [] this->rbegin()->buffer;
254
255 this->pop_back();
256 }
257 }
258 }
259
261 }
unsigned char * buffer
void free(void *p, const std::size_t block_size)
Deallocate memory.
Definition JAllocator.hh:83

Member Data Documentation

◆ BLOCK_SIZE

std::size_t JLANG::JAllocator::BLOCK_SIZE
private

Definition at line 123 of file JAllocator.hh.

◆ numberOfBlocks

JBlock_t JLANG::JAllocator::numberOfBlocks
private

Definition at line 124 of file JAllocator.hh.

◆ pAlloc

JAllocatorBuffer* JLANG::JAllocator::pAlloc
private

Definition at line 126 of file JAllocator.hh.

◆ pDealloc

JAllocatorBuffer* JLANG::JAllocator::pDealloc
private

Definition at line 127 of file JAllocator.hh.


The documentation for this class was generated from the following file: