1 #ifndef PARGEMSLR_MEMORY_H
2 #define PARGEMSLR_MEMORY_H
5 #include <unordered_map>
34 void* PargemslrMalloc(
size_t length,
int location);
42 void* PargemslrCalloc(
size_t length,
int location);
53 void* PargemslrRealloc(
void* ptr,
size_t current_length,
size_t new_length,
int location);
64 void PargemslrMemcpy(
void* ptr_to,
const void* ptr_from,
size_t length,
int loc_to,
int loc_from);
72 void PargemslrFree(
void* ptr,
int location);
74 #ifdef PARGEMSLR_DEBUG_MEMORY
79 typedef struct MemoryDebuggerStruct
85 std::vector<char> _actions;
91 std::vector<size_t> _lengths;
97 std::vector<void*> _addresses;
103 std::vector<std::string> _filenames;
109 std::vector<std::string> _functions;
115 std::vector<int> _line;
121 static std::unordered_map< void*, MemoryDebuggerStruct> _memory_tracker;
127 static void InsertMemoryAction(
char action,
size_t length,
const void *address,
const char *filename,
const char *
function,
const int line);
133 static void CheckMemoryLocation(
const void *ptr,
int location,
const char *filename,
const char *
function,
const int line);
141 #ifdef PARGEMSLR_DEBUG_MEMORY
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__);\
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__);\
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__);\
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__);\
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__);\
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__);\
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);\
179 #define PARGEMSLR_FREE( ptr, location) {\
180 pargemslr::MemoryDebuggerStruct::CheckMemoryLocation( (const void*)ptr, location, __FILE__, __func__, __LINE__);\
181 pargemslr::PargemslrFree( (void*)(ptr), location);\
187 #define PARGEMSLR_MALLOC_VOID(ptr, length, location) {\
188 (ptr) = pargemslr::PargemslrMalloc( (size_t)(length), location);\
191 #define PARGEMSLR_MALLOC(ptr, length, location, ...) {\
192 (ptr) = (__VA_ARGS__*) pargemslr::PargemslrMalloc( (size_t)(length)*sizeof(__VA_ARGS__), location);\
195 #define PARGEMSLR_CALLOC_VOID(ptr, length, location) {\
196 (ptr) = pargemslr::PargemslrCalloc( (size_t)(length), location);\
199 #define PARGEMSLR_CALLOC(ptr, length, location, ...) {\
200 (ptr) = (__VA_ARGS__*) pargemslr::PargemslrCalloc( (size_t)(length)*sizeof(__VA_ARGS__), location);\
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);\
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);\
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);\
215 #define PARGEMSLR_FREE( ptr, location) {\
216 pargemslr::PargemslrFree( (void*)(ptr), location);\
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__();\