Skip to main content
GameDev.net gamedev.net
🔒 Locked

Improving my stack allocator

Started by Aqua Costa Aug 30, 2011 at 3:29 PM 5 replies 1.4k views
Original Post
Aqua Costa
Aqua Costa
This is my stack allocator:

#include <stdlib.h>
#include <cstdint>

typedef uintptr_t Marker;

namespace Loodus
{
class StackAllocator
{
public:
StackAllocator(U32 size);

Marker allocate(U32 size);

void freeToMarker(Marker marker);

void clear();

private:
Marker m_InitialPosition;
Marker m_CurrentPosition;
};
};


StackAllocator.cpp

#include "StackAllocator.h"

using namespace Loodus;

StackAllocator::StackAllocator(U32 size)
{
m_InitialPosition = (Marker) malloc(size);
}

Marker StackAllocator::allocate(U32 size)
{
Marker m = m_CurrentPosition;

m_CurrentPosition += size;

return m;
}

void StackAllocator::freeToMarker(Marker marker)
{
m_CurrentPosition = marker;
}

void StackAllocator::clear()
{
m_CurrentPosition = m_InitialPosition;
}


However the allocate method doesn't return aligned pointers. How should I write allocate() method to return aligned pointers?
Ectara
Ectara
Try internally rounding the size parameter to the next multiple of 8 or whatever you choose for alignment. Also, ensure the beginning of the pool is aligned to begin with.
Aqua Costa
Aqua Costa
Should I use __alignof(type) to choose the correct alignment of each structure?
smasherprog
smasherprog
The memory allocator does not have a free function?
If the memory requested is smaller than 16 bytes, just make sure the chunk can fit within a 16 byte aligned chunk. If it is bigger than 16 bytes, make sure it is aligned on a 16 byte boundary.

If you want to check out my memory allocator that I wrote a while ago, check it out at http://nolimitsdesig...html/index.html
It can give you an idea of how to write an allocator to free memory, and make sure it is aligned to a specific byte alignment. The code is short and if you need anything explained, you can contact me. I dont currently support it, but if there are any errors in the code, I will correct them.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Aqua Costa
Aqua Costa
So if I have a struct like this:

struct Packing
{
int x; // 4-byte align
int y; // 4-byte align
short int z; // 2-byte align
char m; // 1-byte align;
char _pad[1]; // explicit padding
};

The size of this struct is 12 bytes. Since 12 is not a power of two how do I align this structure? What about a struct with size 36?
Or should I align the struct with the alignment that its members use? In the example 4-byte align?
smasherprog
smasherprog
I remember a topic we had a while ago about memory alignment. The short version is: the CPU loads chunks of memory in chunks of 16 bytes (generally). If the cpu is working on something that is overlapping the 16 byte boundary, it does more work.
Check this out http://msdn.microsoft.com/en-us/library/xh3e3fd0%28v=vs.80%29.aspx visual studio will help you with data member alignment.
Generally, you want the beginning of a structure to be aligned to a 16 byte boundary. So, in your example, the easiest way would be to add padding to make it exactly 16 bytes, and make sure the beginning is aligned to a 16 byte boundary.
Dont go overboard thought. What I mean is dont try to optimize all parts of your application to ensure everything is perfectly aligned all the time. if you are iterating over an array less than a hundred objects long and the memory is misaligned, it wont hurt performance. But, if you have a large array, then make sure it is aligned.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Ectara
Ectara
You don't need compiler intrinsics to align structure sizes if you're allocating one at a time. Allocate ((size + (align_bytes - 1)) - (size + (align_bytes - 1)) % align_bytes). If you allocate 16 bytes for a 12 byte structure, the last four just aren't use, and make no difference; the OS likely allocates aligned chunks of memory, anyway, so if you request an exact amount or an aligned amount, you'd likely consume just as much memory.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.