6 #ifndef MEMLEAK_CHECK_HPP
7 #define MEMLEAK_CHECK_HPP
9 typedef unsigned char * byte_ptr;
15 #define VCLUSTER_EVENT 0x2001
17 #define VECTOR_EVENT 0x1102
18 #define VECTOR_STD_EVENT 0x1101
19 #define GRID_EVENT 0x1100
21 #define VECTOR_DIST_EVENT 0x4002
22 #define GRID_DIST_EVENT 0x4001
24 #define HEAPMEMORY_EVENT 0x0100
25 #define CUDAMEMORY_EVENT 0x0101
29 #include "util/se_util.hpp"
30 #include "ptr_info.hpp"
33 #define MEM_ERROR_OBJECT std::runtime_error("Runtime memory error")
35 extern long int msg_on_alloc;
36 extern long int msg_on_dealloc;
37 extern long int thr_on_alloc;
38 extern std::string col_stop;
39 extern long int new_data;
40 extern size_t delete_data;
42 extern std::map<byte_ptr,ptr_info> active_ptr;
44 extern long int process_v_cl;
45 extern long int process_to_print;
56 static bool remove_ptr(
const void * ptr)
59 std::map<byte_ptr, ptr_info>::iterator it = active_ptr.find((byte_ptr)ptr);
62 if ( it == active_ptr.end() )
64 std::cout <<
"Error " << __FILE__ <<
":" << __LINE__ <<
" pointer not found " << ptr <<
"\n";
65 ACTION_ON_ERROR(MEM_ERROR_OBJECT);
72 if (it->second.ref_id == 0)
73 active_ptr.erase((byte_ptr)ptr);
80 #define PRJ_VCLUSTER 2
91 inline static void get_color(
size_t project_id,
size_t size, std::string & col)
98 col = std::string(
"\e[97m");
101 col = std::string(
"\e[95m");
104 col = std::string(
"\e[96m");
107 col = std::string(
"\e[97m");
110 col = std::string(
"\e[93m");
119 col = std::string(
"\e[7;92m");
122 col = std::string(
"\e[7;95m");
125 col = std::string(
"\e[7;96m");
128 col = std::string(
"\e[7;97m");
131 col = std::string(
"\e[7;93m");
143 inline static void get_structure(
size_t struct_id, std::string & str)
148 str = std::string(
"Vcluster");
150 case VECTOR_STD_EVENT:
151 str = std::string(
"Vector_std");
154 str = std::string(
"Vector_native");
157 str = std::string(
"Grid");
159 case VECTOR_DIST_EVENT:
160 str = std::string(
"Vector distributed");
162 case GRID_DIST_EVENT:
163 str = std::string(
"Grid distributed");
165 case HEAPMEMORY_EVENT:
166 str = std::string(
"HeapMemory");
168 case CUDAMEMORY_EVENT:
169 str = std::string(
"CudaMemory");
172 str = std::to_string(struct_id);
183 inline static void get_project(
size_t project_id, std::string & prj)
188 prj = std::string(
"devices");
191 prj = std::string(
"data");
194 prj = std::string(
"vcluster");
197 prj = std::string(
"io");
200 prj = std::string(
"pdata");
210 inline static void print_alloc()
216 for (std::map<byte_ptr,ptr_info>::iterator it = active_ptr.begin(); it != active_ptr.end(); ++it)
218 get_color(it->second.project_id,it->second.size,col);
219 get_structure(it->second.struct_id,sid);
220 get_project(it->second.project_id,prj);
222 std::cout << col <<
"Allocated memory " << (
void *)it->first <<
" size=" << it->second.size <<
" id=" << it->second.id <<
" structure id=" << std::hex << sid << std::dec <<
" project id=" << prj << col_stop <<
"\n";
231 inline static void message_on_alloc(
long int break_id)
233 msg_on_alloc = break_id;
241 inline static void message_on_dealloc(
long int break_id)
243 msg_on_dealloc = break_id;
251 inline static void throw_on_alloc(
long int throw_id)
253 thr_on_alloc = throw_id;
264 inline static bool check_new(
const void * data,
size_t sz,
size_t struct_id,
size_t project_id)
268 ptr_info & ptr = active_ptr[(byte_ptr)data];
271 if (sz > ptr.size) ptr.size = sz;
276 ptr.struct_id = struct_id;
277 ptr.project_id = project_id;
280 #ifdef SE_CLASS2_VERBOSE
281 if (process_to_print < 0 || process_to_print == process_v_cl)
282 std::cout <<
"New data: " << new_data <<
" " << data <<
" " << ptr.size <<
"\n";
285 if (msg_on_alloc == new_data)
286 std::cout <<
"Detected allocation: " << __FILE__ <<
":" << __LINE__ <<
" id=" << msg_on_alloc <<
"\n";
288 if (thr_on_alloc == new_data)
289 throw MEM_ERROR_OBJECT;
302 inline static bool check_delete(
const void * data)
304 if (data == NULL)
return true;
308 if (msg_on_dealloc == (
long int)delete_data)
309 std::cout <<
"Detected destruction: " << __FILE__ <<
":" << __LINE__ <<
" id=" << msg_on_alloc <<
"\n";
311 bool result = remove_ptr(data);
313 #ifdef SE_CLASS2_VERBOSE
314 if (process_to_print < 0 || process_to_print == process_v_cl)
315 std::cout <<
"Delete data: " << delete_data <<
" " << data <<
"\n";
331 inline static bool check_valid(
const void * ptr,
size_t size_access)
333 if (active_ptr.size() == 0)
335 std::cerr <<
"Error invalid pointer: " << __FILE__ <<
":" << __LINE__ <<
" " << ptr <<
"\n";
336 ACTION_ON_ERROR(MEM_ERROR_OBJECT);
342 std::map<byte_ptr, ptr_info>::iterator l_b = active_ptr.upper_bound((byte_ptr)ptr);
345 if (l_b == active_ptr.begin())
347 if (process_to_print < 0 || process_to_print == process_v_cl)
349 std::cerr <<
"Error invalid pointer: " << __FILE__ <<
":" << __LINE__ <<
" " << ptr <<
" base allocation id=" << l_b->second.id <<
"\n";
350 ACTION_ON_ERROR(MEM_ERROR_OBJECT);
359 if (l_b == active_ptr.end())
361 if (process_to_print < 0 || process_to_print == process_v_cl)
363 std::cerr <<
"Error invalid pointer: " << __FILE__ <<
":" << __LINE__ <<
" " << ptr <<
" base allocation id=" << l_b->second.id <<
"\n";
364 ACTION_ON_ERROR(MEM_ERROR_OBJECT);
371 size_t sz = l_b->second.size;
373 if (((
unsigned char *)l_b->first) + sz < ((
unsigned char *)ptr) + size_access)
379 std::map<byte_ptr, ptr_info>::iterator fit = active_ptr.begin();
380 for(; fit != active_ptr.end(); fit++)
382 if (ptr >= fit->first && (((
unsigned char *)ptr) + size_access) <= (((
unsigned char *)fit->first) + fit->second.size) )
391 if (process_to_print < 0 || process_to_print == process_v_cl)
393 std::cerr <<
"Error invalid pointer: " << __FILE__ <<
":" << __LINE__ <<
" " << ptr <<
" base allocation id=" << l_b->second.id <<
"\n";
394 ACTION_ON_ERROR(MEM_ERROR_OBJECT);
413 inline static long int check_whoami(
const void * ptr,
size_t size_access)
415 if (active_ptr.size() == 0)
420 std::map<byte_ptr, ptr_info>::iterator l_b = active_ptr.upper_bound((byte_ptr)ptr);
423 if (l_b == active_ptr.begin())
430 if (l_b == active_ptr.end())
435 size_t sz = l_b->second.size;
437 if (((
unsigned char *)l_b->first) + sz < ((
unsigned char *)ptr) + size_access)
440 return l_b->second.id;
448 inline static void set_process_to_print(
long int p_to_print)
450 process_to_print = p_to_print;