-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathz_zone.c
536 lines (425 loc) · 13.9 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id:$
//
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 2023-2024 by Frenkel Smeijers
//
// This source is available for distribution and/or modification
// only under the terms of the DOOM Source Code License as
// published by id Software. All rights reserved.
//
// The source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
// for more details.
//
// $Log:$
//
// DESCRIPTION:
// Zone Memory Allocation. Neat.
//
//-----------------------------------------------------------------------------
#include <stdint.h>
#include <stdlib.h>
#include "compiler.h"
#include "z_zone.h"
#include "doomdef.h"
#include "i_system.h"
//
// ZONE MEMORY
// PU - purge tags.
// Tags < 100 are not overwritten until freed.
#define PU_STATIC 1 // static entire execution time
#define PU_LEVEL 2 // static until level exited
#define PU_LEVSPEC 3 // a special thinker in a level
#define PU_CACHE 4
#define PU_PURGELEVEL PU_CACHE
//
// 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.
//
#if defined INSTRUMENTED
static int32_t running_count = 0;
#endif
#define ZONEID 0x1dea
typedef struct
{
#if SIZE_OF_SEGMENT_T == 2
uint32_t size; // including the header and possibly tiny fragments
uint16_t tag; // purgelevel
#else
uint32_t size:24; // including the header and possibly tiny fragments
uint32_t tag:4; // purgelevel
#endif
void __far*__far* user; // NULL if a free block
segment_t next;
segment_t prev;
#if defined ZONEIDCHECK
uint16_t id; // should be ZONEID
#endif
} memblock_t;
typedef char assertMemblockSize[sizeof(memblock_t) <= PARAGRAPH_SIZE ? 1 : -1];
static memblock_t __far* mainzone_sentinal;
static segment_t mainzone_rover_segment;
static segment_t pointerToSegment(const memblock_t __far* ptr)
{
#if defined RANGECHECK
if ((((uint32_t) ptr) & (PARAGRAPH_SIZE - 1)) != 0)
I_Error("pointerToSegment: pointer is not aligned: 0x%lx", ptr);
#endif
return D_FP_SEG(ptr);
}
static memblock_t __far* segmentToPointer(segment_t seg)
{
return D_MK_FP(seg, 0);
}
//
// Z_Init
//
void Z_Init (void)
{
// allocate all available conventional memory.
uint32_t heapSize;
segment_t segment = I_ZoneBase(&heapSize);
static uint8_t __far* mainzone; mainzone = D_MK_FP(segment, 0);
// align blocklist
uint_fast8_t i = 0;
static uint8_t __far mainzone_sentinal_buffer[PARAGRAPH_SIZE * 2];
uint32_t b = (uint32_t) &mainzone_sentinal_buffer[i++];
while ((b & (PARAGRAPH_SIZE - 1)) != 0)
b = (uint32_t) &mainzone_sentinal_buffer[i++];
mainzone_sentinal = (memblock_t __far*)b;
#if defined __WATCOMC__ && defined _M_I86
// normalize pointer
mainzone_sentinal = D_MK_FP(D_FP_SEG(mainzone_sentinal) + D_FP_OFF(mainzone_sentinal) / PARAGRAPH_SIZE, 0);
#endif
// set the entire zone to one free block
memblock_t __far* block = (memblock_t __far*)mainzone;
mainzone_rover_segment = pointerToSegment(block);
mainzone_sentinal->tag = PU_STATIC;
mainzone_sentinal->user = (void __far*)mainzone;
mainzone_sentinal->next = mainzone_rover_segment;
mainzone_sentinal->prev = mainzone_rover_segment;
block->size = heapSize;
block->tag = 0;
block->user = NULL; // NULL indicates a free block.
block->prev = pointerToSegment(mainzone_sentinal);
block->next = block->prev;
#if defined ZONEIDCHECK
block->id = ZONEID;
#endif
uint32_t addMemSize;
segment_t addsegment = I_ZoneAdditional(&addMemSize);
if (addMemSize)
{
segment_t romblock_segment = mainzone_rover_segment + heapSize / PARAGRAPH_SIZE - 1;
memblock_t __far* romblock = segmentToPointer(romblock_segment);
romblock->size = (uint32_t)(addsegment - romblock_segment) * PARAGRAPH_SIZE;
romblock->tag = PU_STATIC;
romblock->user = (void __far*)mainzone;
romblock->next = addsegment;
romblock->prev = mainzone_rover_segment;
#if defined ZONEIDCHECK
romblock->id = ZONEID;
#endif
memblock_t __far* addblock = segmentToPointer(addsegment);
addblock->size = addMemSize;
addblock->tag = 0;
addblock->user = NULL; // NULL indicates a free block.
addblock->next = block->next; // == pointerToSegment(mainzone_sentinal)
addblock->prev = romblock_segment;
#if defined ZONEIDCHECK
addblock->id = ZONEID;
#endif
block->size -= PARAGRAPH_SIZE;
block->next = romblock_segment;
}
}
static void Z_ChangeTag(const void __far* ptr, uint_fast8_t tag)
{
#if defined RANGECHECK
if ((((uint32_t) ptr) & (PARAGRAPH_SIZE - 1)) != 0)
I_Error("Z_ChangeTag: pointer is not aligned: 0x%lx", ptr);
#endif
#if defined _M_I86
memblock_t __far* block = (memblock_t __far*)(((uint32_t)ptr) - 0x00010000);
#else
memblock_t __far* block = (memblock_t __far*)(((uint32_t)ptr) - 0x00010);
#endif
#if defined ZONEIDCHECK
if (block->id != ZONEID)
I_Error("Z_ChangeTag: block has id %x instead of ZONEID", block->id);
#endif
block->tag = tag;
}
void Z_ChangeTagToStatic(const void __far* ptr)
{
Z_ChangeTag(ptr, PU_STATIC);
}
void Z_ChangeTagToCache(const void __far* ptr)
{
Z_ChangeTag(ptr, PU_CACHE);
}
static void Z_FreeBlock(memblock_t __far* block)
{
#if defined ZONEIDCHECK
if (block->id != ZONEID)
I_Error("Z_FreeBlock: block has id %x instead of ZONEID", block->id);
#endif
if (D_FP_SEG(block->user) != 0)
{
// far pointers with segment 0 are not user pointers
// Note: OS-dependend
// clear the user's mark
*block->user = NULL;
}
// mark as free
block->user = NULL;
block->tag = 0;
#if defined INSTRUMENTED
running_count -= block->size;
printf("Free: %ld\n", running_count);
#endif
memblock_t __far* other = segmentToPointer(block->prev);
if (!other->user)
{
// merge with previous free block
other->size += block->size;
other->next = block->next;
segmentToPointer(other->next)->prev = block->prev; // == pointerToSegment(other);
if (pointerToSegment(block) == mainzone_rover_segment)
mainzone_rover_segment = block->prev; // == pointerToSegment(other);
block = other;
}
other = segmentToPointer(block->next);
if (!other->user)
{
// merge the next free block onto the end
block->size += other->size;
block->next = other->next;
segmentToPointer(block->next)->prev = pointerToSegment(block);
if (pointerToSegment(other) == mainzone_rover_segment)
mainzone_rover_segment = pointerToSegment(block);
}
}
//
// Z_Free
//
void Z_Free (const void __far* ptr)
{
#if defined RANGECHECK
if ((((uint32_t) ptr) & (PARAGRAPH_SIZE - 1)) != 0)
I_Error("Z_Free: pointer is not aligned: 0x%lx", ptr);
#endif
#if defined _M_I86
memblock_t __far* block = (memblock_t __far*)(((uint32_t)ptr) - 0x00010000);
#else
memblock_t __far* block = (memblock_t __far*)(((uint32_t)ptr) - 0x00010);
#endif
Z_FreeBlock(block);
}
uint32_t Z_GetLargestFreeBlockSize(void)
{
uint32_t largestFreeBlockSize = 0;
segment_t mainzone_sentinal_segment = pointerToSegment(mainzone_sentinal);
for (memblock_t __far* block = segmentToPointer(mainzone_sentinal->next); pointerToSegment(block) != mainzone_sentinal_segment; block = segmentToPointer(block->next))
if (!block->user && block->size > largestFreeBlockSize)
largestFreeBlockSize = block->size;
return largestFreeBlockSize;
}
static uint32_t Z_GetTotalFreeMemory(void)
{
uint32_t totalFreeMemory = 0;
segment_t mainzone_sentinal_segment = pointerToSegment(mainzone_sentinal);
for (memblock_t __far* block = segmentToPointer(mainzone_sentinal->next); pointerToSegment(block) != mainzone_sentinal_segment; block = segmentToPointer(block->next))
if (!block->user)
totalFreeMemory += block->size;
return totalFreeMemory;
}
//
// Z_TryMalloc
// You can pass a NULL user if the tag is < PU_PURGELEVEL.
// Because Z_TryMalloc is static, we can control the input and we can make sure tag is always < PU_PURGELEVEL.
//
#define MINFRAGMENT 64
static void __far* Z_TryMalloc(uint16_t size, int8_t tag, void __far*__far* user)
{
size = (size + (PARAGRAPH_SIZE - 1)) & ~(PARAGRAPH_SIZE - 1);
// scan through the block list,
// looking for the first free block
// of sufficient size,
// throwing out any purgable blocks along the way.
// account for size of block header
size += PARAGRAPH_SIZE;
// if there is a free block behind the rover,
// back up over them
memblock_t __far* base = segmentToPointer(mainzone_rover_segment);
memblock_t __far* previous_block = segmentToPointer(base->prev);
if (!previous_block->user)
base = previous_block;
memblock_t __far* rover = base;
segment_t start_segment = base->prev;
do
{
if (pointerToSegment(rover) == start_segment)
{
// scanned all the way around the list
return NULL;
}
if (rover->user)
{
if (rover->tag < PU_PURGELEVEL)
{
// hit a block that can't be purged,
// so move base past it
base = rover = segmentToPointer(rover->next);
}
else
{
// free the rover block (adding the size to base)
// the rover can be the base block
base = segmentToPointer(base->prev);
Z_FreeBlock(rover);
base = segmentToPointer(base->next);
rover = segmentToPointer(base->next);
}
}
else
rover = segmentToPointer(rover->next);
} while (base->user || base->size < size);
// found a block big enough
int32_t newblock_size = base->size - size;
if (newblock_size > MINFRAGMENT)
{
// there will be a free fragment after the allocated block
segment_t base_segment = pointerToSegment(base);
segment_t newblock_segment = base_segment + (size / PARAGRAPH_SIZE);
memblock_t __far* newblock = segmentToPointer(newblock_segment);
newblock->size = newblock_size;
newblock->tag = 0;
newblock->user = NULL; // NULL indicates free block.
newblock->next = base->next;
newblock->prev = base_segment;
#if defined ZONEIDCHECK
newblock->id = ZONEID;
#endif
segmentToPointer(base->next)->prev = newblock_segment;
base->size = size;
base->next = newblock_segment;
}
base->tag = tag;
if (user)
base->user = user;
else
base->user = (void __far*__far*) D_MK_FP(0,2); // unowned
#if defined ZONEIDCHECK
base->id = ZONEID;
#endif
// next allocation will start looking here
mainzone_rover_segment = base->next;
#if defined INSTRUMENTED
running_count += base->size;
printf("Alloc: %ld (%ld)\n", base->size, running_count);
#endif
#if defined _M_I86
memblock_t __far* block = (memblock_t __far*)(((uint32_t)base) + 0x00010000);
#else
memblock_t __far* block = (memblock_t __far*)(((uint32_t)base) + 0x00010);
#endif
return block;
}
static void __far* Z_Malloc(uint16_t size, int8_t tag, void __far*__far* user) {
void __far* ptr = Z_TryMalloc(size, tag, user);
if (!ptr)
I_Error ("Z_Malloc: failed to allocate %u B, max free block %li B, total free %li", size, Z_GetLargestFreeBlockSize(), Z_GetTotalFreeMemory());
return ptr;
}
static void __far* Z_TryMallocStatic(uint16_t size)
{
return Z_TryMalloc(size, PU_STATIC, NULL);
}
void __far* Z_MallocStatic(uint16_t size)
{
return Z_Malloc(size, PU_STATIC, NULL);
}
void __far* Z_MallocStaticWithUser(uint16_t size, void __far*__far* user)
{
return Z_Malloc(size, PU_STATIC, user);
}
void __far* Z_MallocLevel(uint16_t size, void __far*__far* user)
{
return Z_Malloc(size, PU_LEVEL, user);
}
void __far* Z_CallocLevel(uint16_t size)
{
void __far* ptr = Z_Malloc(size, PU_LEVEL, NULL);
_fmemset(ptr, 0, size);
return ptr;
}
void __far* Z_CallocLevSpec(uint16_t size)
{
void __far* ptr = Z_Malloc(size, PU_LEVSPEC, NULL);
_fmemset(ptr, 0, size);
return ptr;
}
boolean Z_IsEnoughFreeMemory(uint16_t size)
{
const uint8_t __far* ptr = Z_TryMallocStatic(size);
if (ptr)
{
Z_Free(ptr);
return true;
} else
return false;
}
//
// Z_FreeTags
//
void Z_FreeTags(void)
{
memblock_t __far* next;
segment_t mainzone_sentinal_segment = pointerToSegment(mainzone_sentinal);
for (memblock_t __far* block = segmentToPointer(mainzone_sentinal->next); pointerToSegment(block) != mainzone_sentinal_segment; block = next)
{
// get link before freeing
next = segmentToPointer(block->next);
// already a free block?
if (!block->user)
continue;
if (PU_LEVEL <= block->tag && block->tag <= (PU_PURGELEVEL - 1))
Z_FreeBlock(block);
}
}
//
// Z_CheckHeap
//
void Z_CheckHeap (void)
{
segment_t mainzone_sentinal_segment = pointerToSegment(mainzone_sentinal);
for (memblock_t __far* block = segmentToPointer(mainzone_sentinal->next); ; block = segmentToPointer(block->next))
{
if (block->next == mainzone_sentinal_segment)
{
// all blocks have been hit
break;
}
#if defined ZONEIDCHECK
if (block->id != ZONEID)
I_Error("Z_CheckHeap: block has id %x instead of ZONEID", block->id);
#endif
if (pointerToSegment(block) + (block->size / PARAGRAPH_SIZE) != block->next)
I_Error ("Z_CheckHeap: block size does not touch the next block\n");
if (segmentToPointer(block->next)->prev != pointerToSegment(block))
I_Error ("Z_CheckHeap: next block doesn't have proper back link\n");
if (!block->user && !segmentToPointer(block->next)->user)
I_Error ("Z_CheckHeap: two consecutive free blocks\n");
}
}