Pool Allocator

Another useful memory algorithm is a pool allocator, which allocates lots of small blocks of memory of the same size.  Often times in programming games (and other applications) there are times where you need to allocate small chunks for same-sized items such as matrices, or multiple instances of the same object.  Obviously a pool allocator is the natural fit.  My implementation is below. 

--> Open PoolAllocator.cpp in a new window <--

The code above is hosten on Google Code, and you can comment by double clicking anywhere, or by clicking in the left hand margin on the little speech balloon plus icon. You need to be signed in with a google account.  Please comment and tell me if my code sucks!

Implementing a stack allocator isn't hard, but there is one important memory saving trick I'll discuss below:

  1. When creating the pool, preallocate a large block of memory, the size of which is a multiple of the size of the separate pool elements.
  2. Add each of the free elements to a linked list.
  3. When someone wants to use a block, pop one off the free list and return it to the user.
  4. When a block is freed, add it back to the free list.
  5. When deleting the pool, assert that the number of free elements is equal to the pool size divided by the size of an individual element.

That part is easy.  But we have to consider where we will get memory for the linked list to track the free elements.  Or do we?  We have all this free space in each of the free elements, why not put it to good use?

NOTE: The one caveat here is that the size of each element in the list must be larger than sizeof(void*)

To track the free items in the free list itself we do the following.

  1. When creating the pool, after allocating the memory, starting with the beggining of the list, write the address of the next free block at the start of the current block.
  2. When providing a block to the user, return the start of the list, but not before setting the pointer to the start of the list to the next block.
  3. When returning a block to the free list, write the address of the start of the free list into the start of the newly free block, and set the start of the free list to the newly freed block's address.

Remember that when using the memory of the free block itself as the pointer to the next block, the equivalent to 

mFreeListStart = mFreeListStart->next;

Becomes:

mFreeListStart = (u32*)*mFreeListStart;

Where mFreeListStart is a u32* to the start of memory of the first element in the list,

next is a u32* to the next element,

and  u32 is a 32-bit unsigned integer type.

Stack Allocator

So I said I was going to start working on my game engine project, and I know it seems like I've been doing a whole lot of nothing, but I've been working this whole time!  ...Well some of this whole time.

Working on what?  On Memory!

I've written a few different memory algorithms now, but I'll start with the simplest: a Stack Allocator.

--> Open StackAllocator.cpp in a new window <--

The code above is hosten on Google Code, and you can comment by double clicking anywhere, or by clicking in the left hand margin on the little speech balloon plus icon. You need to be signed in with a google account.  Please comment and tell me if my code sucks!

A stack allocator is useful in games because many games allocate memory for levels in a stack like manner.  When a new game level is loaded, the memory for all the textures and meshes etc is loaded at once, and then nothing more is allocated during the duration of the level.  When the level is unloaded, all of the memory is freed.  A stack memory allocator is perfect for this sort of application.

Implementing a stack allocator is pretty simple:

  1. In the constructor, malloc a block of memory to use as the stack, and set it as the base of the stack and as the current top (ensuring it's not NULL).
  2. When memory is requested from the stack, store the address of the current top to return, and then move the current top up by the size requested.
  3. When freeing, you just set the current top back to the marker passed to the freeToMarker function.
  4. Clear the stack by setting the current top back to the base of the stack.
  5. When deleting the whole stack, you can ensure all memory has been freed by asserting that the current top is the same memory address as the base

The next write up I believe will be Pool Allocators.

Game Engine Project (It Begins!)

See that? That's ¥156,789 down the drain.

I mean, that's my new computer and my new desk! (Also, if you look closely, you can see my space-saving strategy: my futon bed rolled out beneath the desk. I now sleep under my desk every night.)

Why did I spend ~$1,900? Because I'm crazy.  Because I have a plan!

I want to start writing my own game engine. I've recently been reading Jason Gregory's Game Engine Architecture book, and it made me want to have the experience of writing all these systems myself.

While it may take a few years, I'm going to start doing just that, in my spare time.  I wanted to write for code to run on a game console, but the closest thing I have is this iPad, so I bought a new mac computer. (My previous one is about 5 years old, the first Intel mac.) I also spent $100 on an iOS developer license, so now we're up over $2,000.

If you want to follow along with my progress, bookmark this link. I'll try to post on here somewhat frequently about how the project is going, but it will be slow, especially while I'm still taking masters classes part time in Japanese, and working full time.  But maybe in summer the pace will pick up a bit.