QxOrm  1.2.9
C++ Object Relational Mapping library
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-2008 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 
00040 #ifndef QT_NO_DEBUG
00041 #ifndef _QX_MODE_RELEASE
00042 #if _QX_USE_MEM_LEAK_DETECTION
00043 
00044 #ifndef _DEBUG_NEW_H
00045 #define _DEBUG_NEW_H
00046 
00047 #ifdef _MSC_VER
00048 #pragma once
00049 #endif
00050 
00051 #include <new>
00052 #include <stdio.h>
00053 
00054 #include <QxCommon/QxMacro.h>
00055 
00067 #ifndef HAVE_PLACEMENT_DELETE
00068 #define HAVE_PLACEMENT_DELETE 1
00069 #endif
00070 
00095 #ifndef _DEBUG_NEW_REDEFINE_NEW
00096 #define _DEBUG_NEW_REDEFINE_NEW 1
00097 #endif
00098 
00106 #ifndef _DEBUG_NEW_CALLER_ADDRESS
00107 #ifdef __GNUC__
00108 #define _DEBUG_NEW_CALLER_ADDRESS __builtin_return_address(0)
00109 #else
00110 #define _DEBUG_NEW_CALLER_ADDRESS NULL
00111 #endif
00112 #endif
00113 
00122 #define DEBUG_NEW qx::memory::__debug_new_recorder(__FILE__, __LINE__, __FUNCTION__) ->* new
00123 
00124 #if _DEBUG_NEW_REDEFINE_NEW
00125 #define new DEBUG_NEW
00126 #endif // _DEBUG_NEW_REDEFINE_NEW
00127 
00128 #ifdef _DEBUG_NEW_EMULATE_MALLOC
00129 #include <stdlib.h>
00130 #ifdef new
00131 #define malloc(s) ((void*)(new char[s]))
00132 #else // new
00133 #define malloc(s) ((void*)(DEBUG_NEW char[s]))
00134 #endif // new
00135 #define free(p) delete[] (char*)(p)
00136 #endif // _DEBUG_NEW_EMULATE_MALLOC
00137 
00138 QX_DLL_EXPORT void * operator new(size_t size, const char * file, int line);
00139 QX_DLL_EXPORT void * operator new[](size_t size, const char * file, int line);
00140 
00141 #if HAVE_PLACEMENT_DELETE
00142 QX_DLL_EXPORT void operator delete(void * pointer, const char * file, int line) throw();
00143 QX_DLL_EXPORT void operator delete[](void * pointer, const char * file, int line) throw();
00144 #endif
00145 
00146 #if defined(_MSC_VER) && _MSC_VER < 1300
00147 // MSVC 6 requires the following declarations; or the non-placement
00148 // new[]/delete[] will not compile.
00149 QX_DLL_EXPORT void * operator new[](size_t) throw(std::bad_alloc);
00150 QX_DLL_EXPORT void operator delete[](void *) throw();
00151 #endif
00152 
00153 namespace qx {
00154 namespace memory {
00155 
00156 /* Prototypes */
00157 QX_DLL_EXPORT int check_leaks();
00158 QX_DLL_EXPORT int check_mem_corruption();
00159 
00160 /* Control variables */
00161 extern bool new_autocheck_flag; // default to true: call check_leaks() on exit
00162 extern bool new_verbose_flag;   // default to false: no verbose information
00163 extern FILE* new_output_fp;     // default to stderr: output to console
00164 extern const char* new_progname;// default to NULL; should be assigned argv[0]
00165 
00171 class QX_DLL_EXPORT __debug_new_recorder
00172 {
00173     const char* _M_file;
00174     const int   _M_line;
00175     const char* _M_fct;
00176     void _M_process(void* pointer);
00177 public:
00182     __debug_new_recorder(const char* file, int line, const char* fct)
00183         : _M_file(file), _M_line(line), _M_fct(fct) {}
00190     template <class _Tp> _Tp* operator->*(_Tp* pointer) { _M_process(pointer); return pointer; };
00191     static void free_pointer(void* pointer, void* addr, bool is_array);
00192 private:
00193     __debug_new_recorder(const __debug_new_recorder&);
00194     __debug_new_recorder& operator=(const __debug_new_recorder&);
00195 };
00196 
00203 class QX_DLL_EXPORT __debug_new_counter
00204 {
00205     static int _S_count;
00206 public:
00207     __debug_new_counter();
00208     ~__debug_new_counter();
00209 };
00210 
00212 static __debug_new_counter __debug_new_count;
00213 
00214 } // namespace memory
00215 } // namespace qx
00216 
00217 #endif // _DEBUG_NEW_H
00218 #endif // _QX_USE_MEM_LEAK_DETECTION
00219 #endif // _QX_MODE_RELEASE
00220 #endif // QT_NO_DEBUG