Jpp  18.6.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Private Types | Private Attributes | List of all members
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. More...
 
long long int getTotalRAM ()
 Get total used RAM. More...
 
long long int getFreeRAM ()
 Get total free RAM. More...
 
void * allocate ()
 Allocate memory. More...
 
void free (void *p)
 Deallocate memory. More...
 

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

Definition at line 121 of file JAllocator.hh.

Constructor & Destructor Documentation

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
Definition: JAllocator.hh:127
JAllocatorBuffer * pAlloc
Definition: JAllocator.hh:126
JBlock_t numberOfBlocks
Definition: JAllocator.hh:124
std::size_t BLOCK_SIZE
Definition: JAllocator.hh:123

Member Function Documentation

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  }
JBlock_t numberOfBlocks
Definition: JAllocator.hh:124
std::size_t BLOCK_SIZE
Definition: JAllocator.hh:123
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:786
std::size_t BLOCK_SIZE
Definition: JAllocator.hh:123
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();
190  pDealloc = pAlloc;
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  }
General exception.
Definition: JException.hh:24
JAllocatorBuffer * pDealloc
Definition: JAllocator.hh:127
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
JAllocatorBuffer * pAlloc
Definition: JAllocator.hh:126
Low-level memory management.
Definition: JAllocator.hh:26
JBlock_t numberOfBlocks
Definition: JAllocator.hh:124
void * allocate(const std::size_t block_size)
Allocate memory.
Definition: JAllocator.hh:65
std::size_t BLOCK_SIZE
Definition: JAllocator.hh:123
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 
238  pDealloc->free(p, BLOCK_SIZE);
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 
260  pAlloc = pDealloc;
261  }
JAllocatorBuffer * pDealloc
Definition: JAllocator.hh:127
JAllocatorBuffer * pAlloc
Definition: JAllocator.hh:126
void free(void *p, const std::size_t block_size)
Deallocate memory.
Definition: JAllocator.hh:83
JBlock_t numberOfBlocks
Definition: JAllocator.hh:124
unsigned char * buffer
Definition: JAllocator.hh:103
std::size_t BLOCK_SIZE
Definition: JAllocator.hh:123

Member Data Documentation

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

Definition at line 123 of file JAllocator.hh.

JBlock_t JLANG::JAllocator::numberOfBlocks
private

Definition at line 124 of file JAllocator.hh.

JAllocatorBuffer* JLANG::JAllocator::pAlloc
private

Definition at line 126 of file JAllocator.hh.

JAllocatorBuffer* JLANG::JAllocator::pDealloc
private

Definition at line 127 of file JAllocator.hh.


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