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.