Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
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.
 
 ~JRAM ()
 Destructor.
 
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.
 
void gc ()
 Run garbage collector.
 

Private Member Functions

void addMemory ()
 Add memory.
 
 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

◆ JRAM() [1/3]

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 numberOfBlocks
Definition JRAM.hh:30
const std::size_t BLOCK_SIZE
Definition JRAM.hh:29
void addMemory()
Add memory.
Definition JRAM.hh:37

◆ ~JRAM()

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

◆ JRAM() [2/3]

JLANG::JRAM::JRAM ( const JRAM & )
private

◆ JRAM() [3/3]

JLANG::JRAM::JRAM ( JRAM && )
private

Member Function Documentation

◆ addMemory()

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 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.

◆ getTotalRAM()

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 }

◆ getFreeRAM()

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 }

◆ allocate()

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 }

◆ free()

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 }

◆ gc()

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.

◆ operator=() [1/2]

JRAM & JLANG::JRAM::operator= ( const JRAM & )
private

◆ operator=() [2/2]

JRAM & JLANG::JRAM::operator= ( JRAM && )
private

Member Data Documentation

◆ BLOCK_SIZE

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

Definition at line 29 of file JRAM.hh.

◆ numberOfBlocks

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

Definition at line 30 of file JRAM.hh.

◆ memory

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: