Heap allocation and uint32_t alignment

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
tomlogic
Posts: 13
Joined: Tue Jul 19, 2016 10:20 pm

Heap allocation and uint32_t alignment

Post by tomlogic » Tue Aug 09, 2016 12:51 am

Just getting my feet wet with the MicroPython codebase, and porting it to an ARM-based platform.

I think I understand how the heap works in the "minimal" platform design, but I was surprised that it was an array of "char" instead of an array of "uint32_t". On a platform that requires alignment of 32-bit values, wouldn't it make more sense to create an array of uint32_t to use for the heap? Does it happen to work with that platform because the heap is allocated after "stack_top", which is a pointer and therefore aligned?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Heap allocation and uint32_t alignment

Post by dhylands » Tue Aug 09, 2016 5:58 am

I agree that it would be safer to declare the heap as an array of unit32_t's.

On stmhal, the heap is aligned in the linker script file.
On unix, the heap is allocated from the C runtime heap, so its automatically aligned.

The fact that its declared after stacktop will make it be 4-byte aligned, but it could be unaligned if you changed the code.

Post Reply