Game Engine Architecture

(Ben Green) #1

290 6. Resources and the File System


code. Most other resources can reside in main RAM, but diff erent kinds of
resources might need to reside within diff erent address ranges. For example, a
resource that is loaded and stays resident for the entire game (LSR resources)
might be loaded into one region of memory, while resources that are loaded
and unloaded frequently might go somewhere else.
The design of a game engine’s memory allocation subsystem is usually
closely tied to that of its resource manager. Sometimes we will design the re-
source manager to take best advantage of the types of memory allocators we
have available; or vice-versa, we may design our memory allocators to suit the
needs of the resource manager.
As we saw in Section 5.2.1.4, one of the primary problems facing any re-
source management system is the need to avoid fragmenting memory as re-
sources are loaded and unloaded. We’ll discuss a few of the more-common
solutions to this problem below.
Heap-Based Resource Allocation
One approach is to simply ignore memory fragmentation issues and use a
general-purpose heap allocator to allocate your resources (like the one imple-
mented by malloc() in C, or the global new operator in C++). This works best
if your game is only intended to run on personal computers, on operating
systems that support advanced virtual memory allocation. On such a system,
physical memory will become fragmented, but the operating system’s abil-
ity to map non-contiguous pages of physical RAM into a contiguous virtual
memory space helps to mitigate some of the eff ects of fragmentation.
If your game is running on a console with limited physical RAM and only
a rudimentary virtual memory manager (or none whatsoever), then fragmen-
tation will become a problem. In this case, one alternative is to defragment
your memory periodically. We saw how to do this in Section 5.2.2.2.

Event AB CDE
Initial state 00000
Level 1 counts incremented 11100
Level 1 loads (1) (1) (1) 00
Level 1 plays 11100
Level 2 counts incremented 12211
Level 1 counts decremented 01111
Level 1 unloads, level 2 loads (0) 1 1 (1) (1)
Level 2 plays 0 1111
Table 6.2. Resource usage as two levels load and unload.
Free download pdf