Jpp test-rotations-old
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 this->pop_back();
122
123 return *(this->end());
124 }
125
126
127 /**
128 * Deallocate memory.
129 *
130 * \param p pointer to memory to be freed
131 */
132 inline void free(void* p)
133 {
134 this->push_back(p);
135 }
136
137
138 /**
139 * Run garbage collector.
140 */
141 void gc()
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 }
173
174 private:
175 JRAM(const JRAM&);
179 };
180}
181
182#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:132
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:141
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).