ParGeMSLR
memory.hpp
Go to the documentation of this file.
1 #ifndef PARGEMSLR_MEMORY_H
2 #define PARGEMSLR_MEMORY_H
3 
4 #include <vector>
5 #include <unordered_map>
6 
12 namespace pargemslr
13 {
14 
20  {
21  kMemoryHost,
22  kMemoryDevice,
23  kMemoryUnified,
24  kMemoryPinned
25  };
26 
34  void* PargemslrMalloc( size_t length, int location);
42  void* PargemslrCalloc( size_t length, int location);
43 
53  void* PargemslrRealloc( void* ptr, size_t current_length, size_t new_length, int location);
54 
64  void PargemslrMemcpy( void* ptr_to, const void* ptr_from, size_t length, int loc_to, int loc_from);
65 
72  void PargemslrFree( void* ptr, int location);
73 
74 #ifdef PARGEMSLR_DEBUG_MEMORY
75 
79  typedef struct MemoryDebuggerStruct
80  {
85  std::vector<char> _actions;
86 
91  std::vector<size_t> _lengths;
92 
97  std::vector<void*> _addresses;
98 
103  std::vector<std::string> _filenames;
104 
109  std::vector<std::string> _functions;
110 
115  std::vector<int> _line;
116 
121  static std::unordered_map< void*, MemoryDebuggerStruct> _memory_tracker;
122 
127  static void InsertMemoryAction( char action, size_t length, const void *address, const char *filename, const char *function, const int line);
128 
133  static void CheckMemoryLocation( const void *ptr, int location, const char *filename, const char *function, const int line);
134 
135  }memory_debugger;
136 
137 #endif
138 
139 }
140 
141 #ifdef PARGEMSLR_DEBUG_MEMORY
142 
143 #define PARGEMSLR_MALLOC_VOID(ptr, length, location) {\
144  (ptr) = pargemslr::PargemslrMalloc( (size_t)(length), location);\
145  pargemslr::MemoryDebuggerStruct::InsertMemoryAction('m', (size_t)(length), (const void*)ptr, __FILE__, __func__, __LINE__);\
146 }
147 
148 #define PARGEMSLR_MALLOC(ptr, length, location, ...) {\
149  (ptr) = (__VA_ARGS__*) pargemslr::PargemslrMalloc( (size_t)(length)*sizeof(__VA_ARGS__), location);\
150  pargemslr::MemoryDebuggerStruct::InsertMemoryAction('m', (size_t)(length)*sizeof(__VA_ARGS__), (const void*)ptr, __FILE__, __func__, __LINE__);\
151 }
152 
153 #define PARGEMSLR_CALLOC_VOID(ptr, length, location) {\
154  (ptr) = pargemslr::PargemslrCalloc( (size_t)(length), location);\
155  pargemslr::MemoryDebuggerStruct::InsertMemoryAction('c', (size_t)(length), (const void*)ptr, __FILE__, __func__, __LINE__);\
156 }
157 
158 #define PARGEMSLR_CALLOC(ptr, length, location, ...) {\
159  (ptr) = (__VA_ARGS__*) pargemslr::PargemslrCalloc( (size_t)length*sizeof(__VA_ARGS__), location);\
160  pargemslr::MemoryDebuggerStruct::InsertMemoryAction('c', (size_t)(length)*sizeof(__VA_ARGS__), (const void*)ptr, __FILE__, __func__, __LINE__);\
161 }
162 
163 #define PARGEMSLR_REALLOC_VOID(ptr, current_length, new_length, location) {\
164  (ptr) = pargemslr::PargemslrRealloc( (void*)(ptr), (size_t)(current_length), (size_t)(new_length), location);\
165  pargemslr::MemoryDebuggerStruct::InsertMemoryAction('r', (size_t)(new_length), (const void*)ptr, __FILE__, __func__, __LINE__);\
166 }
167 
168 #define PARGEMSLR_REALLOC(ptr, current_length, new_length, location, ...) {\
169  (ptr) = (__VA_ARGS__*) pargemslr::PargemslrRealloc( (void*)(ptr), (size_t)(current_length)*sizeof(__VA_ARGS__), (size_t)(new_length)*sizeof(__VA_ARGS__), location);\
170  pargemslr::MemoryDebuggerStruct::InsertMemoryAction('r', (size_t)(new_length)*sizeof(__VA_ARGS__), (const void*)ptr, __FILE__, __func__, __LINE__);\
171 }
172 
173 #define PARGEMSLR_MEMCPY(ptr_to, ptr_from, length, loc_to, loc_from, ...) {\
174  pargemslr::MemoryDebuggerStruct::CheckMemoryLocation( (const void*)(ptr_to), loc_to, __FILE__, __func__, __LINE__);\
175  pargemslr::MemoryDebuggerStruct::CheckMemoryLocation( (const void*)(ptr_from), loc_from, __FILE__, __func__, __LINE__);\
176  pargemslr::PargemslrMemcpy( (void*)(ptr_to), (void*)(ptr_from), (size_t)(length)*sizeof(__VA_ARGS__), loc_to, loc_from);\
177 }
178 
179 #define PARGEMSLR_FREE( ptr, location) {\
180  pargemslr::MemoryDebuggerStruct::CheckMemoryLocation( (const void*)ptr, location, __FILE__, __func__, __LINE__);\
181  pargemslr::PargemslrFree( (void*)(ptr), location);\
182  (ptr) = NULL;\
183 }
184 
185 #else
186 
187 #define PARGEMSLR_MALLOC_VOID(ptr, length, location) {\
188  (ptr) = pargemslr::PargemslrMalloc( (size_t)(length), location);\
189 }
190 
191 #define PARGEMSLR_MALLOC(ptr, length, location, ...) {\
192  (ptr) = (__VA_ARGS__*) pargemslr::PargemslrMalloc( (size_t)(length)*sizeof(__VA_ARGS__), location);\
193 }
194 
195 #define PARGEMSLR_CALLOC_VOID(ptr, length, location) {\
196  (ptr) = pargemslr::PargemslrCalloc( (size_t)(length), location);\
197 }
198 
199 #define PARGEMSLR_CALLOC(ptr, length, location, ...) {\
200  (ptr) = (__VA_ARGS__*) pargemslr::PargemslrCalloc( (size_t)(length)*sizeof(__VA_ARGS__), location);\
201 }
202 
203 #define PARGEMSLR_REALLOC_VOID(ptr, current_length, new_length, location) {\
204  (ptr) = (void*) pargemslr::PargemslrRealloc( (void*)(ptr), (size_t)(current_length), (size_t)(new_length), location);\
205 }
206 
207 #define PARGEMSLR_REALLOC(ptr, current_length, new_length, location, ...) {\
208  (ptr) = (__VA_ARGS__*) pargemslr::PargemslrRealloc( (void*)(ptr), (size_t)(current_length)*sizeof(__VA_ARGS__), (size_t)(new_length)*sizeof(__VA_ARGS__), location);\
209 }
210 
211 #define PARGEMSLR_MEMCPY(ptr_to, ptr_from, length, loc_to, loc_from, ...) {\
212  pargemslr::PargemslrMemcpy( (void*)(ptr_to), (void*)(ptr_from), (size_t)(length)*sizeof(__VA_ARGS__), loc_to, loc_from);\
213 }
214 
215 #define PARGEMSLR_FREE( ptr, location) {\
216  pargemslr::PargemslrFree( (void*)(ptr), location);\
217  (ptr) = NULL;\
218 }
219 
220 #endif
221 
222 #define PARGEMSLR_PLACEMENT_NEW(ptr, location, ...) {\
223  (ptr) = (__VA_ARGS__*) pargemslr::PargemslrMalloc( (size_t)sizeof(__VA_ARGS__), location);\
224  (ptr) = new (ptr) __VA_ARGS__();\
225 }
226 
227 #endif
pargemslr::MemoryLocationEnum
MemoryLocationEnum
The memory location. Host, device, shared, page-locked.
Definition: memory.hpp:20