Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JRAM.hh
Go to the documentation of this file.
1#ifndef __JLANG__JRAM__
2#define __JLANG__JRAM__
3
4#include <vector>
5#include <algorithm>
6
7#include "JLang/JException.hh"
8
9
10/**
11 * \file
12 * Auxiliary class to speed up new/delete operations of any class.
13 * \author mdejong
14 */
15namespace JLANG {}
16namespace JPP { using namespace JLANG; }
17
18namespace JLANG {
19
20 /**
21 * Memory management for small objects.
22 * This object allocator is optimised for speed at the cost of some memory overhead.
23 */
24 class JRAM :
25 public std::vector<void*>
26 {
27 private:
28
29 const std::size_t BLOCK_SIZE;
30 const std::size_t numberOfBlocks;
32
33
34 /**
35 * Add memory.
36 */
37 void addMemory()
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 }
59
60 public:
61 /**
62 * Constructor.
63 *
64 * \param block_size number of bytes
65 * \param number_of_blocks number of blocks
66 */
67 JRAM(const std::size_t block_size,
68 const std::size_t number_of_blocks = 65536) :
69 std::vector<void*>(),
70 BLOCK_SIZE (block_size),
71 numberOfBlocks(number_of_blocks)
72 {
73 addMemory();
74 }
75
76
77 /**
78 * Destructor.
79 */
81 {
82 for (std::vector<unsigned char*>::iterator i = memory.begin(); i != memory.end(); ++i) {
83 delete [] (*i);
84 }
85 }
86
87
88 /**
89 * Get total used RAM.
90 *
91 * \return number of bytes
92 */
93 long long int getTotalRAM()
94 {
95 return this->size() * sizeof(void*) + memory.size() * BLOCK_SIZE * numberOfBlocks;
96 }
97
98
99 /**
100 * Get total free RAM.
101 *
102 * \return number of bytes
103 */
104 long long int getFreeRAM()
105 {
106 return this->size() * BLOCK_SIZE;
107 }
108
109
110 /**
111 * Allocate memory.
112 *
113 * \return pointer to avaible memory
114 */
115 inline void* allocate()
116 {
117 if (this->empty()) {
118 addMemory();
119 }
120
121 void* p = this->back();
122
123 this->pop_back();
124
125 return p;
126 }
127
128
129 /**
130 * Deallocate memory.
131 *
132 * \param p pointer to memory to be freed
133 */
134 inline void free(void* p)
135 {
136 this->push_back(p);
137 }
138
139
140 /**
141 * Run garbage collector.
142 */
143 void gc()
144 {
145 using namespace std;
146
147 sort(this->begin(), this->end());
148
150
151 for (vector<unsigned char*>::iterator i = memory.begin(); i != __end; ) {
152
153 iterator p = lower_bound(this->begin(), this->end(), *i);
154 iterator q = p + numberOfBlocks - 1;
155
156 if (*p == *i &&
157 distance(q, this->end()) > 0 &&
158 ((unsigned char*) (*q) - (unsigned char*) *p) == (int) (BLOCK_SIZE * (numberOfBlocks - 1))) {
159
160 iter_swap(i, --__end);
161
162 this->erase(p,++q);
163
164 } else {
165 ++i;
166 }
167 }
168
169 for (std::vector<unsigned char*>::iterator i = __end; i != memory.end(); ++i) {
170 delete [] (*i);
171 }
172
173 memory.erase(__end, memory.end());
174 }
175
176 private:
177 JRAM(const JRAM&);
181 };
182}
183
184#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
General exception.
Definition JException.hh:24
Memory management for small objects.
Definition JRAM.hh:26
JRAM(const std::size_t block_size, const std::size_t number_of_blocks=65536)
Constructor.
Definition JRAM.hh:67
void free(void *p)
Deallocate memory.
Definition JRAM.hh:134
JRAM(JRAM &&)
const std::size_t numberOfBlocks
Definition JRAM.hh:30
JRAM(const JRAM &)
std::vector< unsigned char * > memory
Definition JRAM.hh:31
~JRAM()
Destructor.
Definition JRAM.hh:80
JRAM & operator=(const JRAM &)
void gc()
Run garbage collector.
Definition JRAM.hh:143
const std::size_t BLOCK_SIZE
Definition JRAM.hh:29
void * allocate()
Allocate memory.
Definition JRAM.hh:115
long long int getTotalRAM()
Get total used RAM.
Definition JRAM.hh:93
long long int getFreeRAM()
Get total free RAM.
Definition JRAM.hh:104
void addMemory()
Add memory.
Definition JRAM.hh:37
JRAM & operator=(JRAM &&)
Auxiliary classes and methods for language specific functionality.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).