| 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 | |
| 12 | /** |
| 13 | * \brief This class give memory from a preallocated memory, memory destruction is not performed |
| 14 | * |
| 15 | * Useful to shape pieces of memory |
| 16 | * |
| 17 | * Usage: |
| 18 | * |
| 19 | * void * ptr = new int[1000] |
| 20 | * |
| 21 | * PtrMemory m = new PtrMemory(ptr,1000); |
| 22 | * |
| 23 | * m.allocate(); |
| 24 | * int * ptr = m.getPointer(); |
| 25 | * *ptr[999] = 1000; |
| 26 | * .... |
| 27 | * |
| 28 | * delete[] ptr; |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include "config.h" |
| 33 | #include "memory.hpp" |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
| 36 | #include <iostream> |
| 37 | |
| 38 | |
| 39 | class PtrMemory : public memory |
| 40 | { |
| 41 | //! Size of the pointed memory |
| 42 | size_t spm; |
| 43 | |
| 44 | //! Pointed memory |
| 45 | void * dm; |
| 46 | |
| 47 | //! Reference counter |
| 48 | long int ref_cnt; |
| 49 | |
| 50 | //! copy from Pointer to Heap |
| 51 | bool copyFromPointer(const void * ptr, size_t sz); |
| 52 | |
| 53 | //! Set alignment the memory will be aligned with this number |
| 54 | void setAlignment(size_t align); |
| 55 | |
| 56 | public: |
| 57 | |
| 58 | //! copy from same Heap to Heap |
| 59 | bool copyDeviceToDevice(const PtrMemory & m); |
| 60 | |
| 61 | //! flush the memory |
| 62 | virtual bool flush() {return true;}; |
| 63 | //! allocate memory |
| 64 | virtual bool allocate(size_t sz); |
| 65 | //! destroy memory |
| 66 | virtual void destroy(); |
| 67 | //! copy memory |
| 68 | virtual bool copy(const memory & m); |
| 69 | //! the the size of the allocated memory |
| 70 | virtual size_t size() const; |
| 71 | //! resize the memory allocated |
| 72 | virtual bool resize(size_t sz); |
| 73 | //! get a readable pointer with the data |
| 74 | virtual void * getPointer(); |
| 75 | |
| 76 | //! get a readable pointer with the data |
| 77 | virtual const void * getPointer() const; |
| 78 | |
| 79 | //! get a readable pointer with the data |
| 80 | virtual void * getDevicePointer(); |
| 81 | |
| 82 | |
| 83 | //! Do nothing |
| 84 | virtual void deviceToHost(){}; |
| 85 | |
| 86 | //! Do nothing |
| 87 | virtual void hostToDevice(){}; |
| 88 | |
| 89 | //! Do nothing |
| 90 | virtual void deviceToHost(size_t start, size_t stop) {}; |
| 91 | |
| 92 | //! Do nothing |
| 93 | virtual void hostToDevice(size_t start, size_t top) {}; |
| 94 | |
| 95 | /*! \brief fill memory with the selected byte |
| 96 | * |
| 97 | * |
| 98 | */ |
| 99 | virtual void fill(unsigned char c); |
| 100 | |
| 101 | //! Increment the reference counter |
| 102 | virtual void incRef() |
| 103 | {ref_cnt++;} |
| 104 | |
| 105 | //! Decrement the reference counter |
| 106 | virtual void decRef() |
| 107 | {ref_cnt--;} |
| 108 | |
| 109 | //! Return the reference counter |
| 110 | virtual long int ref() |
| 111 | { |
| 112 | return ref_cnt; |
| 113 | } |
| 114 | |
| 115 | /*! \brief Return true if the device and the host pointer are the same |
| 116 | * |
| 117 | * \return true if they are the same |
| 118 | * |
| 119 | */ |
| 120 | static bool isDeviceHostSame() |
| 121 | { |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | /*! \brief Allocated Memory is already initialized |
| 126 | * |
| 127 | * \return true |
| 128 | * |
| 129 | */ |
| 130 | bool isInitialized() |
| 131 | { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | // Default constructor |
| 136 | PtrMemory():spm(0),dm(NULL),ref_cnt(0) |
| 137 | { |
| 138 | }; |
| 139 | |
| 140 | //! Constructor, we choose a default alignment of 32 for avx |
| 141 | PtrMemory(void * ptr, size_t sz):spm(sz),dm(ptr),ref_cnt(0) |
| 142 | { |
| 143 | }; |
| 144 | |
| 145 | ~PtrMemory() |
| 146 | { |
| 147 | if(ref_cnt == 0) |
| 148 | destroy(); |
| 149 | else |
| 150 | std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " destroying a live object" << "\n" ; |
| 151 | }; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | #endif /* PTRMEMORY_HPP_ */ |
| 156 | |