SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
debug_new.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 // vim:tabstop=4:shiftwidth=4:expandtab:
3 #ifdef _MSC_VER
4 #include <windows_config.h>
5 #else
6 #include <config.h>
7 #endif
8 #ifdef CHECK_MEMORY_LEAKS
9 /*
10  * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
11  *
12  * This software is provided 'as-is', without any express or implied
13  * warranty. In no event will the authors be held liable for any
14  * damages arising from the use of this software.
15  *
16  * Permission is granted to anyone to use this software for any purpose,
17  * including commercial applications, and to alter it and redistribute
18  * it freely, subject to the following restrictions:
19  *
20  * 1. The origin of this software must not be misrepresented; you must
21  * not claim that you wrote the original software. If you use this
22  * software in a product, an acknowledgement in the product
23  * documentation would be appreciated but is not required.
24  * 2. Altered source versions must be plainly marked as such, and must
25  * not be misrepresented as being the original software.
26  * 3. This notice may not be removed or altered from any source
27  * distribution.
28  *
29  * This file is part of Stones of Nvwa:
30  * http://sourceforge.net/projects/nvwa
31  *
32  */
33 
44 #ifndef _DEBUG_NEW_H
45 #define _DEBUG_NEW_H
46 
47 #include <new>
48 #include <stdio.h>
49 
61 #ifndef HAVE_PLACEMENT_DELETE
62 #define HAVE_PLACEMENT_DELETE 1
63 #endif
64 
89 #ifndef _DEBUG_NEW_REDEFINE_NEW
90 #define _DEBUG_NEW_REDEFINE_NEW 1
91 #endif
92 
93 /* Prototypes */
94 int check_leaks();
95 int check_mem_corruption();
96 void* operator new(size_t size, const char* file, int line);
97 void* operator new[](size_t size, const char* file, int line);
98 #if HAVE_PLACEMENT_DELETE
99 void operator delete(void* pointer, const char* file, int line) throw();
100 void operator delete[](void* pointer, const char* file, int line) throw();
101 #endif
102 #if defined(_MSC_VER) && _MSC_VER < 1300
103 // MSVC 6 requires the following declarations; or the non-placement
104 // new[]/delete[] will not compile.
105 void* operator new[](size_t) throw(std::bad_alloc);
106 void operator delete[](void*) throw();
107 #endif
108 
109 /* Control variables */
110 extern bool new_autocheck_flag; // default to true: call check_leaks() on exit
111 extern bool new_verbose_flag; // default to false: no verbose information
112 extern FILE* new_output_fp; // default to stderr: output to console
113 extern const char* new_progname;// default to NULL; should be assigned argv[0]
114 
123 #define DEBUG_NEW __debug_new_recorder(__FILE__, __LINE__) ->* new
124 
125 # if _DEBUG_NEW_REDEFINE_NEW
126 # define new DEBUG_NEW
127 # endif
128 # ifdef _DEBUG_NEW_EMULATE_MALLOC
129 # include <stdlib.h>
130 # ifdef new
131 # define malloc(s) ((void*)(new char[s]))
132 # else
133 # define malloc(s) ((void*)(DEBUG_NEW char[s]))
134 # endif
135 # define free(p) delete[] (char*)(p)
136 # endif
137 
143 class __debug_new_recorder
144 {
145  const char* _M_file;
146  const int _M_line;
147  void _M_process(void* pointer);
148 public:
153  __debug_new_recorder(const char* file, int line)
154  : _M_file(file), _M_line(line) {}
161  template <class _Tp> _Tp* operator->*(_Tp* pointer)
162  { _M_process(pointer); return pointer; }
163 private:
164  __debug_new_recorder(const __debug_new_recorder&);
165  __debug_new_recorder& operator=(const __debug_new_recorder&);
166 };
167 
174 class __debug_new_counter
175 {
176  static int _S_count;
177 public:
178  __debug_new_counter();
179  ~__debug_new_counter();
180 };
182 static __debug_new_counter __debug_new_count;
183 
184 #endif // _DEBUG_NEW_H
185 
186 
187 #endif
188