forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcd.h
More file actions
285 lines (231 loc) · 6.56 KB
/
mcd.h
File metadata and controls
285 lines (231 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Softpixel MemCheckDeluxe v1.2.2
* 2003 cwright <cwright@softpixel.com>
*/
/* Copyright (C) 2002, 2003 Softpixel (http://softpixel.com/)
* This file is covered by a BSD-Style License, available in
* the LICENSE file in the root directory of this package, as well as at
* http://prj.softpixel.com/licenses/#bsd
*/
#ifndef MCD_H
#define MCD_H
#include <stdio.h> //for file redirection
#define MCD_VERSION 0x010201
#ifdef __cplusplus
extern "C" {
#endif
#ifndef _MCD_GONE
#ifdef _MCD_CHECK
#include <stdarg.h>
#ifdef WIN32
#define __FUNCTION__ __FILE__
#endif
#ifndef MCD_QUIET
#ifndef WIN32
#warning - Building with memory checking.
#warning expect lower performance. -
#endif
#endif
//warn about redefinitions (sometimes these can be #defined)
#ifdef malloc
#undef malloc
#ifndef WIN32
#warning ------ Redefining malloc() ------
#endif
#endif
#ifdef calloc
#undef calloc
#ifndef WIN32
#warning ------ Redefining calloc() ------
#endif
#endif
#ifdef realloc
#undef realloc
#ifndef WIN32
#warning ------ Redefining realloc() ------
#endif
#endif
#ifdef free
#undef free
#ifndef WIN32
#warning ------ Redefining free() ------
#endif
#endif
#ifdef strdup
#undef strdup
#ifndef WIN32
#warning ------- Redefining strdup() --------
#endif
#endif
#ifdef strndup
#undef strndup
#ifndef WIN32
#warning ------- Redefining strndup() -------
#endif
#endif
#ifdef asprintf
#undef asprintf
#ifndef WIN32
#warning ------ Redefining asprintf() ------
#endif
#endif
#ifdef vasprintf
#undef vasprintf
#ifndef WIN32
#warning ------ Redefining vasprintf() ------
#endif
#endif
#ifdef scanf
#undef scanf
#ifndef WIN32
#warning ------ Redefining scanf() ------
#endif
#endif
#ifdef fscanf
#undef fscanf
#ifndef WIN32
#warning ------ Redefining fscanf() ------
#endif
#endif
#ifdef sscanf
#undef sscanf
#ifndef WIN32
#warning ------ Redefining sscanf() ------
#endif
#endif
#ifdef getcwd
#undef getcwd
#ifndef WIN32
#warning ------ Redefining getcwd() ------
#endif
#endif
#define strdup(p) MCD_strdup(p,__FUNCTION__,__FILE__,__LINE__)
#ifndef WIN32 //no strndup in win32
#define strndup(p,n) MCD_strndup(p,n,__FUNCTION__,__FILE__,__LINE__)
#endif
#define malloc(size) MCD_malloc(size,__FUNCTION__,__FILE__,__LINE__)
#define calloc(n,s) MCD_calloc(s*n,__FUNCTION__,__FILE__,__LINE__)
#define realloc(p,s) MCD_realloc(p,s,__FUNCTION__,__FILE__,__LINE__)
#ifdef _GNU_SOURCE
#define asprintf(p,f,args...) MCD_asprintf(p,f,__FUNCTION__,__FILE__,__LINE__, ## args)
#define vasprintf(p,f,ap) MCD_vasprintf(p,f,ap,__FUNCTION__,__FILE__,__LINE__)
#endif
#ifndef WIN32
/* windows doesn't like variable arguments in #define's */
#ifndef MCD_FASTFREE //fastfree isnt supported here yet
#define scanf(f,args...) MCD_scanf(f,__FUNCTION__,__FILE__,__LINE__, ## args)
#define fscanf(s,f,args...) MCD_fscanf(s,f,__FUNCTION__,__FILE__,__LINE__, ## args)
#define sscanf(s,f,args...) MCD_sscanf(s,f,__FUNCTION__,__FILE__,__LINE__, ## args)
#define getcwd(p,s) MCD_getcwd(p,s,__FUNCTION__,__FILE__,__LINE__)
#endif //fastfree
#endif //win32
#define free(p) MCD_free(p,__FUNCTION__,__FILE__,__LINE__)
#endif // _MCD_CHECK
/* !!! These are called by the defines only. Do NOT use directly. !!! */
void *MCD_malloc(int size, char*, char*, int);
void *MCD_calloc(int size, char*, char*, int);
void *MCD_realloc(void *p, int size, char*, char*, int);
char *MCD_getcwd (char *p, int size, char*, char*, int);
#ifdef __GNUC__
char *MCD_strdup (const char*s, char*, char*, int);
char *MCD_strndup(const char*s, int n, char*, char*, int);
#else
char *MCD_strdup (char*s, char*, char*, int);
char *MCD_strndup(char*s, int n, char*, char*, int);
#endif
int MCD_scanf (const char *fmt, char*fun, char*file, int line,...);
int MCD_fscanf(FILE *stream,const char *fmt,char*fun,char*file,int line,...);
int MCD_sscanf(const char *str,const char *fmt,char*fun,char*file,int line,...);
// Private MCD function, no need to be in public namespace
//void scan_args(const char*fmt,va_list argptr,char*fun,char*file,int line);
#ifdef _GNU_SOURCE
int MCD_asprintf(char **ptr, const char*fmt,char*,char*,int,...);
int MCD_vasprintf(char **ptr, const char*fmt,va_list argptr,char*,char*,int);
#endif // _GNU_SOURCE
void MCD_free(void *p,char*,char*,int);
/* --- call this for memory stats --- */
void showMemStats(void);
/* --- to send realtime stats somewhere other than stderr,
put an opened fp in here --- */
void _MCD_RealTimeLog(FILE*);
/* --- to send showMemStats() somewhere other than stdout,
put an opened fp in here --- */
void _MCD_MemStatLog(FILE*);
#else //MCD is gone
/* define functions so source will compile without modification */
#define showMemStats()
#define _MCD_RealTimeLog(x)
#define _MCD_MemStatLog(x)
#endif //_MCD_GONE
#ifdef __cplusplus
} /* extern "C" */
#endif
#ifndef _MCD_GONE // check again, outside of extern "C" mode
#ifdef __cplusplus /* Some C++ new/delete operator overloading */
#ifndef WIN32
#warning C++ Extentions Enabled
#endif
#ifdef new
#undef new
#endif
#ifdef delete
#undef delete
#endif
extern char *_MCD_LastSetFile,*_MCD_LastSetFun;
extern int _MCD_LastSetLine;
inline void setFileFunLineState(char*file,char*fun,int line)
{
_MCD_LastSetLine=line;
_MCD_LastSetFile=file;
_MCD_LastSetFun=fun;
}
inline void* operator new (unsigned int size,char *file,
char*fun,int line)
{
return MCD_malloc(size,file,fun,line);
}
inline void* operator new[] (unsigned int size,char*file,
char*fun,int line)
{
return MCD_malloc(size,file,fun,line);
}
// currently, passing args to delete operator is not working at all...
#ifndef WIN32 //win32 doesn't like default params to delete
inline void operator delete (void * buf,char*file=__FILE__,
#ifdef WIN32
char*fun=__FILE__,int line=__LINE__)
#else
char*fun=__FUNCTION__,int line=__LINE__)
#endif
{
MCD_free(buf,_MCD_LastSetFile,_MCD_LastSetFun,_MCD_LastSetLine);
}
// ...so we have these here for the day they work, which isn't today.
inline void operator delete[] (void * buf,char*file=__FILE__,
#ifdef WIN32
char*fun=__FILE__,int line=__LINE__)
#else
char*fun=__FUNCTION__,int line=__LINE__)
#endif
{
MCD_free(buf,_MCD_LastSetFile,_MCD_LastSetFun,_MCD_LastSetLine);
}
#endif // win32 default delete params
inline void operator delete (void * buf)
{
MCD_free(buf,0,0,0);
}
inline void operator delete[] (void * buf)
{
MCD_free(buf,0,0,0);
}
#ifdef WIN32
#define new new(__FILE__,__FILE__,__LINE__)
#else
#define new new(__FILE__,__FUNCTION__,__LINE__)
#endif
//#define delete setFileFunLineState(__FILE__,__FUNCTION__,__LINE__);delete
//#define delete delete(__FILE__,__FUNCTION__,__LINE__)
#endif // __cplusplus
#endif // _MCD_GONE
#endif // MCD_H