Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/MemoryChecker.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 ///@cond DOXYGEN_HIDDEN
00020 
00021 #if !defined(NDEBUG) && !defined(__EMSCRIPTEN__)
00022 
00023 #ifndef MEMORYCHECKER_ON
00024 #define MEMORYCHECKER_ON
00025 
00026 #include <execinfo.h>
00027 #include <cstdlib>
00028 #include <iostream>
00029 
00030 void memchecker_insert_stack(void* ptr,char** strings, size_t size);
00031 void memchecker_remove_stack(void* ptr);
00032 void memory_checker_print_report();
00033 void memory_checker_clear_report();
00034 
00035 inline void* operator new(size_t size) throw(std::bad_alloc) {
00036   void *ptr = (void *)malloc(size);
00037 
00038   void* stack_array[10];
00039   size_t stack_size = backtrace(stack_array,10);
00040   char** stack_data = backtrace_symbols(stack_array,stack_size);
00041   memchecker_insert_stack(ptr,stack_data,stack_size);
00042 
00043   return ptr;
00044 }
00045 
00046 inline void* operator new[](size_t size) throw(std::bad_alloc) {
00047   void *ptr = (void *)malloc(size);
00048 
00049   void* stack_array[10];
00050   size_t stack_size = backtrace(stack_array,10);
00051   char** stack_data = backtrace_symbols(stack_array,stack_size);
00052   memchecker_insert_stack(ptr,stack_data,stack_size);
00053 
00054   return ptr;
00055 }
00056 
00057 inline void operator delete(void *ptr) throw() {
00058   memchecker_remove_stack(ptr);
00059   free(ptr);
00060 }
00061 
00062 inline void operator delete[](void *ptr) throw() {
00063   memchecker_remove_stack(ptr);
00064   free(ptr);
00065 }
00066 
00067 
00068 #endif // MEMORYCHECKER_ON
00069 #endif // NDEBUG
00070 ///@endcond
 All Classes Files Functions Variables Enumerations Enumerator Properties