I'm working on a memory pool for a small game engine.
The main use will be as a segregated storage; a pool contains object of a specific type and size. Currently the pools can be used to store anything, but allocations will be done in blocks of a specific size. Most of the memory need will be allocated at once, but "overgrowth" can be enabled if needed to assist in tuning (almost fixed size).
Problem is, I started to get somewhat paranoid when contemplating about memory alignment. I'm only used to raw memory management on 8 bit processors where everything is byte aligned.
I'm letting the user (me) specify the desired size of the blocks, which in the segregated storage case would be the size of the objects that I'm going to store in it.
The current approach is to allocate a chunk of memory blocks * (desired_size + header_size) big and place the objects in it, with a header for each block; objects would obviously be positioned directly behind this header.
What do I need to consider with regards to memory alignment in my scenario?
The answer I've come up with so far is that as long as desired_size represents n-byte aligned data; the header is correctly aligned and packed by the compiler as well as the actual data, everything stored in the block will be n-byte aligned.
n is whatever boundary required by the platform. I'm targeting x86 for the moment but I don't like to make any assumptions about the platform in my code.
Some of the resources I've used:
- http://www.ibm.com/developerworks/library/pa-dalign/
- http://en.wikipedia.org/wiki/Data_structure_alignment
- Memory alignment on a 32-bit Intel processor
- Boost Pool docs for inspiration on the general design. I would like to avoid dragging boost into this project; and I'm seeing this as a learning opportunity as well.
Edit
Uploaded small sample code which may helpful anybody as confused as me in the future here.