Jpp  17.2.1-pre0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRAM.hh
Go to the documentation of this file.
1 #ifndef __JLANG__JRAM__
2 #define __JLANG__JRAM__
3 
4 #include <vector>
5 
6 #include "JLang/JException.hh"
7 
8 
9 /**
10  * \file
11  * Auxiliary class to speed up new/delete operations of any class.
12  * \author mdejong
13  */
14 namespace JLANG {}
15 namespace JPP { using namespace JLANG; }
16 
17 namespace JLANG {
18 
19  /**
20  * Memory management for small objects.
21  * This object allocator is optimised for speed at the cost of some memory overhead.
22  */
23  class JRAM :
24  public std::vector<void*>
25  {
26  private:
27 
28  const std::size_t BLOCK_SIZE;
29  const std::size_t numberOfBlocks;
31 
32 
33  /**
34  * Add memory.
35  */
36  void addMemory()
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  }
58 
59  public:
60  /**
61  * Constructor.
62  *
63  * \param block_size number of bytes
64  * \param number_of_blocks number of blocks
65  */
66  JRAM(const std::size_t block_size,
67  const std::size_t number_of_blocks = 65536) :
68  std::vector<void*>(),
69  BLOCK_SIZE (block_size),
70  numberOfBlocks(number_of_blocks)
71  {
72  addMemory();
73  }
74 
75 
76  /**
77  * Destructor.
78  */
80  {
81  for (std::vector<unsigned char*>::iterator i = memory.begin(); i != memory.end(); ++i) {
82  delete [] (*i);
83  }
84  }
85 
86 
87  /**
88  * Get total used RAM.
89  *
90  * \return number of bytes
91  */
92  long long int getTotalRAM()
93  {
94  return this->size() * sizeof(void*) + memory.size() * BLOCK_SIZE * numberOfBlocks;
95  }
96 
97 
98  /**
99  * Get total free RAM.
100  *
101  * \return number of bytes
102  */
103  long long int getFreeRAM()
104  {
105  return this->size() * BLOCK_SIZE;
106  }
107 
108 
109  /**
110  * Allocate memory.
111  *
112  * \return pointer to avaible memory
113  */
114  inline void* allocate()
115  {
116  if (this->empty()) {
117  addMemory();
118  }
119 
120  this->pop_back();
121 
122  return *(this->end());
123  }
124 
125 
126  /**
127  * Deallocate memory.
128  *
129  * \param p pointer to memory to be freed
130  */
131  inline void free(void* p)
132  {
133  this->push_back(p);
134  }
135 
136 
137  /**
138  * Run garbage collector.
139  */
140  void gc()
141  {
142  using namespace std;
143 
144  sort(this->begin(), this->end());
145 
146  vector<unsigned char*>::iterator __end = memory.end();
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  }
172 
173  private:
174  JRAM(const JRAM&);
175  JRAM(JRAM&&);
176  JRAM& operator=(const JRAM&);
177  JRAM& operator=(JRAM&&);
178  };
179 }
180 
181 #endif
void * allocate()
Allocate memory.
Definition: JRAM.hh:114
General exception.
Definition: JException.hh:23
JRAM(const std::size_t block_size, const std::size_t number_of_blocks=65536)
Constructor.
Definition: JRAM.hh:66
Exceptions.
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
JRAM & operator=(const JRAM &)
void addMemory()
Add memory.
Definition: JRAM.hh:36
~JRAM()
Destructor.
Definition: JRAM.hh:79
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
long long int getFreeRAM()
Get total free RAM.
Definition: JRAM.hh:103
const std::size_t numberOfBlocks
Definition: JRAM.hh:29
std::vector< unsigned char * > memory
Definition: JRAM.hh:30
long long int getTotalRAM()
Get total used RAM.
Definition: JRAM.hh:92
void gc()
Run garbage collector.
Definition: JRAM.hh:140
Memory management for small objects.
Definition: JRAM.hh:23
void free(void *p)
Deallocate memory.
Definition: JRAM.hh:131