3 Memory Allocator: Overview
This tour explores how DOOM
manages memory through a tagging system. Weβll look at lifetime tags, memory block organization, the public interface, and core operations like allocation and freeing.
3.0.1 π File: z_zone.h
(lines 36β44)
Tags define when memory blocks can be automatically freed.
3.0.2 π File: z_zone.h
(lines 47β55)
Memory management API functions for allocation, deallocation, and heap maintenance.
3.0.3 π File: z_zone.h
(lines 58β66)
memblock_t
represents memory blocks in a circular doubly linked list, tracking both allocated and free blocks in the heap.
3.0.4 π File: z_zone.h
(lines 72β77)
Z_ChangeTag
modifies the lifetime tag of an allocated memory block.
3.0.5 π File: z_zone.c
(lines 46β56)
memzone_t
tracks memory allocation state, with mainzone
serving as the global heap instance.
3.0.6 π File: z_zone.c
(lines 67β86)
Z_ClearZone
links the zoneβs blocklist to a single free block spanning all available memory.
3.0.7 π File: z_zone.c
(lines 93β116)
Z_Init
obtains memory from I_ZoneBase
and initializes it as a single free block with blocklist
as the sentinel node.
The next steps examine Z_Malloc
βs allocation process: size adjustment, block scanning, block splitting, and final setup.
3.0.8 π File: z_zone.c
ZONEID
validates memory blocks.
3.0.9 π File: z_zone.c
(line 43)
ZONEID
is a magic number stored in each allocated block to validate it belongs to the zone allocator.
3.0.10 π File: z_zone.c
(lines 195β204)
Aligns memory blocks to 4-byte boundaries and adds space for metadata.
3.0.11 π File: z_zone.c
(lines 223β240)
Blocks with tag β₯ PU_PURGELEVEL
are freed; others are skipped.
3.0.12 π File: z_zone.c
(lines 251β265)
After allocating memory, excess space is split into a new free block.
3.0.13 π File: z_zone.c
(lines 141β147)
When freeing memory blocks, their metadata fields are cleared to indicate the block is now available.
3.0.14 π File: z_zone.c
(lines 161β165)
When freeing memory, adjacent free blocks are combined to prevent fragmentation.
3.0.15 π File: z_zone.c
(lines 304β317)
Z_FreeTags
provides batch memory deallocation based on memory block tags.
3.0.16 π File: z_zone.c
(lines 333β344)
Prints block information within a tag range for debugging.
3.0.17 π File: z_zone.c
(lines 411β419)
Z_CheckHeap
validates block linkage invariants and checks for adjacent free blocks.
3.0.18 π File: g_game.c
(lines 486β488)
ZONEID
is a magic identifier used to validate that memory block pointers refer to real heap blocks.
3.0.19 π File: p_setup.c
(lines 613β614)
A memory allocator that uses tags to manage block lifetimes, enabling flexible memory management through purging and automatic cleanup.