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 
00004 /*
00005  * Copyright (C) 2004-2005 Wu Yongwei <adah at users dot sourceforge dot net>
00006  *
00007  * This software is provided 'as-is', without any express or implied
00008  * warranty.  In no event will the authors be held liable for any
00009  * damages arising from the use of this software.
00010  *
00011  * Permission is granted to anyone to use this software for any purpose,
00012  * including commercial applications, and to alter it and redistribute
00013  * it freely, subject to the following restrictions:
00014  *
00015  * 1. The origin of this software must not be misrepresented; you must
00016  *    not claim that you wrote the original software. If you use this
00017  *    software in a product, an acknowledgment in the product
00018  *    documentation would be appreciated but is not required.
00019  * 2. Altered source versions must be plainly marked as such, and must
00020  *    not be misrepresented as being the original software.
00021  * 3. This notice may not be removed or altered from any source
00022  *    distribution.
00023  *
00024  * This file is part of Stones of Nvwa:
00025  *      http://sourceforge.net/projects/nvwa
00026  *
00027  */
00028 
00039 #ifndef _DEBUG_NEW_H
00040 #define _DEBUG_NEW_H
00041 
00042 #include <new>
00043 #include <stdio.h>
00044 
00056 #ifndef HAS_PLACEMENT_DELETE
00057 #define HAS_PLACEMENT_DELETE 1
00058 #endif
00059 
00084 #ifndef _DEBUG_NEW_REDEFINE_NEW
00085 #define _DEBUG_NEW_REDEFINE_NEW 1
00086 #endif
00087 
00088 /* Prototypes */
00089 int check_leaks();
00090 void* operator new(size_t size, const char* file, int line);
00091 void* operator new[](size_t size, const char* file, int line);
00092 #if HAS_PLACEMENT_DELETE
00093 void operator delete(void* pointer, const char* file, int line) throw();
00094 void operator delete[](void* pointer, const char* file, int line) throw();
00095 #endif
00096 #if defined(_MSC_VER) && _MSC_VER < 1300
00097 // MSVC 6 requires the following declarations; or the non-placement
00098 // new[]/delete[] will not compile.
00099 void* operator new[](size_t) throw(std::bad_alloc);
00100 void operator delete[](void*) throw();
00101 #endif
00102 
00103 /* Control variables */
00104 extern bool new_autocheck_flag; // default to true: call check_leaks() on exit
00105 extern bool new_verbose_flag;   // default to false: no verbose information
00106 extern FILE* new_output_fp;     // default to stderr: output to console
00107 extern const char* new_progname;// default to NULL; should be assigned argv[0]
00108 
00117 #define DEBUG_NEW new(__FILE__, __LINE__)
00118 
00119 # if _DEBUG_NEW_REDEFINE_NEW
00120 #   define new DEBUG_NEW
00121 # endif
00122 # ifdef _DEBUG_NEW_EMULATE_MALLOC
00123 #   include <stdlib.h>
00124 #   ifdef new
00125 #     define malloc(s) ((void*)(new char[s]))
00126 #   else
00127 #     define malloc(s) ((void*)(DEBUG_NEW char[s]))
00128 #   endif
00129 #   define free(p) delete[] (char*)(p)
00130 # endif
00131 
00133 class __debug_new_counter
00134 {
00135     static int _count;
00136 public:
00137     __debug_new_counter();
00138     ~__debug_new_counter();
00139 };
00141 static __debug_new_counter __debug_new_count;
00142 
00143 #endif // _DEBUG_NEW_H

Generated on Wed May 5 00:06:28 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6