Skip to content

Header alignment in StackAllocator #5

@txemagon

Description

@txemagon

Maybe is a misunderstanding but I think the header in the StackAllocator is placed at the beginning of the block, then there is an adjustment, and finally the new data (to which the pointer is returned).

That, as I understand it, leaves the header unreachable since the adjustment length is what the header is.

                                                 +---- aligned mem pos. (and pointer returned by the allocator)
                                                 v
| o l d · d a t a | adj | e m p t y · s p a c e | n e w · d a t a| 
                                DE AD BE EF

So when freeing the memory the adjustement isn't next to the new data.

Despite no incidence when aligning on a per byte strategy. Which I believe is the case:

Read in ECSMM.h: 62

void* pMemory = m_MemoryAllocator->allocate(memSize, alignof(u8));

with other alignments will corrupt the free operation with a wild pointer.

I'd like to suggest changing

StackAllocator.cpp: 49

		// store alignment in allocation meta info
		asMeta->adjustment = adjustment;

		// determine aligned memory address
		asUptr += adjustment;

to

	// determine aligned memory address
	asUptr += adjustment;

	// store alignment in allocation meta info in the previous block
	(asMeta-1)->adjustment = adjustment;
	/*
	 * todo: move header before data
                            adjustment              asUptr new emplacement
	                            v                          v
	  | u·s·e·d  m·e·m·o·r·y || 0 | 1 | 2 | 3 | 4 | adj |  new data
	                                  ^
	                                  L empty bytes
	 */

since (asMeta-1) is pointer arithmetics measured in AllocMetaInfo size.

Thanks a lot for sharing your code. There isn't a single file in which I hadn't learn something about C++ programing.
Regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions