Jpp  19.0.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 24 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 67 of file JRAM.hh.

68  :
70  BLOCK_SIZE (block_size),
71  numberOfBlocks(number_of_blocks)
72  {
73  addMemory();
74  }
const std::size_t BLOCK_SIZE
Definition: JRAM.hh:29
void addMemory()
Add memory.
Definition: JRAM.hh:37
const std::size_t numberOfBlocks
Definition: JRAM.hh:30
JLANG::JRAM::~JRAM ( )
inline

Destructor.

Definition at line 80 of file JRAM.hh.

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

Member Function Documentation

void JLANG::JRAM::addMemory ( )
inlineprivate

Add memory.

Definition at line 37 of file JRAM.hh.

38  {
39  unsigned char* buffer = new unsigned char[BLOCK_SIZE * numberOfBlocks];
40 
41  if (buffer == NULL) {
42  THROW(JException, "JRAM::allocate(): not enough space in memory.");
43  }
44 
45  memory.push_back(buffer);
46 
47  const std::size_t size = this->size();
48 
49  this->resize(size + numberOfBlocks);
50 
51  iterator q = this->begin();
52 
53  std::advance(q, size);
54 
55  for ( ; q != this->end(); buffer += BLOCK_SIZE, ++q) {
56  *q = buffer;
57  }
58  }
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:29
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:30
std::vector< unsigned char * > memory
Definition: JRAM.hh:31
long long int JLANG::JRAM::getTotalRAM ( )
inline

Get total used RAM.

Returns
number of bytes

Definition at line 93 of file JRAM.hh.

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

Get total free RAM.

Returns
number of bytes

Definition at line 104 of file JRAM.hh.

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

Allocate memory.

Returns
pointer to avaible memory

Definition at line 115 of file JRAM.hh.

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

Deallocate memory.

Parameters
ppointer to memory to be freed

Definition at line 132 of file JRAM.hh.

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

Run garbage collector.

Definition at line 141 of file JRAM.hh.

142  {
143  using namespace std;
144 
145  sort(this->begin(), this->end());
146 
148 
149  for (vector<unsigned char*>::iterator i = memory.begin(); i != __end; ) {
150 
151  iterator p = lower_bound(this->begin(), this->end(), *i);
152  iterator q = p + numberOfBlocks - 1;
153 
154  if (*p == *i &&
155  distance(q, this->end()) > 0 &&
156  ((unsigned char*) (*q) - (unsigned char*) *p) == (int) (BLOCK_SIZE * (numberOfBlocks - 1))) {
157 
158  iter_swap(i, --__end);
159 
160  this->erase(p,++q);
161 
162  } else {
163  ++i;
164  }
165  }
166 
167  for (std::vector<unsigned char*>::iterator i = __end; i != memory.end(); ++i) {
168  delete [] (*i);
169  }
170 
171  memory.erase(__end, memory.end());
172  }
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:29
const std::size_t numberOfBlocks
Definition: JRAM.hh:30
std::vector< unsigned char * > memory
Definition: JRAM.hh:31
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 29 of file JRAM.hh.

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

Definition at line 30 of file JRAM.hh.

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

Definition at line 31 of file JRAM.hh.


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