-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathz_native.cpp
1233 lines (1037 loc) · 30 KB
/
z_native.cpp
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2009 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:
//
// Native implementation of the zone API. This doesn't have a lot of advantage
// over the zone heap during normal play, but it shines when the game is
// under stress, whereas the zone heap chokes doing an O(N) search over the
// block list and wasting time dumping purgables, causing unnecessary disk
// thrashing.
//
// When running with this heap, there is no limitation to the amount of memory
// allocated except what the system will provide.
//
// Limitations:
// * Purgables are never currently dumped unless the machine runs out of RAM.
// * Instrumentation cannot track the amount of free memory.
// * Heap check is limited to a zone ID check.
// * Core dump function is not supported.
//
//-----------------------------------------------------------------------------
#include "z_zone.h"
#include "i_system.h"
#include "doomtype.h"
//#include "doomstat.h"
#include "m_argv.h"
//=============================================================================
//
// Macros
//
// Uncomment this to see real-time memory allocation statistics.
//#define INSTRUMENTED
// Uncomment this to use memory scrambling on allocations and frees.
// haleyjd 01/27/09: made independent of INSTRUMENTED
//#define ZONESCRAMBLE
// Uncomment this to exhaustively run memory checks
// while the game is running (this is EXTREMELY slow).
// haleyjd 01/27/09: made independent of INSTRUMENTED
//#define CHECKHEAP
// Uncomment this to perform id checks on zone blocks,
// to detect corrupted and illegally freed blocks
//#define ZONEIDCHECK
// Uncomment this to dump the heap to file on exit.
//#define DUMPONEXIT
// Uncomment this to log all memory operations to a file
//#define ZONEFILE
// Uncomment this to enable more verbose - but dangerous - error messages
// that could cause crashes
//#define ZONEVERBOSE
//=============================================================================
//
// Tunables
//
// signature for block header
#define ZONEID 0x931d4a11
// End Tunables
//=============================================================================
//
// Memblock Structure
//
typedef struct memblock
{
#ifdef ZONEIDCHECK
unsigned int id;
#endif
struct memblock *next,**prev;
size_t size;
void **user;
unsigned char tag;
#ifdef INSTRUMENTED
const char *file;
int line;
#endif
} memblock_t;
//=============================================================================
//
// Heap Globals
//
// haleyjd 03/08/10: dynamically calculated header size;
// round sizeof(memblock_t) up to nearest 16-byte boundary. This should work
// just about everywhere, and keeps the assumption of a 32-byte header on
// 32-bit. 64-bit will use a 64-byte header.
static const size_t header_size = (sizeof(memblock_t) + 15) & ~15;
static memblock_t *blockbytag[PU_MAX]; // used for tracking all zone blocks
// ZoneObject class statics
ZoneObject *ZoneObject::objectbytag[PU_MAX]; // like blockbytag but for objects
void *ZoneObject::newalloc; // most recent ZoneObject alloc
//=============================================================================
//
// Debug Macros
//
// haleyjd 11/18/09
// These help clean up the #ifdef hell.
//
// Instrumentation macros
#ifdef INSTRUMENTED
#define INSTRUMENT(a) a
#define INSTRUMENT_IF(opt, a, b) if((opt)) (a); else (b)
#else
#define INSTRUMENT(a)
#define INSTRUMENT_IF(opt, a, b)
#endif
// ID Check macros
#ifdef ZONEIDCHECK
#define IDCHECK(a) a
#define IDBOOL(a) (a)
//
// Z_IDCheckNB
//
// Performs a fatal error condition check contingent on the definition
// of ZONEIDCHECK, in any context where a memblock pointer is not available.
//
static void Z_IDCheckNB(bool err, const char *errmsg,
const char *file, int line)
{
if(err)
I_FatalError(I_ERR_KILL, "%s\nSource: %s:%d\n", errmsg, file, line);
}
//
// Z_IDCheck
//
// Performs a fatal error condition check contingent on the definition
// of ZONEIDCHECK, and accepts a memblock pointer for provision of additional
// malloc source information available when INSTRUMENTED is also defined.
//
static void Z_IDCheck(bool err, const char *errmsg,
memblock_t *block, const char *file, int line)
{
if(err)
{
I_FatalError(I_ERR_KILL,
"%s\nSource: %s:%d\nSource of malloc: %s:%d\n",
errmsg, file, line,
#if defined(ZONEVERBOSE) && defined(INSTRUMENTED)
block->file, block->line
#else
"(not available)", 0
#endif
);
}
}
#else
#define IDCHECK(a)
#define IDBOOL(a) false
#define Z_IDCheckNB(err, errmsg, file, line)
#define Z_IDCheck(err, errmsg, block, file, line)
#endif
// Heap checking macro
#ifdef CHECKHEAP
#define DEBUG_CHECKHEAP() Z_CheckHeap()
#else
#define DEBUG_CHECKHEAP()
#endif
// Zone scrambling macro
#ifdef ZONESCRAMBLE
#define SCRAMBLER(b, s) memset((b), 1 | (gametic & 0xff), (s))
#else
#define SCRAMBLER(b, s)
#endif
//=============================================================================
//
// Instrumentation Statistics
//
// statistics for evaluating performance
INSTRUMENT(size_t memorybytag[PU_MAX]); // haleyjd 04/02/11: track by tag
INSTRUMENT(int printstats = 0); // killough 8/23/98
// haleyjd 04/02/11: Instrumentation output has been moved to d_main.cpp and
// is now drawn directly to the screen instead of passing through doom_printf.
// haleyjd 06/20/09: removed unused, crashy, and non-useful Z_DumpHistory
//=============================================================================
//
// Zone Log File
//
// haleyjd 09/16/06
//
#ifdef ZONEFILE
static FILE *zonelog;
static bool logclosed;
static void Z_OpenLogFile()
{
if(!zonelog && !logclosed)
zonelog = fopen("zonelog.txt", "w");
}
#endif
static void Z_CloseLogFile()
{
#ifdef ZONEFILE
if(zonelog)
{
fputs("Closing zone log", zonelog);
fclose(zonelog);
zonelog = NULL;
}
// Do not open a new log after this point.
logclosed = true;
#endif
}
static void Z_LogPrintf(const char *msg, ...)
{
#ifdef ZONEFILE
if(!zonelog)
Z_OpenLogFile();
if(zonelog)
{
va_list ap;
va_start(ap, msg);
vfprintf(zonelog, msg, ap);
va_end(ap);
// flush after every message
fflush(zonelog);
}
#endif
}
static void Z_LogPuts(const char *msg)
{
#ifdef ZONEFILE
if(!zonelog)
Z_OpenLogFile();
if(zonelog)
fputs(msg, zonelog);
#endif
}
//=============================================================================
//
// Initialization and Shutdown
//
static void Z_Close(void)
{
Z_CloseLogFile();
#ifdef DUMPONEXIT
Z_PrintZoneHeap();
#endif
}
void Z_Init(void)
{
atexit(Z_Close); // exit handler
Z_LogPrintf("Initialized zone heap (using native implementation)\n");
}
//=============================================================================
//
// Core Memory Management Routines
//
//
// Z_Malloc
//
// You can pass a NULL user if the tag is < PU_PURGELEVEL.
//
void *(Z_Malloc)(size_t size, int tag, void **user, const char *file, int line)
{
register memblock_t *block;
byte *ret;
DEBUG_CHECKHEAP();
Z_IDCheckNB(IDBOOL(tag >= PU_PURGELEVEL && !user),
"Z_Malloc: an owner is required for purgable blocks",
file, line);
if(!size)
return user ? *user = NULL : NULL; // malloc(0) returns NULL
if(!(block = (memblock_t *)(malloc(size + header_size))))
{
if(blockbytag[PU_CACHE])
{
Z_FreeTags(PU_CACHE, PU_CACHE);
block = (memblock_t *)(malloc(size + header_size));
}
}
if(!block)
{
I_FatalError(I_ERR_KILL, "Z_Malloc: Failure trying to allocate %u bytes\n"
"Source: %s:%d\n", (unsigned int)size, file, line);
}
block->size = size;
if((block->next = blockbytag[tag]))
block->next->prev = &block->next;
blockbytag[tag] = block;
block->prev = &blockbytag[tag];
INSTRUMENT(memorybytag[tag] += block->size);
INSTRUMENT(block->file = file);
INSTRUMENT(block->line = line);
IDCHECK(block->id = ZONEID); // signature required in block header
block->tag = tag; // tag
block->user = user; // user
ret = ((byte *) block + header_size);
if(user) // if there is a user
*user = ret; // set user to point to new block
// scramble memory -- weed out any bugs
SCRAMBLER(ret, size);
Z_LogPrintf("* %p = Z_Malloc(size=%lu, tag=%d, user=%p, source=%s:%d)\n",
ret, size, tag, user, file, line);
return ret;
}
//
// Z_Free
//
void (Z_Free)(void *p, const char *file, int line)
{
DEBUG_CHECKHEAP();
if(p)
{
memblock_t *block = (memblock_t *)((byte *) p - header_size);
Z_IDCheck(IDBOOL(block->id != ZONEID),
"Z_Free: freed a pointer without ZONEID", block, file, line);
// haleyjd: permanent blocks are never freed even if the code tries.
if(block->tag == PU_PERMANENT)
return;
IDCHECK(block->id = 0); // Nullify id so another free fails
// haleyjd 01/20/09: check invalid tags
// catches double frees and possible selective heap corruption
if(block->tag == PU_FREE || block->tag >= PU_MAX)
{
I_FatalError(I_ERR_KILL,
"Z_Free: freed a pointer with invalid tag %d\n"
"Source: %s:%d\n"
#if defined(ZONEVERBOSE) && defined(INSTRUMENTED)
"Source of malloc: %s:%d\n"
, block->tag, file, line, block->file, block->line
#else
, block->tag, file, line
#endif
);
}
INSTRUMENT(memorybytag[block->tag] -= block->size);
block->tag = PU_FREE; // Mark block freed
// scramble memory -- weed out any bugs
SCRAMBLER(p, block->size);
if(block->user) // Nullify user if one exists
*block->user = NULL;
if((*block->prev = block->next))
block->next->prev = block->prev;
free(block);
Z_LogPrintf("* Z_Free(p=%p, file=%s:%d)\n", p, file, line);
}
}
//
// Z_FreeTags
//
void (Z_FreeTags)(int lowtag, int hightag, const char *file, int line)
{
memblock_t *block;
// haleyjd 03/30/2011: delete ZoneObjects of the same tags as well
ZoneObject::FreeTags(lowtag, hightag);
if(lowtag <= PU_FREE)
lowtag = PU_FREE+1;
if(hightag > PU_CACHE)
hightag = PU_CACHE;
for(; lowtag <= hightag; ++lowtag)
{
for(block = blockbytag[lowtag], blockbytag[lowtag] = NULL; block;)
{
memblock_t *next = block->next;
Z_IDCheck(IDBOOL(block->id != ZONEID),
"Z_FreeTags: Changed a tag without ZONEID",
block, file, line);
(Z_Free)((byte *)block + header_size, file, line);
block = next; // Advance to next block
}
}
Z_LogPrintf("* Z_FreeTags(lowtag=%d, hightag=%d, file=%s:%d)\n",
lowtag, hightag, file, line);
}
//
// Z_ChangeTag
//
void (Z_ChangeTag)(void *ptr, int tag, const char *file, int line)
{
memblock_t *block;
DEBUG_CHECKHEAP();
if(!ptr)
{
I_FatalError(I_ERR_KILL,
"Z_ChangeTag: can't change a NULL pointer at %s:%d\n",
file, line);
}
block = (memblock_t *)((byte *) ptr - header_size);
Z_IDCheck(IDBOOL(block->id != ZONEID),
"Z_ChangeTag: Changed a tag without ZONEID", block, file, line);
Z_IDCheck(IDBOOL(tag >= PU_PURGELEVEL && !block->user),
"Z_ChangeTag: an owner is required for purgable blocks",
block, file, line);
// haleyjd: permanent blocks are not re-tagged even if the code tries.
if(block->tag == PU_PERMANENT)
return;
if((*block->prev = block->next))
block->next->prev = block->prev;
if((block->next = blockbytag[tag]))
block->next->prev = &block->next;
block->prev = &blockbytag[tag];
blockbytag[tag] = block;
INSTRUMENT(memorybytag[block->tag] -= block->size);
INSTRUMENT(memorybytag[tag] += block->size);
block->tag = tag;
Z_LogPrintf("* Z_ChangeTag(p=%p, tag=%d, file=%s:%d)\n",
ptr, tag, file, line);
}
//
// Z_Realloc
//
// For the native heap, this can easily behave as a real realloc, and not
// just an ignorant copy-and-free.
//
void *(Z_Realloc)(void *ptr, size_t n, int tag, void **user,
const char *file, int line)
{
void *p;
memblock_t *block, *newblock, *origblock;
// if not allocated at all, defer to Z_Malloc
if(!ptr)
return (Z_Malloc)(n, tag, user, file, line);
// size == 0 is a special case that cannot be handled below
if(n == 0)
{
(Z_Free)(ptr, file, line);
return NULL;
}
DEBUG_CHECKHEAP();
block = origblock = (memblock_t *)((byte *)ptr - header_size);
Z_IDCheck(IDBOOL(block->id != ZONEID),
"Z_Realloc: Reallocated a block without ZONEID\n",
block, file, line);
// haleyjd: realloc cannot change the tag of a permanent block
if(block->tag == PU_PERMANENT)
tag = PU_PERMANENT;
// nullify current user, if any
if(block->user)
*(block->user) = NULL;
// detach from list before reallocation
if((*block->prev = block->next))
block->next->prev = block->prev;
block->next = NULL;
block->prev = NULL;
INSTRUMENT(memorybytag[block->tag] -= block->size);
if(!(newblock = (memblock_t *)(realloc(block, n + header_size))))
{
// haleyjd 07/09/10: Note that unlinking the block above makes this safe
// even if the current block is PU_CACHE; Z_FreeTags won't find it.
if(blockbytag[PU_CACHE])
{
Z_FreeTags(PU_CACHE, PU_CACHE);
newblock = (memblock_t *)(realloc(block, n + header_size));
}
}
if(!(block = newblock))
{
if(origblock->size >= n)
{
block = origblock; // restore original block if size was equal or smaller
n = block->size; // keep same size in this event
}
else
{
I_FatalError(I_ERR_KILL, "Z_Realloc: Failure trying to allocate %u bytes\n"
"Source: %s:%d\n", (unsigned int)n, file, line);
}
}
block->size = n;
block->tag = tag;
p = (byte *)block + header_size;
// set new user, if any
block->user = user;
if(user)
*user = p;
// reattach to list at possibly new address, new tag
if((block->next = blockbytag[tag]))
block->next->prev = &block->next;
blockbytag[tag] = block;
block->prev = &blockbytag[tag];
INSTRUMENT(memorybytag[tag] += block->size);
INSTRUMENT(block->file = file);
INSTRUMENT(block->line = line);
Z_LogPrintf("* %p = Z_Realloc(ptr=%p, n=%lu, tag=%d, user=%p, source=%s:%d)\n",
p, ptr, n, tag, user, file, line);
return p;
}
//
// Z_Calloc
//
void *(Z_Calloc)(size_t n1, size_t n2, int tag, void **user,
const char *file, int line)
{
return (n1*=n2) ? memset((Z_Malloc)(n1, tag, user, file, line), 0, n1) : NULL;
}
//
// Z_Strdup
//
char *(Z_Strdup)(const char *s, int tag, void **user,
const char *file, int line)
{
return strcpy((char *)((Z_Malloc)(strlen(s)+1, tag, user, file, line)), s);
}
//=============================================================================
//
// Heap Verification
//
//
// Z_CheckHeap
//
void (Z_CheckHeap)(const char *file, int line)
{
#ifdef ZONEIDCHECK
memblock_t *block;
int lowtag;
for(lowtag = PU_FREE+1; lowtag < PU_MAX; ++lowtag)
{
for(block = blockbytag[lowtag]; block; block = block->next)
{
Z_IDCheck(IDBOOL(block->id != ZONEID),
"Z_CheckHeap: Block found without ZONEID",
block, file, line);
}
}
#endif
#ifndef CHECKHEAP
Z_LogPrintf("* Z_CheckHeap(file=%s:%d)\n", file, line);
#endif
}
//
// Z_CheckTag
//
// haleyjd: a function to return the allocation tag of a block.
// This is needed by W_CacheLumpNum so that it does not
// inadvertently lower the cache level of lump allocations and
// cause code which expects them to be static to lose them
//
int (Z_CheckTag)(void *ptr, const char *file, int line)
{
memblock_t *block = (memblock_t *)((byte *) ptr - header_size);
DEBUG_CHECKHEAP();
Z_IDCheck(IDBOOL(block->id != ZONEID),
"Z_CheckTag: block doesn't have ZONEID", block, file, line);
return block->tag;
}
//
// Z_PrintZoneHeap
//
void Z_PrintZoneHeap(void)
{
memblock_t *block;
int lowtag;
FILE *outfile;
const char *fmtstr =
#if defined(ZONEIDCHECK) && defined(INSTRUMENTED)
"%p: { %8X : %p : %p : %8u : %p : %d : %s : %d }\n"
#elif defined(INSTRUMENTED)
"%p: { %p : %p : %8u : %p : %d : %s : %d }\n"
#elif defined(ZONEIDCHECK)
"%p: { %8X : %p : %p : %8u : %p : %d }\n"
#else
"%p: { %p : %p : %8u : %p : %d }\n"
#endif
;
outfile = fopen("heap.txt", "w");
if(!outfile)
return;
for(lowtag = PU_FREE; lowtag < PU_MAX; ++lowtag)
{
for(block = blockbytag[lowtag]; block; block = block->next)
{
fprintf(outfile, fmtstr, block,
#if defined(ZONEIDCHECK)
block->id,
#endif
block->next, block->prev, block->size,
block->user, block->tag
#if defined(INSTRUMENTED)
#if defined(ZONEVERBOSE)
, block->file, block->line
#else
, "not printed", 0
#endif
#endif
);
// warnings
#if defined(ZONEIDCHECK)
if(block->tag != PU_FREE && block->id != ZONEID)
fputs("\tWARNING: block does not have ZONEID\n", outfile);
#endif
if(!block->user && block->tag >= PU_PURGELEVEL)
fputs("\tWARNING: purgable block with no user\n", outfile);
if(block->tag >= PU_MAX)
fputs("\tWARNING: invalid cache level\n", outfile);
fflush(outfile);
}
}
fclose(outfile);
}
//
// Z_DumpCore
//
// haleyjd 03/18/07: Write the zone heap to file
//
void Z_DumpCore()
{
static const char *namefortag[PU_MAX] =
{
"PU_FREE",
"PU_STATIC",
"PU_PERMANENT",
"PU_SOUND",
"PU_MUSIC",
"PU_RENDERER",
"PU_VALLOC",
"PU_AUTO",
"PU_LEVEL",
"PU_CACHE",
};
int tag;
memblock_t *block;
uint32_t dirofs = 12;
uint32_t dirlen;
uint32_t numentries = 0;
for(tag = PU_FREE+1; tag < PU_MAX; tag++)
{
for(block = blockbytag[tag]; block; block = block->next)
++numentries;
}
dirlen = numentries * 64; // crazy PAK format...
FILE *f = fopen("coredump.pak", "wb");
if(!f)
return;
fwrite("PACK", 4, 1, f);
fwrite(&dirofs, sizeof(dirofs), 1, f);
fwrite(&dirlen, sizeof(dirlen), 1, f);
uint32_t offs = 12 + 64 * numentries;
for(tag = PU_FREE+1; tag < PU_MAX; tag++)
{
for(block = blockbytag[tag]; block; block = block->next)
{
char name[56];
uint32_t filepos = offs;
uint32_t filelen = (uint32_t)(block->size);
memset(name, 0, sizeof(name));
sprintf(name, "/%s/%p",
block->tag < PU_MAX ? namefortag[block->tag] : "UNKNOWN",
block);
fwrite(name, sizeof(name), 1, f);
fwrite(&filepos, sizeof(filepos), 1, f);
fwrite(&filelen, sizeof(filelen), 1, f);
offs += filelen;
}
}
for(tag = PU_FREE+1; tag < PU_MAX; tag++)
{
for(block = blockbytag[tag]; block; block = block->next)
fwrite(((byte *)block + header_size), block->size, 1, f);
}
fclose(f);
}
//=============================================================================
//
// System Allocator Functions
//
// Guaranteed access to system malloc and free, regardless of the heap in use.
//
//
// Z_SysMalloc
//
// Similar to I_AllocLow in the original source, this function gives explicit
// access to the C heap. There are allocations which are a detriment to the zone
// system, such as large video buffers, which should be handled through this
// function instead.
//
// Care must be taken, of course, to not mix zone and C heap allocations.
//
void *Z_SysMalloc(size_t size)
{
void *ret;
if(!(ret = malloc(size)))
{
I_FatalError(I_ERR_KILL,
"Z_SysMalloc: failed on allocation of %u bytes\n",
(unsigned int)size);
}
return ret;
}
//
// Z_SysCalloc
//
// Convenience routine to match above.
//
void *Z_SysCalloc(size_t n1, size_t n2)
{
void *ret;
if(!(ret = calloc(n1, n2)))
{
I_FatalError(I_ERR_KILL,
"Z_SysCalloc: failed on allocation of %u bytes\n",
(unsigned int)(n1*n2));
}
return ret;
}
//
// Z_SysRealloc
//
// Turns out I need this in the sound code to avoid possible multithreaded
// race conditions.
//
void *Z_SysRealloc(void *ptr, size_t size)
{
void *ret;
if(!(ret = realloc(ptr, size)))
{
I_FatalError(I_ERR_KILL,
"Z_SysRealloc: failed on allocation of %u bytes\n",
(unsigned int)size);
}
return ret;
}
//
// Z_SysFree
//
// For use with Z_SysMalloc.
//
void Z_SysFree(void *p)
{
if(p)
free(p);
}
//=============================================================================
//
// Zone Alloca
//
// haleyjd 12/06/06
//
//
// Z_FreeAlloca
//
// haleyjd 12/06/06: Frees all blocks allocated with Z_Alloca.
//
void Z_FreeAlloca(void)
{
memblock_t *block = blockbytag[PU_AUTO];
if(!block)
return;
Z_LogPuts("* Freeing alloca blocks\n");
blockbytag[PU_AUTO] = NULL;
while(block)
{
memblock_t *next = block->next;
Z_IDCheckNB(IDBOOL(block->id != ZONEID),
"Z_FreeAlloca: Freed a tag without ZONEID",
__FILE__, __LINE__);
Z_Free((byte *)block + header_size);
block = next; // Advance to next block
}
}
//
// Z_Alloca
//
// haleyjd 12/06/06:
// Implements a portable garbage-collected alloca on the zone heap.
//
void *(Z_Alloca)(size_t n, const char *file, int line)
{
void *ptr;
if(n == 0)
return NULL;
// allocate it
ptr = (Z_Calloc)(n, 1, PU_AUTO, NULL, file, line);
Z_LogPrintf("* %p = Z_Alloca(n = %lu, file = %s, line = %d)\n",
ptr, n, file, line);
return ptr;
}
//
// Z_Realloca
//
// haleyjd 07/08/10: realloc for automatic allocations.
//
void *(Z_Realloca)(void *ptr, size_t n, const char *file, int line)
{
void *ret;
if(ptr)
{
// get zone block
memblock_t *block = (memblock_t *)((byte *)ptr - header_size);
Z_IDCheck(IDBOOL(block->id != ZONEID),
"Z_Realloca: block found without ZONEID", block, file, line);
if(block->tag != PU_AUTO)
I_FatalError(I_ERR_KILL, "Z_Realloca: strange block tag %d\n", block->tag);
}
ret = (Z_Realloc)(ptr, n, PU_AUTO, NULL, file, line);
Z_LogPrintf("* %p = Z_Realloca(ptr = %p, n = %lu, file = %s, line = %d)\n",
ret, ptr, n, file, line);
return ret;
}
//
// Z_Strdupa
//
// haleyjd 05/07/08: strdup that uses alloca, for convenience.
//
char *(Z_Strdupa)(const char *s, const char *file, int line)
{
return strcpy((char *)((Z_Alloca)(strlen(s)+1, file, line)), s);
}
//=============================================================================
//
// ZoneObject class methods
//
//
// ZoneObject::operator new
//
// Heap allocation new for ZoneObject, which calls Z_Calloc.
//
void *ZoneObject::operator new (size_t size)
{
return (newalloc = Z_Calloc(1, size, PU_STATIC, NULL));
}
//
// ZoneObject::operator new
//
// Overload supporting full zone allocation semantics.