Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
MemoryChecker.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef NDEBUG
22 
23 #ifndef MEMORYCHECKER_ON
24 #define MEMORYCHECKER_ON
25 
26 #include <execinfo.h>
27 #include <cstdlib>
28 #include <iostream>
29 
30 void memchecker_insert_stack(void* ptr,char** strings, size_t size);
31 void memchecker_remove_stack(void* ptr);
32 void memory_checker_print_report();
33 void memory_checker_clear_report();
34 
35 inline void* operator new(size_t size) throw(std::bad_alloc) {
36  void *ptr = (void *)malloc(size);
37 
38  void* stack_array[10];
39  size_t stack_size = backtrace(stack_array,10);
40  char** stack_data = backtrace_symbols(stack_array,stack_size);
41  memchecker_insert_stack(ptr,stack_data,stack_size);
42 
43  return ptr;
44 }
45 
46 inline void* operator new[](size_t size) throw(std::bad_alloc) {
47  void *ptr = (void *)malloc(size);
48 
49  void* stack_array[10];
50  size_t stack_size = backtrace(stack_array,10);
51  char** stack_data = backtrace_symbols(stack_array,stack_size);
52  memchecker_insert_stack(ptr,stack_data,stack_size);
53 
54  return ptr;
55 }
56 
57 inline void operator delete(void *ptr) throw() {
58  memchecker_remove_stack(ptr);
59  free(ptr);
60 }
61 
62 inline void operator delete[](void *ptr) throw() {
63  memchecker_remove_stack(ptr);
64  free(ptr);
65 }
66 
67 
68 #endif // MEMORYCHECKER_ON
69 #endif // NDEBUG
70 ///@endcond