Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header alignment in StackAllocator #5

Open
txemagon opened this issue Jul 21, 2021 · 0 comments
Open

Header alignment in StackAllocator #5

txemagon opened this issue Jul 21, 2021 · 0 comments

Comments

@txemagon
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant