diff options
author | Christopher Ferris <cferris@google.com> | 2016-02-11 15:51:31 -0800 |
---|---|---|
committer | Christopher Ferris <cferris@google.com> | 2016-02-12 17:30:30 -0800 |
commit | 72df6708c829a4c6494936fdfbda6dc7e68e647b (patch) | |
tree | 17ede0d54b6f50c3598a3fcec56f50754067690d /libc/malloc_debug/malloc_debug.cpp | |
parent | 72bca4b4105e24058f3f2eca024382bedb122a30 (diff) |
Fix the default alignment of the allocations.
In order to enforce this constraint:
The pointer returned if the allocation succeeds shall be suitably
aligned so that it may be assigned to a pointer to any type of object
and then used to access such an object in the space allocated.
Force all allocations on 32 bit systems to have 8 byte alignment,
and all allocations on 64 bit systems to have 16 byte alignment.
Add a test to verify that the allocator returns the correct alignments.
Bug: 26739265
Change-Id: I9af53279617408676b94e4ec6481b3ed7ffafc6a
Diffstat (limited to 'libc/malloc_debug/malloc_debug.cpp')
-rw-r--r-- | libc/malloc_debug/malloc_debug.cpp | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/libc/malloc_debug/malloc_debug.cpp b/libc/malloc_debug/malloc_debug.cpp index 4f8657969..0c0907d00 100644 --- a/libc/malloc_debug/malloc_debug.cpp +++ b/libc/malloc_debug/malloc_debug.cpp @@ -39,6 +39,7 @@ #include <private/bionic_malloc_dispatch.h> #include "backtrace.h" +#include "Config.h" #include "DebugData.h" #include "debug_disable.h" #include "debug_log.h" @@ -264,7 +265,8 @@ void* debug_malloc(size_t size) { return nullptr; } - Header* header = reinterpret_cast<Header*>(g_dispatch->memalign(sizeof(uintptr_t), real_size)); + Header* header = reinterpret_cast<Header*>( + g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); if (header == nullptr) { return nullptr; } @@ -355,10 +357,10 @@ void* debug_memalign(size_t alignment, size_t bytes) { if (!powerof2(alignment)) { alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment); } - // Force the alignment to at least sizeof(uintptr_t) to guarantee + // Force the alignment to at least MINIMUM_ALIGNMENT_BYTES to guarantee // that the header is aligned properly. - if (alignment < sizeof(uintptr_t)) { - alignment = sizeof(uintptr_t); + if (alignment < MINIMUM_ALIGNMENT_BYTES) { + alignment = MINIMUM_ALIGNMENT_BYTES; } // We don't have any idea what the natural alignment of @@ -512,7 +514,8 @@ void* debug_calloc(size_t nmemb, size_t bytes) { } // Need to guarantee the alignment of the header. - Header* header = reinterpret_cast<Header*>(g_dispatch->memalign(sizeof(uintptr_t), real_size)); + Header* header = reinterpret_cast<Header*>( + g_dispatch->memalign(MINIMUM_ALIGNMENT_BYTES, real_size)); if (header == nullptr) { return nullptr; } |