SUMO - Simulation of Urban MObility
debug_new.h
Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 // vim:tabstop=4:shiftwidth=4:expandtab:
00003 #ifdef _MSC_VER
00004 #include <windows_config.h>
00005 #else
00006 #include <config.h>
00007 #endif
00008 #ifdef CHECK_MEMORY_LEAKS
00009 /*
00010  * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
00011  *
00012  * This software is provided 'as-is', without any express or implied
00013  * warranty.  In no event will the authors be held liable for any
00014  * damages arising from the use of this software.
00015  *
00016  * Permission is granted to anyone to use this software for any purpose,
00017  * including commercial applications, and to alter it and redistribute
00018  * it freely, subject to the following restrictions:
00019  *
00020  * 1. The origin of this software must not be misrepresented; you must
00021  *    not claim that you wrote the original software.  If you use this
00022  *    software in a product, an acknowledgement in the product
00023  *    documentation would be appreciated but is not required.
00024  * 2. Altered source versions must be plainly marked as such, and must
00025  *    not be misrepresented as being the original software.
00026  * 3. This notice may not be removed or altered from any source
00027  *    distribution.
00028  *
00029  * This file is part of Stones of Nvwa:
00030  *      http://sourceforge.net/projects/nvwa
00031  *
00032  */
00033 
00044 #ifndef _DEBUG_NEW_H
00045 #define _DEBUG_NEW_H
00046 
00047 #include <new>
00048 #include <stdio.h>
00049 
00061 #ifndef HAVE_PLACEMENT_DELETE
00062 #define HAVE_PLACEMENT_DELETE 1
00063 #endif
00064 
00089 #ifndef _DEBUG_NEW_REDEFINE_NEW
00090 #define _DEBUG_NEW_REDEFINE_NEW 1
00091 #endif
00092 
00093 /* Prototypes */
00094 int check_leaks();
00095 int check_mem_corruption();
00096 void* operator new(size_t size, const char* file, int line);
00097 void* operator new[](size_t size, const char* file, int line);
00098 #if HAVE_PLACEMENT_DELETE
00099 void operator delete(void* pointer, const char* file, int line) throw();
00100 void operator delete[](void* pointer, const char* file, int line) throw();
00101 #endif
00102 #if defined(_MSC_VER) && _MSC_VER < 1300
00103 // MSVC 6 requires the following declarations; or the non-placement
00104 // new[]/delete[] will not compile.
00105 void* operator new[](size_t) throw(std::bad_alloc);
00106 void operator delete[](void*) throw();
00107 #endif
00108 
00109 /* Control variables */
00110 extern bool new_autocheck_flag; // default to true: call check_leaks() on exit
00111 extern bool new_verbose_flag;   // default to false: no verbose information
00112 extern FILE* new_output_fp;     // default to stderr: output to console
00113 extern const char* new_progname;// default to NULL; should be assigned argv[0]
00114 
00123 #define DEBUG_NEW __debug_new_recorder(__FILE__, __LINE__) ->* new
00124 
00125 # if _DEBUG_NEW_REDEFINE_NEW
00126 #   define new DEBUG_NEW
00127 # endif
00128 # ifdef _DEBUG_NEW_EMULATE_MALLOC
00129 #   include <stdlib.h>
00130 #   ifdef new
00131 #     define malloc(s) ((void*)(new char[s]))
00132 #   else
00133 #     define malloc(s) ((void*)(DEBUG_NEW char[s]))
00134 #   endif
00135 #   define free(p) delete[] (char*)(p)
00136 # endif
00137 
00143 class __debug_new_recorder
00144 {
00145     const char* _M_file;
00146     const int   _M_line;
00147     void _M_process(void* pointer);
00148 public:
00153     __debug_new_recorder(const char* file, int line)
00154         : _M_file(file), _M_line(line) {}
00161     template <class _Tp> _Tp* operator->*(_Tp* pointer)
00162     { _M_process(pointer); return pointer; }
00163 private:
00164     __debug_new_recorder(const __debug_new_recorder&);
00165     __debug_new_recorder& operator=(const __debug_new_recorder&);
00166 };
00167 
00174 class __debug_new_counter
00175 {
00176     static int _S_count;
00177 public:
00178     __debug_new_counter();
00179     ~__debug_new_counter();
00180 };
00182 static __debug_new_counter __debug_new_count;
00183 
00184 #endif // _DEBUG_NEW_H
00185 
00186 
00187 #endif
00188 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines