SUMO - Simulation of Urban MObility
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 
4 /*
5  * Copyright (C) 2004-2013 Wu Yongwei <adah at users dot sourceforge dot net>
6  *
7  * This software is provided 'as-is', without any express or implied
8  * warranty. In no event will the authors be held liable for any
9  * damages arising from the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute
13  * it freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must
16  * not claim that you wrote the original software. If you use this
17  * software in a product, an acknowledgement in the product
18  * documentation would be appreciated but is not required.
19  * 2. Altered source versions must be plainly marked as such, and must
20  * not be misrepresented as being the original software.
21  * 3. This notice may not be removed or altered from any source
22  * distribution.
23  *
24  * This file is part of Stones of Nvwa:
25  * http://sourceforge.net/projects/nvwa
26  *
27  */
28 
37 #ifndef NVWA_DEBUG_NEW_H
38 #define NVWA_DEBUG_NEW_H
39 
40 #include <new> // size_t/std::bad_alloc
41 #include <stdio.h> // FILE
42 #include "_nvwa.h" // NVWA_NAMESPACE_*
43 #include "c++11.h" // _NOEXCEPT
44 
45 /* Special allocation/deallocation functions in the global scope */
46 void* operator new(size_t size, const char* file, int line);
47 void* operator new[](size_t size, const char* file, int line);
48 void operator delete(void* ptr, const char* file, int line) _NOEXCEPT;
49 void operator delete[](void* ptr, const char* file, int line) _NOEXCEPT;
50 
51 NVWA_NAMESPACE_BEGIN
52 
77 #ifndef _DEBUG_NEW_REDEFINE_NEW
78 #define _DEBUG_NEW_REDEFINE_NEW 1
79 #endif
80 
92 #ifndef _DEBUG_NEW_TYPE
93 #define _DEBUG_NEW_TYPE 1
94 #endif
95 
96 /* Prototypes */
97 int check_leaks();
99 
100 /* Control variables */
101 extern bool new_autocheck_flag; // default to true: call check_leaks() on exit
102 extern bool new_verbose_flag; // default to false: no verbose information
103 extern FILE* new_output_fp; // default to stderr: output to console
104 extern const char* new_progname;// default to NULL; should be assigned argv[0]
105 
114 # if _DEBUG_NEW_TYPE == 1
115 # define DEBUG_NEW NVWA::debug_new_recorder(__FILE__, __LINE__) ->* new
116 # else
117 # define DEBUG_NEW new(__FILE__, __LINE__)
118 # endif
119 
120 # if _DEBUG_NEW_REDEFINE_NEW
121 # define new DEBUG_NEW
122 # endif
123 # ifdef _DEBUG_NEW_EMULATE_MALLOC
124 # include <stdlib.h>
125 # ifdef new
126 # define malloc(s) ((void*)(new char[s]))
127 # else
128 # define malloc(s) ((void*)(DEBUG_NEW char[s]))
129 # endif
130 # define free(p) delete[] (char*)(p)
131 # endif
132 
139 {
140  const char* _M_file;
141  const int _M_line;
142  void _M_process(void* ptr);
143 public:
148  debug_new_recorder(const char* file, int line)
149  : _M_file(file), _M_line(line) {}
156  template <class _Tp> _Tp* operator->*(_Tp* ptr)
157  { _M_process(ptr); return ptr; }
158 private:
161 };
162 
170 {
171  static int _S_count;
172 public:
175 };
178 
179 NVWA_NAMESPACE_END
180 
181 #endif // NVWA_DEBUG_NEW_H
const char * new_progname
_Tp * operator-> *(_Tp *ptr)
Definition: debug_new.h:156
bool new_verbose_flag
const int _M_line
Definition: debug_new.h:141
bool new_autocheck_flag
debug_new_recorder & operator=(const debug_new_recorder &)
const char * _M_file
Definition: debug_new.h:140
debug_new_recorder(const char *file, int line)
Definition: debug_new.h:148
FILE * new_output_fp
void _M_process(void *ptr)
static int _S_count
Definition: debug_new.h:171
int check_mem_corruption()
static debug_new_counter __debug_new_count
Definition: debug_new.h:177
int check_leaks()