OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
HeapMemory.hpp
1 /*
2  * HeapMempory.hpp
3  *
4  * Created on: Aug 17, 2014
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef HEAP_MEMORY_HPP
9 #define HEAP_MEMORY_HPP
10 
11 #include "config.h"
12 #include "memory.hpp"
13 #include <cstddef>
14 #include <cstdint>
15 #include <iostream>
16 
17 typedef unsigned char byte;
18 
19 #define MEM_ALIGNMENT 32
20 
21 
39 class HeapMemory : public memory
40 {
42  size_t alignement;
43 
45  size_t sz;
46 
48  byte * dm;
50  byte * dmOrig;
52  long int ref_cnt;
53 
55  bool copyDeviceToDevice(const HeapMemory & m);
56 
58  bool copyFromPointer(const void * ptr, size_t sz);
59 
61  void setAlignment(size_t align);
62 
63 public:
64 
66  virtual bool flush() {return true;};
68  virtual bool allocate(size_t sz);
70  virtual void destroy();
72  virtual bool copy(const memory & m);
74  virtual size_t size() const;
76  virtual bool resize(size_t sz);
78  virtual void * getPointer();
79 
81  virtual const void * getPointer() const;
82 
84  virtual void * getDevicePointer();
85 
87  virtual void deviceToHost(){};
88 
90  virtual void incRef()
91  {ref_cnt++;}
92 
94  virtual void decRef()
95  {ref_cnt--;}
96 
98  virtual long int ref()
99  {
100  return ref_cnt;
101  }
102 
109  {
110  return false;
111  }
112 
113  // Copy the Heap memory
114  HeapMemory & operator=(const HeapMemory & mem)
115  {
116  copy(mem);
117  return *this;
118  }
119 
120  // Copy the Heap memory
121  HeapMemory(const HeapMemory & mem)
122  :HeapMemory()
123  {
124  allocate(mem.size());
125  copy(mem);
126  }
127 
128  HeapMemory(HeapMemory && mem) noexcept
129  {
131  alignement = mem.alignement;
132  sz = mem.sz;
133  dm = mem.dm;
134  dmOrig = mem.dmOrig;
135  ref_cnt = mem.ref_cnt;
136 
137  mem.alignement = MEM_ALIGNMENT;
138  mem.sz = 0;
139  mem.dm = NULL;
140  mem.dmOrig = NULL;
141  mem.ref_cnt = 0;
142  }
143 
145  HeapMemory():alignement(MEM_ALIGNMENT),sz(0),dm(NULL),dmOrig(NULL),ref_cnt(0) {};
146 
147  virtual ~HeapMemory() noexcept
148  {
149  if(ref_cnt == 0)
151  else
152  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
153  };
154 
160  void swap(HeapMemory & mem)
161  {
162  size_t alignement_tmp;
163  size_t sz_tmp;
164  byte * dm_tmp;
165  byte * dmOrig_tmp;
166  long int ref_cnt_tmp;
167 
168  alignement_tmp = alignement;
169  sz_tmp = sz;
170  dm_tmp = dm;
171  dmOrig_tmp = dmOrig;
172  ref_cnt_tmp = ref_cnt;
173 
174  alignement = mem.alignement;
175  sz = mem.sz;
176  dm = mem.dm;
177  dmOrig = mem.dmOrig;
178  ref_cnt = mem.ref_cnt;
179 
180  mem.alignement = alignement_tmp;
181  mem.sz = sz_tmp;
182  mem.dm = dm_tmp;
183  mem.dmOrig = dmOrig_tmp;
184  mem.ref_cnt = ref_cnt_tmp;
185  }
186 };
187 
188 
195 inline void *align( std::size_t alignment, std::size_t size,
196  void *&ptr, std::size_t &space ) {
197  std::uintptr_t pn = reinterpret_cast< std::uintptr_t >( ptr );
198  std::uintptr_t aligned = ( pn + alignment - 1 ) & - alignment;
199  std::size_t padding = aligned - pn;
200  if ( space < size + padding ) return nullptr;
201  space -= padding;
202  return ptr = reinterpret_cast< void * >( aligned );
203 }
204 
205 #endif
HeapMemory(HeapMemory &&mem) noexcept
Definition: HeapMemory.hpp:128
void swap(HeapMemory &mem)
Swap the memory.
Definition: HeapMemory.hpp:160
virtual void deviceToHost()
Do nothing.
Definition: HeapMemory.hpp:87
virtual bool flush()
flush the memory
Definition: HeapMemory.hpp:66
virtual bool allocate(size_t sz)
allocate memory
Definition: HeapMemory.cpp:27
virtual void decRef()
Decrement the reference counter.
Definition: HeapMemory.hpp:94
virtual void incRef()
Increment the reference counter.
Definition: HeapMemory.hpp:90
long int ref_cnt
Reference counter.
Definition: HeapMemory.hpp:52
void setAlignment(size_t align)
Set alignment the memory will be aligned with this number.
Definition: HeapMemory.cpp:54
byte * dm
device memory
Definition: HeapMemory.hpp:48
HeapMemory()
Constructor, we choose a default alignment of 32 for avx.
Definition: HeapMemory.hpp:145
virtual size_t size() const
the the size of the allocated memory
Definition: HeapMemory.cpp:157
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
size_t alignement
memory alignment
Definition: HeapMemory.hpp:42
virtual void * getPointer()
get a readable pointer with the data
Definition: HeapMemory.cpp:237
virtual void destroy()
destroy memory
Definition: HeapMemory.cpp:63
byte * dmOrig
original pointer (before alignment)
Definition: HeapMemory.hpp:50
virtual void * getDevicePointer()
get a device pointer for HeapMemory getPointer and getDevicePointer are equivalents ...
Definition: HeapMemory.cpp:226
virtual long int ref()
Return the reference counter.
Definition: HeapMemory.hpp:98
virtual bool resize(size_t sz)
resize the memory allocated
Definition: HeapMemory.cpp:171
bool copyFromPointer(const void *ptr, size_t sz)
copy from Pointer to Heap
Definition: HeapMemory.cpp:83
bool isInitialized()
Allocated Memory is never initialized.
Definition: HeapMemory.hpp:108
virtual bool copy(const memory &m)
copy memory
Definition: HeapMemory.cpp:127
size_t sz
Size of the memory.
Definition: HeapMemory.hpp:45
bool copyDeviceToDevice(const HeapMemory &m)
copy from same Heap to Heap
Definition: HeapMemory.cpp:103