-
Notifications
You must be signed in to change notification settings - Fork 12
/
z_zone.c
393 lines (322 loc) · 7.02 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* 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.tag = 0;
zone->blocklist.id = ZONEID;
zone->blocklist.next = NULL;
zone->blocklist.prev = NULL;
#ifndef MARS
zone->blocklist.lockframe = -1;
#endif
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, *adj;
block = (memblock_t *) ( (byte *)ptr - sizeof(memblock_t));
if (block->id != ZONEID)
I_Error ("Z_Free: freed a pointer without ZONEID");
block->tag = 0;
block->id = 0;
// merge with adjacent blocks
adj = block->prev;
if (adj && !adj->tag)
{
adj->next = block->next;
adj->next->prev = adj;
adj->size += block->size;
if (mainzone->rover == block)
mainzone->rover = adj;
block = adj;
}
adj = block->next;
if (adj && !adj->tag)
{
block->next = adj->next;
if (block->next)
block->next->prev = block;
block->size += adj->size;
if (mainzone->rover == adj)
mainzone->rover = block;
}
}
/*
========================
=
= Z_Malloc2
=
========================
*/
#define MINFRAGMENT 64
void *Z_Malloc2 (memzone_t *mainzone, int size, int tag, boolean err)
{
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->tag || base->size < size)
{
if (base->tag)
rover = base;
else
rover = base->next;
if (!rover)
goto backtostart;
if (rover->tag)
{
/* 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 */
{
if (err)
I_Error("Z_Malloc: failed on %i", size);
return NULL;
}
continue;
}
}
/* */
/* 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->tag = 0; /* free block */
new->prev = base;
new->next = base->next;
if (new->next)
new->next->prev = new;
base->next = new;
base->size = size;
}
base->tag = tag;
base->id = ZONEID;
#ifndef MARS
base->lockframe = -1;
#endif
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->tag)
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");
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->tag)
free += block->size;
return free;
}
/*
========================
=
= Z_LargestFreeBlock
=
========================
*/
int Z_LargestFreeBlock(memzone_t *mainzone)
{
memblock_t *block;
int free;
free = 0;
for (block = &mainzone->blocklist ; block ; block = block->next)
if (!block->tag)
if (block->size > free) free = block->size;
return free;
}
/*
========================
=
= Z_ForEachBlock
=
========================
*/
void Z_ForEachBlock(memzone_t *mainzone, memblockcall_t cb, void *p)
{
memblock_t *block, *next;
for (block = &mainzone->blocklist ; block ; block = next)
{
next = block->next;
if (block->tag)
cb((byte *)block + sizeof(memblock_t), p);
}
}
/*
========================
=
= Z_FreeBlocks
=
========================
*/
int Z_FreeBlocks(memzone_t* mainzone)
{
int total = 0;
memblock_t* block, * next;
for (block = &mainzone->blocklist; block; block = next)
{
next = block->next;
if (!block->tag)
total += block->size;
}
return total;
}
/*
========================
=
= 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 tag:%3i frame:%i\n",
block, block->size, 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