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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
viDOOM - level editor for DOOM
Copyright (C) 2000 Ian Cowburn (ianc@noddybox.demon.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------
Memory allocation code
*/
static const char rcs_id[]="$Id$";
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "mem.h"
#include "gfx.h"
#include "debug.h"
/* Comment out this define to switch off memory stats in the working version
#define MEMSTAT
*/
#ifdef MEMSTAT
typedef struct
{
Long size;
Long realloc;
Long free;
char file[32];
Long line;
} MemInfo;
static Long grab_call=0;
static Long regrab_call=0;
static Long release_call=0;
static Long copy_call=0;
static Long strdup_call=0;
static Long total=0;
static Long total_grab=0;
static Long total_regrab=0;
static Long total_copy=0;
static Long total_free=0;
static Long total_strdup=0;
static void status(void)
{
Debug(("Total calls to Grab() : %lu\n",grab_call));
Debug(("Total calls to Regrab() : %lu\n",regrab_call));
Debug(("Total calls to Release() : %lu\n",release_call));
Debug(("Total calls to Copy() : %lu\n",copy_call));
Debug(("Total calls to Strdup() : %lu\n",strdup_call));
Debug(("Total bytes allocated : %lu\n",total));
Debug(("Total bytes allocated over life : %lu\n",total_grab));
Debug(("Total bytes regrabbed over life : %lu\n",total_regrab));
Debug(("Total bytes copied over life : %lu\n",total_copy));
Debug(("Total bytes released over life : %lu\n",total_free));
Debug(("Total bytes strdupped over life : %lu\n\n",total_strdup));
}
#endif
#ifdef MEMSTAT
void *FGrab(char *fn, int line, int len)
{
char *ptr;
MemInfo *mi;
if (len==0)
len=1;
if (!(ptr=malloc(len+sizeof(MemInfo))))
{
status();
GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n"
"%s:%d Grab(%d)\n",fn,line,len);
}
mi=(MemInfo *)ptr;
mi->size=len;
mi->realloc=0;
mi->free=0;
ptr+=sizeof(MemInfo);
grab_call++;
total_grab+=len;
total+=len;
memset(ptr,0,len);
return(ptr);
}
#else
void *FGrab(char *fn, int line, int len)
{
void *ptr;
if (len==0)
len=1;
if (!(ptr=malloc(len)))
GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n"
"%s:%d Grab(%d)\n",fn,line,len);
memset(ptr,0,len);
return(ptr);
}
#endif
#ifdef MEMSTAT
void *FReGrab(char *fn, int line, void *ptr, int len)
{
Long old;
MemInfo *mi;
char *p;
if (len==0)
len=1;
p=ptr;
if (p)
p-=sizeof(MemInfo);
if (!(p=realloc(p,len+sizeof(MemInfo))))
{
status();
GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n"
"%s:%d ReGrab(%d)\n",fn,line,len);
}
regrab_call++;
total_regrab+=len;
mi=(MemInfo *)p;
old=mi->size;
mi->size=len;
mi->realloc++;
total-=old;
total+=len;
p+=sizeof(MemInfo);
return(p);
}
#else
void *FReGrab(char *fn, int line, void *ptr, int len)
{
if (len==0)
len=1;
if (!(ptr=realloc(ptr,len)))
GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n"
"%s:%d ReGrab(%d)\n",fn,line,len);
return(ptr);
}
#endif
#ifdef MEMSTAT
void FRelease(char *fn, int line, void *p)
{
char *cp;
MemInfo *mi;
if (!p)
return;
cp=p;
cp-=sizeof(MemInfo);
mi=(MemInfo *)cp;
if (mi->free==1)
Debug(("%p already freed!!! Current=%s:%d, previous=%s:%d\n",
p,fn,line,mi->file,mi->line));
release_call++;
total_free+=mi->size;
total-=mi->size;
strcpy(mi->file,fn);
mi->line=line;
mi->free=1;
free(cp);
}
#else
void FRelease(char *fn, int line, void *p)
{
free(p);
}
#endif
#ifdef MEMSTAT
void *FCopy(char *fn, int line, void *p,int len)
{
char s[128];
void *ptr;
strcpy(s,fn);
strcat(s," [FCopy()]");
if (len==0)
ptr=FGrab(s,line,1);
else
ptr=FGrab(s,line,len);
copy_call++;
total_copy+=len;
if (len)
memcpy(ptr,p,len);
return(ptr);
}
#else
void *FCopy(char *fn, int line, void *p,int len)
{
void *ptr;
if (len==0)
ptr=malloc(1);
else
ptr=malloc(len);
if (!ptr)
GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n"
"%s:%d Copy(%p,%d)\n",fn,line,p,len);
if (len)
memcpy(ptr,p,len);
return(ptr);
}
#endif
#ifdef MEMSTAT
char *FStrdup(char *fn, int line, char *p)
{
char s[128];
char *ptr;
strcpy(s,fn);
strcat(s," [Strdup()]");
if (p)
{
ptr=FGrab(s,line,strlen(p)+1);
strdup_call++;
total_strdup+=strlen(p)+1;
strcpy(ptr,p);
return(ptr);
}
else
return(NULL);
}
#else
char *FStrdup(char *fn, int line, char *p)
{
char *ptr;
if (p)
{
if (!(ptr=malloc(strlen(p)+1)))
GFX_exit(EXIT_FAILURE,"Memory allocation failed!\n"
"%s:%d Stdup(%s)\n",fn,line,p);
strcpy(ptr,p);
return(ptr);
}
else
return(NULL);
}
#endif
/* END OF FILE */
|