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-2007 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 acknowledgement 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 HAVE_PLACEMENT_DELETE
00057 #define HAVE_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 int check_mem_corruption();
00091 void* operator new(size_t size, const char* file, int line);
00092 void* operator new[](size_t size, const char* file, int line);
00093 #if HAVE_PLACEMENT_DELETE
00094 void operator delete(void* pointer, const char* file, int line) throw();
00095 void operator delete[](void* pointer, const char* file, int line) throw();
00096 #endif
00097 #if defined(_MSC_VER) && _MSC_VER < 1300
00098 // MSVC 6 requires the following declarations; or the non-placement
00099 // new[]/delete[] will not compile.
00100 void* operator new[](size_t) throw(std::bad_alloc);
00101 void operator delete[](void*) throw();
00102 #endif
00103 
00104 /* Control variables */
00105 extern bool new_autocheck_flag; // default to true: call check_leaks() on exit
00106 extern bool new_verbose_flag;   // default to false: no verbose information
00107 extern FILE* new_output_fp;     // default to stderr: output to console
00108 extern const char* new_progname;// default to NULL; should be assigned argv[0]
00109 
00118 #define DEBUG_NEW __debug_new_recorder(__FILE__, __LINE__) ->* new
00119 
00120 # if _DEBUG_NEW_REDEFINE_NEW
00121 #   define new DEBUG_NEW
00122 # endif
00123 # ifdef _DEBUG_NEW_EMULATE_MALLOC
00124 #   include <stdlib.h>
00125 #   ifdef new
00126 #     define malloc(s) ((void*)(new char[s]))
00127 #   else
00128 #     define malloc(s) ((void*)(DEBUG_NEW char[s]))
00129 #   endif
00130 #   define free(p) delete[] (char*)(p)
00131 # endif
00132 
00138 class __debug_new_recorder
00139 {
00140     const char* _M_file;
00141     const int   _M_line;
00142     void _M_process(void* pointer);
00143 public:
00148     __debug_new_recorder(const char* file, int line)
00149         : _M_file(file), _M_line(line) {}
00156     template <class _Tp> _Tp* operator->*(_Tp* pointer)
00157     { _M_process(pointer); return pointer; }
00158 private:
00159     __debug_new_recorder(const __debug_new_recorder&);
00160     __debug_new_recorder& operator=(const __debug_new_recorder&);
00161 };
00162 
00169 class __debug_new_counter
00170 {
00171     static int _S_count;
00172 public:
00173     __debug_new_counter();
00174     ~__debug_new_counter();
00175 };
00177 static __debug_new_counter __debug_new_count;
00178 
00179 #endif // _DEBUG_NEW_H

Generated on Mon Dec 31 15:07:24 2007 for Nvwa by  doxygen 1.5.1