aligned malloc

695 views
Skip to first unread message

gadget

unread,
Jan 23, 2013, 10:09:19 PM1/23/13
to andro...@googlegroups.com
Hi everyone, just wondering what the default behavior of malloc() is with respect to alignment is in bionic.

Here it is mentioned that "GNU libc malloc() always returns 8-byte aligned memory addresses"
http://stackoverflow.com/questions/5061392/aligned-memory-management

what about bionic?

David Turner

unread,
Jan 24, 2013, 3:48:09 AM1/24/13
to andro...@googlegroups.com
According to https://android.googlesource.com/platform/bionic/+/master/libc/upstream-dlmalloc/malloc.c this is 8 byte minimum as well (see line 43).
However, this is only the current revision of the sources. Previous iterations might be slightly different, and future ones as well. I.e. treat this as an implementation detail, not a guarantee.

Generally speaking, any allocator *must* return an 8-byte aligned address if the requested size is >= 8 (otherwise lots of things would break, because malloc() doesn't know if the caller is going to store 8-byte aligned values like doubles in the corresponding memory block).

It's what happens for sizes < 8 that could change, and I could easily imagine an allocator implementation that would want to "optimize" these allocations into smaller aligned slots (e.g. in larger page-allocated pools), so I would _not_ rule that out for this case.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

gadget

unread,
Jan 24, 2013, 12:53:47 PM1/24/13
to andro...@googlegroups.com
David, thank you very much for a perfect answer!
Reply all
Reply to author
Forward
0 new messages