OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
PtrMemory.hpp
1 /*
2  * PtrMemory.hpp
3  *
4  * Created on: Apr 15, 2015
5  * Author: Pietro Incardona
6  */
7 
8 #ifndef PTRMEMORY_HPP_
9 #define PTRMEMORY_HPP_
10 
11 
32 #include "config.h"
33 #include "memory.hpp"
34 #include <cstddef>
35 #include <cstdint>
36 #include <iostream>
37 
38 #ifdef SE_CLASS2
39 #include "Memleak_check.hpp"
40 #endif
41 
42 class PtrMemory : public memory
43 {
45  size_t spm;
46 
48  void * dm;
49 
51  long int ref_cnt;
52 
54  bool copyDeviceToDevice(const PtrMemory & m);
55 
57  bool copyFromPointer(const void * ptr, size_t sz);
58 
60  void setAlignment(size_t align);
61 
62 public:
63 
65  virtual bool flush() {return true;};
67  virtual bool allocate(size_t sz);
69  virtual void destroy();
71  virtual bool copy(const memory & m);
73  virtual size_t size() const;
75  virtual bool resize(size_t sz);
77  virtual void * getPointer();
78 
80  virtual const void * getPointer() const;
81 
83  virtual void * getDevicePointer();
84 
86  virtual void deviceToHost(){};
87 
89  virtual void incRef()
90  {ref_cnt++;}
91 
93  virtual void decRef()
94  {ref_cnt--;}
95 
97  virtual long int ref()
98  {
99  return ref_cnt;
100  }
101 
108  {
109  return true;
110  }
111 
112  // Default constructor
113  PtrMemory():spm(0),dm(NULL),ref_cnt(0)
114  {
115  };
116 
118  PtrMemory(void * ptr, size_t sz):spm(sz),dm(ptr),ref_cnt(0)
119  {
120  };
121 
122  ~PtrMemory()
123  {
124  if(ref_cnt == 0)
125  destroy();
126  else
127  std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n";
128  };
129 };
130 
131 
132 #endif /* PTRMEMORY_HPP_ */
long int ref_cnt
Reference counter.
Definition: PtrMemory.hpp:51
virtual void * getDevicePointer()
get a readable pointer with the data
Definition: PtrMemory.cpp:162
bool isInitialized()
Allocated Memory is already initialized.
Definition: PtrMemory.hpp:107
virtual bool resize(size_t sz)
resize the memory allocated
Definition: PtrMemory.cpp:132
bool copyDeviceToDevice(const PtrMemory &m)
copy from same Heap to Heap
Definition: PtrMemory.cpp:67
virtual bool flush()
flush the memory
Definition: PtrMemory.hpp:65
void setAlignment(size_t align)
Set alignment the memory will be aligned with this number.
void * dm
Pointed memory.
Definition: PtrMemory.hpp:48
virtual void deviceToHost()
Do nothing.
Definition: PtrMemory.hpp:86
bool copyFromPointer(const void *ptr, size_t sz)
copy from Pointer to Heap
Definition: PtrMemory.cpp:49
virtual void destroy()
destroy memory
Definition: PtrMemory.cpp:40
virtual void decRef()
Decrement the reference counter.
Definition: PtrMemory.hpp:93
virtual long int ref()
Return the reference counter.
Definition: PtrMemory.hpp:97
PtrMemory(void *ptr, size_t sz)
Constructor, we choose a default alignment of 32 for avx.
Definition: PtrMemory.hpp:118
virtual void incRef()
Increment the reference counter.
Definition: PtrMemory.hpp:89
virtual size_t size() const
the the size of the allocated memory
Definition: PtrMemory.cpp:117
virtual void * getPointer()
get a readable pointer with the data
Definition: PtrMemory.cpp:151
virtual bool allocate(size_t sz)
allocate memory
Definition: PtrMemory.cpp:28
size_t spm
Size of the pointed memory.
Definition: PtrMemory.hpp:45
virtual bool copy(const memory &m)
copy memory
Definition: PtrMemory.cpp:87
This class give memory from a preallocated memory, memory destruction is not performed.
Definition: PtrMemory.hpp:42