debug_new.h Source File
Back to the index.
src
include
thirdparty
debug_new.h
Go to the documentation of this file.
1
/*
2
* Modified for GXemul:
3
*
4
* 1. Better Doxygen comment for class __debug_new_counter.
5
*/
6
7
8
// -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
9
// vim:tabstop=4:shiftwidth=4:expandtab:
10
11
/*
12
* Copyright (C) 2004-2005 Wu Yongwei <adah at users dot sourceforge dot net>
13
*
14
* This software is provided 'as-is', without any express or implied
15
* warranty. In no event will the authors be held liable for any
16
* damages arising from the use of this software.
17
*
18
* Permission is granted to anyone to use this software for any purpose,
19
* including commercial applications, and to alter it and redistribute
20
* it freely, subject to the following restrictions:
21
*
22
* 1. The origin of this software must not be misrepresented; you must
23
* not claim that you wrote the original software. If you use this
24
* software in a product, an acknowledgment in the product
25
* documentation would be appreciated but is not required.
26
* 2. Altered source versions must be plainly marked as such, and must
27
* not be misrepresented as being the original software.
28
* 3. This notice may not be removed or altered from any source
29
* distribution.
30
*
31
* This file is part of Stones of Nvwa:
32
* http://sourceforge.net/projects/nvwa
33
*
34
*/
35
36
/**
37
* @file debug_new.h
38
*
39
* Header file for checking leaks caused by unmatched new/delete.
40
*
41
* @version 3.4, 2005/09/13
42
* @author Wu Yongwei
43
*
44
*/
45
46
#ifndef _DEBUG_NEW_H
47
#define _DEBUG_NEW_H
48
49
#include <new>
50
#include <stdio.h>
51
52
/**
53
* @def HAS_PLACEMENT_DELETE
54
*
55
* Macro to indicate whether placement delete operators are supported on
56
* a certain compiler. Some compilers, like Borland C++ Compiler 5.5.1
57
* and Digital Mars Compiler 8.42, do not support them, and the user
58
* must define this macro to \c 0 to make the program compile. Also
59
* note that in that case memory leakage will occur if an exception is
60
* thrown in the initialization (constructor) of a dynamically created
61
* object.
62
*/
63
#ifndef HAS_PLACEMENT_DELETE
64
#define HAS_PLACEMENT_DELETE 1
65
#endif
66
67
/**
68
* @def _DEBUG_NEW_REDEFINE_NEW
69
*
70
* Macro to indicate whether redefinition of \c new is wanted. If one
71
* wants to define one's own <code>operator new</code>, to call
72
* <code>operator new</code> directly, or to call placement \c new, it
73
* should be defined to \c 0 to alter the default behaviour. Unless, of
74
* course, one is willing to take the trouble to write something like:
75
* @code
76
* # ifdef new
77
* # define _NEW_REDEFINED
78
* # undef new
79
* # endif
80
*
81
* // Code that uses new is here
82
*
83
* # ifdef _NEW_REDEFINED
84
* # ifdef DEBUG_NEW
85
* # define new DEBUG_NEW
86
* # endif
87
* # undef _NEW_REDEFINED
88
* # endif
89
* @endcode
90
*/
91
#ifndef _DEBUG_NEW_REDEFINE_NEW
92
#define _DEBUG_NEW_REDEFINE_NEW 1
93
#endif
94
95
/* Prototypes */
96
int
check_leaks
();
97
void
*
operator
new
(
size_t
size,
const
char
* file,
int
line);
98
void
*
operator
new
[](
size_t
size,
const
char
* file,
int
line);
99
#if HAS_PLACEMENT_DELETE
100
void
operator
delete
(
void
* pointer,
const
char
* file,
int
line)
throw
();
101
void
operator
delete
[](
void
* pointer,
const
char
* file,
int
line)
throw
();
102
#endif
103
#if defined(_MSC_VER) && _MSC_VER < 1300
104
// MSVC 6 requires the following declarations; or the non-placement
105
// new[]/delete[] will not compile.
106
void
*
operator
new
[](size_t)
throw
(std::bad_alloc);
107
void
operator
delete
[](
void
*)
throw
();
108
#endif
109
110
/* Control variables */
111
extern
bool
new_autocheck_flag
;
// default to true: call check_leaks() on exit
112
extern
bool
new_verbose_flag
;
// default to false: no verbose information
113
extern
FILE*
new_output_fp
;
// default to stderr: output to console
114
extern
const
char
*
new_progname
;
// default to NULL; should be assigned argv[0]
115
116
/**
117
* @def DEBUG_NEW
118
*
119
* The macro to catch file/line information on allocation. If
120
* #_DEBUG_NEW_REDEFINE_NEW is not defined, one can use this macro
121
* directly; otherwise \c new will be defined to it, and one must use
122
* \c new instead.
123
*/
124
#define DEBUG_NEW new(__FILE__, __LINE__)
125
126
# if _DEBUG_NEW_REDEFINE_NEW
127
# define new DEBUG_NEW
128
# endif
129
# ifdef _DEBUG_NEW_EMULATE_MALLOC
130
# include <stdlib.h>
131
# ifdef new
132
# define malloc(s) ((void*)(new char[s]))
133
# else
134
# define malloc(s) ((void*)(DEBUG_NEW char[s]))
135
# endif
136
# define free(p) delete[] (char*)(p)
137
# endif
138
139
/**
140
* \brief Part of Wu Yongwei's new/delete debug
141
* memory leak detector.
142
*
143
* Counter class for on-exit leakage check.
144
*/
145
class
__debug_new_counter
146
{
147
static
int
_count;
148
public
:
149
__debug_new_counter
();
150
~__debug_new_counter
();
151
};
152
/** Counting object for each file including debug_new.h. */
153
static
__debug_new_counter
__debug_new_count;
154
155
#endif // _DEBUG_NEW_H
check_leaks
int check_leaks()
Definition:
debug_new.cc:497
__debug_new_counter::~__debug_new_counter
~__debug_new_counter()
Definition:
debug_new.cc:716
new_progname
const char * new_progname
Definition:
debug_new.cc:283
__debug_new_counter
Part of Wu Yongwei's new/delete debug memory leak detector.
Definition:
debug_new.h:146
new_autocheck_flag
bool new_autocheck_flag
Definition:
debug_new.cc:261
new_output_fp
FILE * new_output_fp
Definition:
debug_new.cc:273
new_verbose_flag
bool new_verbose_flag
Definition:
debug_new.cc:266
__debug_new_counter::__debug_new_counter
__debug_new_counter()
Definition:
debug_new.cc:707
Generated on Tue Aug 25 2020 19:25:06 for GXemul by
1.8.18