Nvwa  1.1
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-2015 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 
102 typedef void (*stacktrace_print_callback_t)(FILE* fp, void** stacktrace);
103 
117 typedef bool (*leak_whitelist_callback_t)(char const* file, int line,
118  void* addr, void** stacktrace);
119 
120 /* Prototypes */
121 int check_leaks();
123 
124 /* Control variables */
125 extern bool new_autocheck_flag; // default to true: call check_leaks() on exit
126 extern bool new_verbose_flag; // default to false: no verbose information
127 extern FILE* new_output_fp; // default to stderr: output to console
128 extern const char* new_progname;// default to null; should be assigned argv[0]
130 extern leak_whitelist_callback_t leak_whitelist_callback; // default to null
131 
140 # if _DEBUG_NEW_TYPE == 1
141 # define DEBUG_NEW NVWA::debug_new_recorder(__FILE__, __LINE__) ->* new
142 # else
143 # define DEBUG_NEW new(__FILE__, __LINE__)
144 # endif
145 
146 # if _DEBUG_NEW_REDEFINE_NEW
147 # define new DEBUG_NEW
148 # endif
149 # ifdef _DEBUG_NEW_EMULATE_MALLOC
150 # include <stdlib.h>
151 # ifdef new
152 # define malloc(s) ((void*)(new char[s]))
153 # else
154 # define malloc(s) ((void*)(DEBUG_NEW char[s]))
155 # endif
156 # define free(p) delete[] (char*)(p)
157 # endif
158 
165 {
166  const char* _M_file;
167  const int _M_line;
168  void _M_process(void* ptr);
169 public:
174  debug_new_recorder(const char* file, int line)
175  : _M_file(file), _M_line(line) {}
182  template <class _Tp> _Tp* operator->*(_Tp* ptr)
183  { _M_process(ptr); return ptr; }
184 private:
186  debug_new_recorder& operator=(const debug_new_recorder&);
187 };
188 
196 {
197  static int _S_count;
198 public:
201 };
204 
205 NVWA_NAMESPACE_END
206 
207 #endif // NVWA_DEBUG_NEW_H
Recorder class to remember the call context.
Definition: debug_new.h:164
int check_leaks()
Checks for memory leaks.
Definition: debug_new.cpp:761
bool new_autocheck_flag
Flag to control whether nvwa::check_leaks will be automatically called on program exit...
Definition: debug_new.cpp:325
stacktrace_print_callback_t stacktrace_print_callback
Pointer to the callback used to print the stack backtrace in case of a memory problem.
Definition: debug_new.cpp:354
int check_mem_corruption()
Checks for heap corruption.
Definition: debug_new.cpp:836
static debug_new_counter __debug_new_count
Counting object for each file including debug_new.h.
Definition: debug_new.h:203
Counter class for on-exit leakage check.
Definition: debug_new.h:195
const char * new_progname
Pointer to the program name.
Definition: debug_new.cpp:347
debug_new_recorder(const char *file, int line)
Constructor to remember the call context.
Definition: debug_new.h:174
leak_whitelist_callback_t leak_whitelist_callback
Pointer to the callback used to filter out false positives from leak reports.
Definition: debug_new.cpp:360
bool new_verbose_flag
Flag to control whether verbose messages are output.
Definition: debug_new.cpp:330
C++11 feature detection macros and workarounds.
static int _S_count
Count of source files that use debug_new.
Definition: debug_new.h:197
void(* stacktrace_print_callback_t)(FILE *fp, void **stacktrace)
Callback type for stack trace printing.
Definition: debug_new.h:102
Common definitions for preprocessing.
FILE * new_output_fp
Pointer to the output stream.
Definition: debug_new.cpp:337
bool(* leak_whitelist_callback_t)(char const *file, int line, void *addr, void **stacktrace)
Callback type for the leak whitelist function.
Definition: debug_new.h:117