Zone memory

The Zone memory system is an internal memory allocator used by Doom for memory management. Due to technology at the time of Doom's release, memory allocation was considered an expensive operation. Due, presumably, to inadequacies of memory management functions available at the time of Doom's development, Doom includes its own memory allocator.

In a normal C program, memory is allocated and deallocated using the C functions  and. The zone memory code contains its own implementations of these,  and. At game startup, a large piece of memory is allocated to the zone memory system, which is then divided up for use by the engine.

The  function differs slightly from the C   function in that it includes support for "tags". Each piece of memory has a particular tag which indicates its purpose. Memory is treated differently according to its tag.

The simplest tag is the PU_STATIC tag which specifies a piece of memory which is deallocated with the   function.

More interesting is the PU_CACHE tag. Memory allocated with this tag may be automatically freed back to the system if it runs out of memory. In this way, caching of WAD file data is possible. It is common, for example, to load data from a WAD resource with the PU_STATIC tag, and when operations on that data have completed, change the tag of the area of memory (using the  function) to PU_CACHE. If the system is low on memory, the data may be freed, but otherwise it may also be kept in memory for future use. The WAD loading code interacts with the zone memory system to provide this.

The other interesting tag is the PU_LEVEL tag. Memory related to the level is allocated with the PU_LEVEL tag. When the level exits, the zone memory is searched and all pieces of memory allocated with this tag are freed. This relieves the burden of having to individually free each piece of memory relating to the level. It is possible this is inspired by the garbage collector provided by Objective C (Doom was developed on NeXT machines which used Objective C, and the original Doom editor was written in it).

The file  in the Doom source code contains an anecdote that John Carmack considered the zone memory system in Doom, "the only stuff that might have been useful for Quake".