ManaPlus
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 "debug/nvwa/_nvwa.h" // NVWA_NAMESPACE_*
43 #include "debug/nvwa/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 
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:
187 };
188 
196 {
197  static int _S_count;
198 public:
201 };
204 
206 
207 #endif // NVWA_DEBUG_NEW_H
#define NVWA_NAMESPACE_BEGIN
Definition: _nvwa.h:59
#define NVWA_NAMESPACE_END
Definition: _nvwa.h:60
static int _S_count
Definition: debug_new.h:197
debug_new_recorder(const debug_new_recorder &)
const char * _M_file
Definition: debug_new.h:166
_Tp * operator->*(_Tp *ptr)
Definition: debug_new.h:182
const int _M_line
Definition: debug_new.h:167
debug_new_recorder & operator=(const debug_new_recorder &)
debug_new_recorder(const char *file, int line)
Definition: debug_new.h:174
void _M_process(void *ptr)
Definition: debug_new.cpp:904
bool new_autocheck_flag
Definition: debug_new.cpp:333
bool(* leak_whitelist_callback_t)(char const *file, int line, void *addr, void **stacktrace)
Definition: debug_new.h:117
static debug_new_counter __debug_new_count
Definition: debug_new.h:203
void(* stacktrace_print_callback_t)(FILE *fp, void **stacktrace)
Definition: debug_new.h:102
const char * new_progname
Definition: debug_new.cpp:355
bool new_verbose_flag
Definition: debug_new.cpp:338
int check_leaks()
Definition: debug_new.cpp:769
FILE * new_output_fp
Definition: debug_new.cpp:345
leak_whitelist_callback_t leak_whitelist_callback
Definition: debug_new.cpp:368
int check_mem_corruption()
Definition: debug_new.cpp:844
stacktrace_print_callback_t stacktrace_print_callback
Definition: debug_new.cpp:362
#define _NOEXCEPT
Definition: msemstdvector.h:26
int size()
Definition: emotedb.cpp:306