1 | /* |
2 | * PtrMemory.cpp |
3 | * |
4 | * Created on: Apr 15, 2015 |
5 | * Author: i-bird |
6 | */ |
7 | |
8 | #ifndef PTRMEMORY_CPP_ |
9 | #define PTRMEMORY_CPP_ |
10 | |
11 | #include "PtrMemory.hpp" |
12 | #include <cstddef> |
13 | #include <cstring> |
14 | #include <iostream> |
15 | #include <cstdint> |
16 | |
17 | /*! \brief fill memory with the selected byte |
18 | * |
19 | * \param byte to fill |
20 | * |
21 | */ |
22 | void PtrMemory::fill(unsigned char c) |
23 | { |
24 | memset(dm,c,this->size()); |
25 | } |
26 | |
27 | /*! \brief Allocate a chunk of memory |
28 | * |
29 | * \param sz size of the chunk of memory to allocate in byte |
30 | * |
31 | */ |
32 | |
33 | bool PtrMemory::allocate(size_t sz) |
34 | { |
35 | if (sz <= spm) |
36 | return true; |
37 | |
38 | std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " allocation failed" ; |
39 | return false; |
40 | } |
41 | |
42 | /*! \brief destroy a chunk of memory |
43 | * |
44 | */ |
45 | void PtrMemory::destroy() |
46 | { |
47 | } |
48 | |
49 | |
50 | /*! \brief copy the data from a pointer |
51 | * |
52 | * \param ptr |
53 | */ |
54 | bool PtrMemory::copyFromPointer(const void * ptr,size_t sz) |
55 | { |
56 | // memory copy |
57 | |
58 | memcpy(dm,ptr,sz); |
59 | |
60 | return true; |
61 | } |
62 | |
63 | /*! \brief copy from device to device |
64 | * |
65 | * copy a piece of memory from device to device |
66 | * |
67 | * \param m PtrMemory from where to copy |
68 | * |
69 | * \return true if the memory is successful copy |
70 | * |
71 | */ |
72 | bool PtrMemory::copyDeviceToDevice(const PtrMemory & m) |
73 | { |
74 | //! The source buffer is too big to copy it |
75 | |
76 | if (m.spm > spm) |
77 | { |
78 | std::cerr << "Error " << __LINE__ << " " << __FILE__ << ": source buffer is too big to copy" ; |
79 | return false; |
80 | } |
81 | |
82 | // Copy the memory from m |
83 | memcpy(dm,m.dm,m.spm); |
84 | return true; |
85 | } |
86 | |
87 | /*! \brief copy the memory |
88 | * |
89 | * \param m a memory interface |
90 | * |
91 | */ |
92 | bool PtrMemory::copy(const memory & m) |
93 | { |
94 | //! Here we try to cast memory into PtrMemory |
95 | const PtrMemory * ofpm = dynamic_cast<const PtrMemory *>(&m); |
96 | |
97 | //! if we fail we get the pointer and simply copy from the pointer |
98 | |
99 | if (ofpm == NULL) |
100 | { |
101 | // copy the memory from device to host and from host to device |
102 | |
103 | return copyFromPointer(m.getPointer(),m.size()); |
104 | } |
105 | else |
106 | { |
107 | // they are the same memory type, use cuda/thrust buffer copy |
108 | |
109 | return copyDeviceToDevice(*ofpm); |
110 | } |
111 | return false; |
112 | } |
113 | |
114 | /*! \brief Get the size of the allocated memory |
115 | * |
116 | * Get the size of the allocated memory |
117 | * |
118 | * \return the size of the allocated memory |
119 | * |
120 | */ |
121 | |
122 | size_t PtrMemory::size() const |
123 | { |
124 | return spm; |
125 | } |
126 | |
127 | /*! \brief Resize the allocated memory |
128 | * |
129 | * Resize the allocated memory, if request is smaller than the allocated memory |
130 | * is not resized |
131 | * |
132 | * \param sz size |
133 | * \return true if the resize operation complete correctly |
134 | * |
135 | */ |
136 | |
137 | bool PtrMemory::resize(size_t sz) |
138 | { |
139 | // if the allocated memory is enough, do not resize |
140 | if (sz <= spm) |
141 | { |
142 | this->spm = sz; |
143 | return true; |
144 | } |
145 | |
146 | std::cerr << "Error: " << __FILE__ << " " << __LINE__ << " allocation failed" ; |
147 | return false; |
148 | } |
149 | |
150 | /*! \brief Return a pointer to the allocated memory |
151 | * |
152 | * \return the pointer |
153 | * |
154 | */ |
155 | |
156 | void * PtrMemory::getPointer() |
157 | { |
158 | return dm; |
159 | } |
160 | |
161 | /*! \brief Return a pointer to the allocated memory |
162 | * |
163 | * \return the pointer |
164 | * |
165 | */ |
166 | |
167 | void * PtrMemory::getDevicePointer() |
168 | { |
169 | return dm; |
170 | } |
171 | |
172 | |
173 | /*! \brief Return a pointer to the allocated memory |
174 | * |
175 | * \return the pointer |
176 | * |
177 | */ |
178 | |
179 | const void * PtrMemory::getPointer() const |
180 | { |
181 | return dm; |
182 | } |
183 | |
184 | |
185 | #endif /* PTRMEMORY_CPP_ */ |
186 | |