Jpp  18.4.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
JLANG::JRAM Class Reference

Memory management for small objects. More...

#include <JRAM.hh>

Inheritance diagram for JLANG::JRAM:
std::vector< void * >

Public Member Functions

 JRAM (const std::size_t block_size, const std::size_t number_of_blocks=65536)
 Constructor. More...
 
 ~JRAM ()
 Destructor. 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...
 
void gc ()
 Run garbage collector. More...
 

Private Member Functions

void addMemory ()
 Add memory. More...
 
 JRAM (const JRAM &)
 
 JRAM (JRAM &&)
 
JRAMoperator= (const JRAM &)
 
JRAMoperator= (JRAM &&)
 

Private Attributes

const std::size_t BLOCK_SIZE
 
const std::size_t numberOfBlocks
 
std::vector< unsigned char * > memory
 

Detailed Description

Memory management for small objects.

This object allocator is optimised for speed at the cost of some memory overhead.

Definition at line 23 of file JRAM.hh.

Constructor & Destructor Documentation

JLANG::JRAM::JRAM ( const std::size_t  block_size,
const std::size_t  number_of_blocks = 65536 
)
inline

Constructor.

Parameters
block_sizenumber of bytes
number_of_blocksnumber of blocks

Definition at line 66 of file JRAM.hh.

67  :
69  BLOCK_SIZE (block_size),
70  numberOfBlocks(number_of_blocks)
71  {
72  addMemory();
73  }
const std::size_t BLOCK_SIZE
Definition: JRAM.hh:28
void addMemory()
Add memory.
Definition: JRAM.hh:36
const std::size_t numberOfBlocks
Definition: JRAM.hh:29
JLANG::JRAM::~JRAM ( )
inline

Destructor.

Definition at line 79 of file JRAM.hh.

80  {
81  for (std::vector<unsigned char*>::iterator i = memory.begin(); i != memory.end(); ++i) {
82  delete [] (*i);
83  }
84  }
std::vector< unsigned char * > memory
Definition: JRAM.hh:30
JLANG::JRAM::JRAM ( const JRAM )
private
JLANG::JRAM::JRAM ( JRAM &&  )
private

Member Function Documentation

void JLANG::JRAM::addMemory ( )
inlineprivate

Add memory.

Definition at line 36 of file JRAM.hh.

37  {
38  unsigned char* buffer = new unsigned char[BLOCK_SIZE * numberOfBlocks];
39 
40  if (buffer == NULL) {
41  THROW(JException, "JRAM::allocate(): not enough space in memory.");
42  }
43 
44  memory.push_back(buffer);
45 
46  const std::size_t size = this->size();
47 
48  this->resize(size + numberOfBlocks);
49 
50  iterator q = this->begin();
51 
52  std::advance(q, size);
53 
54  for ( ; q != this->end(); buffer += BLOCK_SIZE, ++q) {
55  *q = buffer;
56  }
57  }
General exception.
Definition: JException.hh:24
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
const std::size_t BLOCK_SIZE
Definition: JRAM.hh:28
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
const std::size_t numberOfBlocks
Definition: JRAM.hh:29
std::vector< unsigned char * > memory
Definition: JRAM.hh:30
long long int JLANG::JRAM::getTotalRAM ( )
inline

Get total used RAM.

Returns
number of bytes

Definition at line 92 of file JRAM.hh.

93  {
94  return this->size() * sizeof(void*) + memory.size() * BLOCK_SIZE * numberOfBlocks;
95  }
const std::size_t BLOCK_SIZE
Definition: JRAM.hh:28
const std::size_t numberOfBlocks
Definition: JRAM.hh:29
std::vector< unsigned char * > memory
Definition: JRAM.hh:30
long long int JLANG::JRAM::getFreeRAM ( )
inline

Get total free RAM.

Returns
number of bytes

Definition at line 103 of file JRAM.hh.

104  {
105  return this->size() * BLOCK_SIZE;
106  }
const std::size_t BLOCK_SIZE
Definition: JRAM.hh:28
void* JLANG::JRAM::allocate ( )
inline

Allocate memory.

Returns
pointer to avaible memory

Definition at line 114 of file JRAM.hh.

115  {
116  if (this->empty()) {
117  addMemory();
118  }
119 
120  this->pop_back();
121 
122  return *(this->end());
123  }
void addMemory()
Add memory.
Definition: JRAM.hh:36
void JLANG::JRAM::free ( void *  p)
inline

Deallocate memory.

Parameters
ppointer to memory to be freed

Definition at line 131 of file JRAM.hh.

132  {
133  this->push_back(p);
134  }
void JLANG::JRAM::gc ( )
inline

Run garbage collector.

Definition at line 140 of file JRAM.hh.

141  {
142  using namespace std;
143 
144  sort(this->begin(), this->end());
145 
147 
148  for (vector<unsigned char*>::iterator i = memory.begin(); i != __end; ) {
149 
150  iterator p = lower_bound(this->begin(), this->end(), *i);
151  iterator q = p + numberOfBlocks - 1;
152 
153  if (*p == *i &&
154  distance(q, this->end()) > 0 &&
155  ((unsigned char*) (*q) - (unsigned char*) *p) == (int) (BLOCK_SIZE * (numberOfBlocks - 1))) {
156 
157  iter_swap(i, --__end);
158 
159  this->erase(p,++q);
160 
161  } else {
162  ++i;
163  }
164  }
165 
166  for (std::vector<unsigned char*>::iterator i = __end; i != memory.end(); ++i) {
167  delete [] (*i);
168  }
169 
170  memory.erase(__end, memory.end());
171  }
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
const std::size_t BLOCK_SIZE
Definition: JRAM.hh:28
const std::size_t numberOfBlocks
Definition: JRAM.hh:29
std::vector< unsigned char * > memory
Definition: JRAM.hh:30
JRAM& JLANG::JRAM::operator= ( const JRAM )
private
JRAM& JLANG::JRAM::operator= ( JRAM &&  )
private

Member Data Documentation

const std::size_t JLANG::JRAM::BLOCK_SIZE
private

Definition at line 28 of file JRAM.hh.

const std::size_t JLANG::JRAM::numberOfBlocks
private

Definition at line 29 of file JRAM.hh.

std::vector<unsigned char*> JLANG::JRAM::memory
private

Definition at line 30 of file JRAM.hh.


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