Heap-dynamic memory allocation for Quake III: Arena game engine


To be a masochist.


Quake III game engine lacks a malloc() function.

Advantages of no malloc():

Disadvantages of no malloc():


The following functions become available to Quake III modifications:

The functions with a return value will return NULL if memory could not be allocated. A common usage is:
char *s;
if (!s = malloc(1024))
    { return 0; }
strcpy(s, "What is six times nine?");
.... (and so on) ...

Programming Notes

This is not true heap memory. A large array of bytes is allocated in the data segment (i.e. a global variable), and then treated as [limited-size] heap memory. One heap-allocated block takes at least 32 bytes. This means with a 256KB heap, only a maximum of about 8000 malloc() calls can be made if no free() calls are ever made.

The heap-memory size (malloc arena) is changeable by altering MALLOC_ARENA in qmalloc.h.

The functions have not been thoroughly stress-tested. I have performed only basic correctness test. In particular, I have not tested these functions when the heap has actually depleted, so borderline cases when heap is almost full may start causing bizarre behavior (such as crashing).

(2002.02.20) The malloc strategy is somewhat saner than the previous version. In short, it's first-fit allocation with fixed-sized pages. Metadata and data are kept in separate arrays to minimize the impact of buffer overruns.


Minor change to speed up subsequent mallocs (by using a last-allocated pointer).
qmalloc3.c, qmalloc.h


This is the second version of malloc that adds alloca:
qmalloc2.c and qmalloc.h (same link as below, but updated file)


Since no files are actually patched per se, the two relevant files are posted as-is:
qmalloc1.c and qmalloc.h

-- PhaethonH (PhaethonH@gmail.com)