-
Notifications
You must be signed in to change notification settings - Fork 3
/
z_zone.c
339 lines (271 loc) · 6.57 KB
/
z_zone.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
/* Z_zone.c */
#include "doomdef.h"
/*
==============================================================================
ZONE MEMORY ALLOCATION
There is never any space between memblocks, and there will never be two
contiguous free memblocks.
The rover can be left pointing at a non-empty block
It is of no value to free a cachable block, because it will get overwritten
automatically if needed
==============================================================================
*/
memzone_t *mainzone;
memzone_t *refzone;
/*
========================
=
= Z_InitZone
=
========================
*/
memzone_t *Z_InitZone (byte *base, int size)
{
memzone_t *zone;
zone = (memzone_t *)base;
zone->size = size;
zone->rover = &zone->blocklist;
zone->blocklist.size = size - 8;
zone->blocklist.user = NULL;
zone->blocklist.tag = 0;
zone->blocklist.id = ZONEID;
zone->blocklist.next = NULL;
zone->blocklist.prev = NULL;
zone->blocklist.lockframe = -1;
return zone;
}
/*
========================
=
= Z_Init
=
========================
*/
void Z_Init (void)
{
byte *mem;
int size;
mem = I_ZoneBase (&size);
#ifdef MARS
/* mars doesn't have a refzone */
mainzone = Z_InitZone (mem,size);
#else
mainzone = Z_InitZone (mem,0x80000);
refzone = Z_InitZone (mem+0x80000,size-0x80000);
#endif
}
/*
========================
=
= Z_Free2
=
========================
*/
void Z_Free2 (memzone_t *mainzone, void *ptr)
{
memblock_t *block;
block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
if (block->id != ZONEID)
I_Error ("Z_Free: freed a pointer without ZONEID");
if (block->user > (void **)0x100) /* smaller values are not pointers */
*block->user = 0; /* clear the user's mark */
block->user = NULL; /* mark as free */
block->tag = 0;
block->id = 0;
}
/*
========================
=
= Z_Malloc2
=
= You can pass a NULL user if the tag is < PU_PURGELEVEL
========================
*/
#define MINFRAGMENT 64
void *Z_Malloc2 (memzone_t *mainzone, int size, int tag, void *user)
{
int extra;
memblock_t *start, *rover, *new, *base;
#if 0
Z_CheckHeap (mainzone); /* DEBUG */
#endif
/* */
/* scan through the block list looking for the first free block */
/* of sufficient size, throwing out any purgable blocks along the way */
/* */
size += sizeof(memblock_t); /* account for size of block header */
size = (size+7)&~7; /* phrase align everything */
start = base = mainzone->rover;
while (base->user || base->size < size)
{
if (base->user)
rover = base;
else
rover = base->next;
if (!rover)
goto backtostart;
if (rover->user)
{
/* hit an in use block, so move base past it */
base = rover->next;
if (!base)
{
backtostart:
base = &mainzone->blocklist;
}
if (base == start) /* scaned all the way around the list */
I_Error ("Z_Malloc: failed on %i",size);
continue;
}
/* */
/* free the rover block (adding the size to base) */
/* */
rover->id = 0;
rover->user = NULL; /* mark as free */
if (base != rover)
{ /* merge with base */
base->size += rover->size;
base->next = rover->next;
if (rover->next)
rover->next->prev = base;
}
}
/* */
/* found a block big enough */
/* */
extra = base->size - size;
if (extra > MINFRAGMENT)
{ /* there will be a free fragment after the allocated block */
new = (memblock_t *) ((byte *)base + size );
new->size = extra;
new->user = NULL; /* free block */
new->tag = 0;
new->prev = base;
new->next = base->next;
if (new->next)
new->next->prev = new;
base->next = new;
base->size = size;
}
if (user)
{
base->user = user; /* mark as an in use block */
*(void **)user = (void *) ((byte *)base + sizeof(memblock_t));
}
else
{
if (tag >= PU_PURGELEVEL)
I_Error ("Z_Malloc: an owner is required for purgable blocks");
base->user = (void *)2; /* mark as in use, but unowned */
}
base->tag = tag;
base->id = ZONEID;
base->lockframe = -1;
mainzone->rover = base->next; /* next allocation will start looking here */
if (!mainzone->rover)
mainzone->rover = &mainzone->blocklist;
return (void *) ((byte *)base + sizeof(memblock_t));
}
/*
========================
=
= Z_FreeTags
=
========================
*/
void Z_FreeTags (memzone_t *mainzone)
{
memblock_t *block, *next;
for (block = &mainzone->blocklist ; block ; block = next)
{
next = block->next; /* get link before freeing */
if (!block->user)
continue; /* free block */
if (block->tag == PU_LEVEL || block->tag == PU_LEVSPEC)
Z_Free2 (mainzone, (byte *)block+sizeof(memblock_t));
}
}
/*
========================
=
= Z_CheckHeap
=
========================
*/
memblock_t *checkblock;
void Z_CheckHeap (memzone_t *mainzone)
{
for (checkblock = &mainzone->blocklist ; checkblock; checkblock = checkblock->next)
{
if (!checkblock->next)
{
if ((byte *)checkblock + checkblock->size - (byte *)mainzone
!= mainzone->size)
I_Error ("Z_CheckHeap: zone size changed\n");
continue;
}
if ( (byte *)checkblock + checkblock->size != (byte *)checkblock->next)
I_Error ("Z_CheckHeap: block size does not touch the next block\n");
if ( checkblock->next->prev != checkblock)
I_Error ("Z_CheckHeap: next block doesn't have proper back link\n");
}
}
/*
========================
=
= Z_ChangeTag
=
========================
*/
void Z_ChangeTag (void *ptr, int tag)
{
memblock_t *block;
block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
if (block->id != ZONEID)
I_Error ("Z_ChangeTag: freed a pointer without ZONEID");
if (tag >= PU_PURGELEVEL && (int)block->user < 0x100)
I_Error ("Z_ChangeTag: an owner is required for purgable blocks");
block->tag = tag;
}
/*
========================
=
= Z_FreeMemory
=
========================
*/
int Z_FreeMemory (memzone_t *mainzone)
{
memblock_t *block;
int free;
free = 0;
for (block = &mainzone->blocklist ; block ; block = block->next)
if (!block->user)
free += block->size;
return free;
}
/*
========================
=
= Z_DumpHeap
=
========================
*/
#ifdef NeXT
void Z_DumpHeap (memzone_t *mainzone)
{
memblock_t *block;
printf ("zone size: %i location: %p\n",mainzone->size,mainzone);
for (block = &mainzone->blocklist ; block ; block = block->next)
{
printf ("block:%p size:%7i user:%p tag:%3i frame:%i\n",
block, block->size, block->user, block->tag,block->lockframe);
if (!block->next)
continue;
if ( (byte *)block + block->size != (byte *)block->next)
printf ("ERROR: block size does not touch the next block\n");
if ( block->next->prev != block)
printf ("ERROR: next block doesn't have proper back link\n");
}
}
#endif