1

Can someone help with my stack based allocator.

static char Stack[MAX_SIZE];
static char *top = &Stack[0];

class STACKED {
    public:
        static void *operator new(size_t size) {
            //Add this context to the context stack.
            void *Result;
            assert( (top + size) <= &Stack[MAX_SIZE] );

            Result = top;
            top = top + align_sizeof(size);
            return Result;
        }

        static void operator delete(void *p) {
            //Its a stack, and delete only removes the top entry.
            // So *p represents where to delete to.
            top = (char *)p;
        }

        static size_t align_sizeof(size_t object_size) {
            // TODO: Align objects.
            return object_size;
        }
};

1) Is the assert correct to detect if the stack will overflow?

2) align_sizeof() - Id like to align these objects for performance/bus issues. But I do not fully understand alignment. Can someone describe memory alignment, and suggest a formula to align the stack objects?

2a) My current thinking is to "roundup" the size of the object so that top is always aligned. Effectively allocating more memory than required (placing padding at the end).

3) I'm guessing I also have to align the static char Stack[MAX_SIZE] - How do I do this?

Thankyou!

P.S any other comments always welcome

4
  • What happens in your allocator if objects A B C get allocated in this order and B is destroyed before C and an object D with sizeof(D) > sizeof(B) is allocated? Commented Jul 9, 2012 at 7:44
  • 1
    If size is too large, the expression top + size causes undefined behaviour. So you should check that Stack + MAX_SIZE - top < size. Commented Jul 9, 2012 at 7:49
  • @RedX good comment, I attempt to put a failsafe into delete so that only the top entry can be deleted at a time. Commented Jul 9, 2012 at 7:49
  • @KerrekSB thankyou for your comment, I have changed it to assert( (&Stack[MAX_SIZE] - top) >= size ); // How much space left >= size asked for. Commented Jul 9, 2012 at 7:54

1 Answer 1

0

First of all, you can replace static char *top = &Stack[0]; with static char *top = Stack;, since the array name is the pointer to the first element.

Your code is very unsafe. You are returning a pointer to an element in the array. The accert is ok, but consider such a usage:

STACKED a;
char * foo = (char *)a.operator new(10);
foo += 10000000;
*foo= 's';

This will result in a crash, and your assert isn't going to help this.

If you want to manage the memory, you should put your array inside the class, and let the class handle all the usage, not letting the user work with the direct pointers. In such a case, you will be able to track if the user wants to access an aout-of-bonds index.

When you are deleting an object, you should not only move the top of your stack. If the user will try to remove an item which is in the "middle" of the stack, you will need to change the stack state appropriately, but look carefuly not to ruin the pointers to the memory that is already allocated.

As for the alignment, you culd check wiki, hope that will help.

Also, when you are deleting an element, you should check that the pointer that you get is really pointing to somewhere inside your stack. Here's an example of a bad code:

STACKED a;
char * foo = new char(10);
a.operator delete(foo - 10000);
char * crash = (char *)a.operator new(10);

Your assert() may help in this case, but the user of the class might not understand what actually happened.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a preprocessor which selects the "top" object on the stack, which is the recommended way to interact with the stack and the objects. - What id like to do is not return a value from operator new? - That should solve the this problem? but is that possible?
would not returning a value from the new operator invalidate the "this" pointer?
@SingerofTheFall the case you portrait as unsafe is true for any allocator. If you try to write over the bounds you allocated you will always be in unsafe territories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.