-
Notifications
You must be signed in to change notification settings - Fork 1
/
z_zone.h
250 lines (206 loc) · 8.07 KB
/
z_zone.h
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
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2000 James Haley
//
// 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
//
//-----------------------------------------------------------------------------
//
// DESCRIPTION:
// Zone Memory Allocation, perhaps NeXT ObjectiveC inspired.
// Remark: this was the only stuff that, according
// to John Carmack, might have been useful for
// Quake.
//
// Rewritten by Lee Killough, though, since it was not efficient enough.
//
//-----------------------------------------------------------------------------
#ifndef Z_ZONE_H__
#define Z_ZONE_H__
#include "d_keywds.h" // haleyjd 05/22/02
// Remove all definitions before including system definitions
// - haleyjd 10/11/11: eliminated non-portable standards-violating idiom from
// BOOM
// Include system definitions so that prototypes become
// active before macro replacements below are in effect.
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS 1
#endif
#include <assert.h>
#include <errno.h>
#include <float.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef LINUX
// Linux needs strings.h too, for strcasecmp etc.
#include <strings.h>
#endif
#include <sys/stat.h>
#ifndef LINUX
// NON-Linux platforms need sys/types.h
#include <sys/types.h>
#endif
#include <time.h>
// haleyjd: inttypes.h - on Windows, this comes from the source\Win32 directory
#include <inttypes.h>
// haleyjd: C++ headers
#include <new>
#include <type_traits>
// haleyjd: portable replacement function headers
#include "m_ctype.h"
#include "psnprntf.h"
// ZONE MEMORY
// PU - purge tags.
enum
{
PU_FREE, // block is free
PU_STATIC, // block is static (remains until explicitly freed)
PU_PERMANENT, // haleyjd 06/09/12: block cannot be freed or re-tagged
// domain-specific allocation lifetimes
PU_SOUND, // currently unused
PU_MUSIC, // currently unused
PU_RENDERER, // haleyjd 06/29/08: for data allocated via R_Init
PU_VALLOC, // haleyjd 04/29/13: belongs to a video/rendering buffer
PU_AUTO, // haleyjd 07/08/10: automatic allocation
PU_LEVEL, // allocation belongs to level (freed at next level load)
// cache levels
PU_CACHE, // block is cached (may be implicitly freed at any time!)
PU_MAX // Must always be last -- killough
};
#define PU_PURGELEVEL PU_CACHE /* First purgable tag's level */
// killough 3/22/98: add file/line info
void *(Z_Malloc)(size_t size, int tag, void **ptr, const char *, int);
void (Z_Free)(void *ptr, const char *, int);
void (Z_FreeTags)(int lowtag, int hightag, const char *, int);
void (Z_ChangeTag)(void *ptr, int tag, const char *, int);
void Z_Init(void);
void *(Z_Calloc)(size_t n, size_t n2, int tag, void **user, const char *, int);
void *(Z_Realloc)(void *p, size_t n, int tag, void **user, const char *, int);
char *(Z_Strdup)(const char *s, int tag, void **user, const char *, int);
void Z_FreeAlloca(void);
void *(Z_Alloca)(size_t n, const char *file, int line);
void *(Z_Realloca)(void *ptr, size_t n, const char *file, int line);
char *(Z_Strdupa)(const char *s, const char *file, int line);
void (Z_CheckHeap)(const char *, int);
int (Z_CheckTag)(void *, const char *, int);
void *Z_SysMalloc(size_t size);
void *Z_SysCalloc(size_t n1, size_t n2);
void *Z_SysRealloc(void *ptr, size_t size);
void Z_SysFree(void *p);
#define Z_Free(a) (Z_Free) (a, __FILE__,__LINE__)
#define Z_FreeTags(a,b) (Z_FreeTags) (a,b, __FILE__,__LINE__)
#define Z_ChangeTag(a,b) (Z_ChangeTag)(a,b, __FILE__,__LINE__)
#define Z_Malloc(a,b,c) (Z_Malloc) (a,b,c, __FILE__,__LINE__)
#define Z_Strdup(a,b,c) (Z_Strdup) (a,b,c, __FILE__,__LINE__)
#define Z_Calloc(a,b,c,d) (Z_Calloc) (a,b,c,d,__FILE__,__LINE__)
#define Z_Realloc(a,b,c,d) (Z_Realloc) (a,b,c,d,__FILE__,__LINE__)
#define Z_Alloca(a) (Z_Alloca) (a, __FILE__,__LINE__)
#define Z_Realloca(a,b) (Z_Realloca) (a,b, __FILE__,__LINE__)
#define Z_Strdupa(a) (Z_Strdupa) (a, __FILE__,__LINE__)
#define Z_CheckHeap() (Z_CheckHeap)( __FILE__,__LINE__)
#define Z_CheckTag(a) (Z_CheckTag) (a, __FILE__,__LINE__)
#define emalloc(type, n) \
static_cast<type>((Z_Malloc)(n, PU_STATIC, 0, __FILE__, __LINE__))
#define emalloctag(type, n1, tag, user) \
static_cast<type>((Z_Malloc)(n1, tag, user, __FILE__, __LINE__))
#define ecalloc(type, n1, n2) \
static_cast<type>((Z_Calloc)(n1, n2, PU_STATIC, 0, __FILE__, __LINE__))
#define ecalloctag(type, n1, n2, tag, user) \
static_cast<type>((Z_Calloc)(n1, n2, tag, user, __FILE__, __LINE__))
#define erealloc(type, p, n) \
static_cast<type>((Z_Realloc)(p, n, PU_STATIC, 0, __FILE__, __LINE__))
#define estructalloc(type, n) \
static_cast<type *>((Z_Calloc)(n, sizeof(type), PU_STATIC, 0, __FILE__, __LINE__))
#define estructalloctag(type, n, tag) \
static_cast<type *>((Z_Calloc)(n, sizeof(type), tag, 0, __FILE__, __LINE__))
#define estrdup(s) (Z_Strdup)(s, PU_STATIC, 0, __FILE__, __LINE__)
#define efree(p) (Z_Free)(p, __FILE__, __LINE__)
// Get the size of a static array
#define earrlen(a) (sizeof(a) / sizeof(*a))
// Define a struct var and ensure it is fully initialized
#define edefstructvar(type, name) \
type name; \
memset(&name, 0, sizeof(name))
// Doom-style printf
void doom_printf(const char *, ...) __attribute__((format(printf,1,2)));
#ifdef INSTRUMENTED
extern size_t memorybytag[PU_MAX]; // haleyjd 04/01/11
extern int printstats; // killough 08/23/98
#endif
void Z_PrintZoneHeap(void);
void Z_DumpCore(void);
//
// ZoneObject Class
//
// This class serves as a base class for C++ objects that want to support
// allocation on the zone heap.
//
class ZoneObject
{
private:
// static data
static ZoneObject *objectbytag[PU_MAX];
static void *newalloc;
// instance data
void *zonealloc; // If non-null, the object is living on the zone heap
ZoneObject *zonenext; // Next object on tag chain
ZoneObject **zoneprev; // Previous object on tag chain's next pointer
void removeFromTagList();
void addToTagList(int tag);
public:
ZoneObject();
virtual ~ZoneObject();
void *operator new (size_t size);
void *operator new (size_t size, int tag, void **user = NULL);
void operator delete (void *p);
void operator delete (void *p, int, void **);
void changeTag(int tag);
// zone memblock reflection
int getZoneTag() const;
size_t getZoneSize() const;
const void *getBlockPtr() const { return zonealloc; }
static void FreeTags(int lowtag, int hightag);
};
#endif
//----------------------------------------------------------------------------
//
// $Log: z_zone.h,v $
// Revision 1.7 1998/05/08 20:32:12 killough
// fix __attribute__ redefinition
//
// Revision 1.6 1998/05/03 22:38:11 killough
// Remove unnecessary #include
//
// Revision 1.5 1998/04/27 01:49:42 killough
// Add history of malloc/free and scrambler (INSTRUMENTED only)
//
// Revision 1.4 1998/03/23 03:43:54 killough
// Make Z_CheckHeap() more diagnostic
//
// Revision 1.3 1998/02/02 13:28:06 killough
// Add doom_printf
//
// Revision 1.2 1998/01/26 19:28:04 phares
// First rev with no ^Ms
//
// Revision 1.1.1.1 1998/01/19 14:03:06 rand
// Lee's Jan 19 sources
//
//
//----------------------------------------------------------------------------