OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
ExtPreAlloc.hpp
1 /* ExtPreAlloc.hpp
2  *
3  * Created on: Apr 3, 2015
4  * Author: Pietro Incardona
5  */
6 
7 #ifndef EXTPREALLOC_HPP_
8 #define EXTPREALLOC_HPP_
9 
10 #include <stddef.h>
11 #include "memory.hpp"
12 #include <iostream>
13 
26 template<typename Mem>
27 class ExtPreAlloc : public memory
28 {
30  size_t a_seq ;
31 
33  size_t l_size;
34 
36  Mem * mem;
37 
39  long int ref_cnt;
40 
41 public:
42 
43  virtual ~ExtPreAlloc()
44  {
45  if (ref_cnt != 0)
46  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
47  }
48 
51  :a_seq(0),l_size(0),mem(NULL),ref_cnt(0)
52  {
53  }
54 
61  ExtPreAlloc(size_t size, Mem & mem)
62  :a_seq(0),l_size(0),mem(&mem),ref_cnt(0)
63  {
64  // Allocate the total size of memory
65  mem.resize(size);
66  }
67 
69  virtual void incRef()
70  {ref_cnt++;}
71 
73  virtual void decRef()
74  {ref_cnt--;}
75 
77  virtual long int ref()
78  {
79  return ref_cnt;
80  }
81 
83  virtual bool flush() {return mem->flush();};
84 
92  virtual bool allocate(size_t sz)
93  {
94  // Zero sized allocation are ignored
95  if (sz == 0)
96  return true;
97 
98  a_seq = l_size;
99  l_size += sz;
100 
101  // Check we do not overflow the allocated memory
102 #ifdef SE_CLASS1
103 
104  if (l_size > mem->size())
105  std::cerr << __FILE__ << ":" << __LINE__ << " Error requesting more memory than the allocated one" << std::endl;
106 
107 #endif
108 
109  return true;
110  }
111 
117  void * getPointerEnd()
118  {
119  return (char *)mem->getPointer() + l_size;
120  }
121 
127  void * getPointerBase()
128  {
129  return mem->getPointer();
130  }
131 
137  virtual void * getDevicePointer()
138  {
139  return getPointer();
140  }
141 
143  virtual void deviceToHost(){};
144 
150  virtual void * getPointer()
151  {
152  return (((unsigned char *)mem->getPointer()) + a_seq );
153  }
154 
160  virtual const void * getPointer() const
161  {
162  return (((unsigned char *)mem->getPointer()) + a_seq);
163  }
164 
170  void * getPointerOffset(size_t offset)
171  {
172  return (((unsigned char *)mem->getPointer()) + offset);
173  }
174 
185  virtual bool resize(size_t sz)
186  {
187  return allocate(sz);
188  }
189 
198  virtual size_t size() const
199  {
200  return l_size;
201  }
202 
207  void destroy()
208  {
209  mem->destroy();
210  }
211 
216  virtual bool copy(const memory & m)
217  {
218  return mem->copy(m);
219  }
220 
227  {
228  return false;
229  }
230 
236  static size_t calculateMem(std::vector<size_t> & mm)
237  {
238  size_t s = 0;
239 
240  for (size_t i = 0 ; i < mm.size() ; i++)
241  s += mm[i];
242 
243  return s;
244  }
245 
280  void shift_backward(size_t sz)
281  {
282  a_seq -= sz;
283  l_size = a_seq;
284  }
285 
297  void shift_forward(size_t sz)
298  {
299  a_seq += sz;
300  l_size = a_seq;
301  }
302 };
303 
304 #endif /* PREALLOCHEAPMEMORY_HPP_ */
bool isInitialized()
Allocated Memory is never initialized.
virtual void deviceToHost()
Do nothing.
long int ref_cnt
Reference counter.
Definition: ExtPreAlloc.hpp:39
virtual void * getPointer()
Return the pointer of the last allocation.
void shift_forward(size_t sz)
shift the pointer forward
void shift_backward(size_t sz)
shift the pointer backward
size_t a_seq
Actual allocation pointer.
Definition: ExtPreAlloc.hpp:30
virtual bool allocate(size_t sz)
Allocate a chunk of memory.
Definition: ExtPreAlloc.hpp:92
ExtPreAlloc()
Default constructor.
Definition: ExtPreAlloc.hpp:50
virtual long int ref()
Return the reference counter.
Definition: ExtPreAlloc.hpp:77
size_t l_size
Last allocation size.
Definition: ExtPreAlloc.hpp:33
ExtPreAlloc(size_t size, Mem &mem)
Preallocated memory sequence.
Definition: ExtPreAlloc.hpp:61
virtual bool flush()
flush the memory
Definition: ExtPreAlloc.hpp:83
void * getPointerOffset(size_t offset)
Get the base memory pointer increased with an offset.
virtual void incRef()
Increment the reference counter.
Definition: ExtPreAlloc.hpp:69
virtual bool copy(const memory &m)
Copy memory.
virtual bool resize(size_t sz)
Allocate or resize the allocated memory.
void * getPointerBase()
The the base pointer of the preallocate memory.
void destroy()
Destroy memory.
virtual void decRef()
Decrement the reference counter.
Definition: ExtPreAlloc.hpp:73
Mem * mem
Main class for memory allocation.
Definition: ExtPreAlloc.hpp:36
virtual const void * getPointer() const
Return the pointer of the last allocation.
virtual size_t size() const
Get the size of the LAST allocated memory.
virtual void * getDevicePointer()
Return the pointer of the last allocation.
static size_t calculateMem(std::vector< size_t > &mm)
Calculate the total memory required to pack the message.
void * getPointerEnd()
Return the end pointer of the previous allocated memory.