Game Development Reference
In-Depth Information
'
Try not to rely on virtual memory systems. Game consoles typically don
thave
any kind of virtual memory, so you ' re stuck with the amount of memory the sys-
tem gives you. If you allocate one byte more, the system crashes. This can be espe-
cially deadly if you
re allocating and deallocating a lot during runtime because it
will be nearly impossible to determine your peak memory usage for any given
situation.
'
Memory Insurance
When I worked at Planet Moon, we made an educational game for the Gameboy DS called BrainQuest.
The DS only has 4MB of RAM, and toward the end of the project, we were running right up against that
limit. When the final assets came in and were added to the package, we were just over the 4MB limit.
One of the engineers grinned and walked over to his computer. He opened up main.cpp and commented
out the following line:
unsigned char insurance[10240];
Writing Your Own Memory Manager
Most games extend the provided memory management system. The biggest reasons to
do this are performance, efficiency, and improved debugging. Default memory man-
agers in the C runtime are designed to run fairly well in a wide range of memory allo-
cation scenarios. They tend to break down under the load of computer games, where
allocations and deallocations of relatively tiny memory blocks can be fast and furious.
A standard memory manager, like the one in the C runtime, must support multi-
threading. Each time the memory manager
s data structures are accessed or changed,
they must be protected with critical sections, allowing only one thread to allocate or
deallocate memory at a time. All this extra code is time consuming, especially if you
use malloc() and free() very frequently. Most games are multithreaded but
don
'
t necessarily need a multithreaded memory manager for every part of the
game. A single-threaded memory manager that you write yourself might be a good
solution.
Simple memory managers can use a doubly linked list as the basis for keeping track
of allocated and free memory blocks. The C runtime uses a more complicated system
to reduce the algorithmic complexity of searching through the allocated and free
blocks that could be as small as a single byte. Your memory blocks might be either
more regularly shaped, fewer in number, or both. This creates an opportunity to
design a simpler, more efficient system.
Defaultmemorymanagersmustassumethatdeallocations happen approximately
as often as allocations, and they might happen in any order and at any time.
'
 
 
Search WWH ::




Custom Search