From e8fdd1303c6c28ebd2183a77b507c26a570fe4f5 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Sat, 25 Jul 2015 17:04:17 -0400 Subject: tests: Fix test allocator to provide fundamental alignment Previously test allocator only guaranteed alignment enough for a pointer. On some platforms (e.g. SPARC) double has to be aligned to 8 bytes but pointers can have a size of 4 bytes. This commit increases allocation header to fix that. In practical terms the allocation header is now always 8 bytes. --- tests/allocator.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'tests/allocator.cpp') diff --git a/tests/allocator.cpp b/tests/allocator.cpp index 3db390e..c3ade48 100644 --- a/tests/allocator.cpp +++ b/tests/allocator.cpp @@ -138,14 +138,16 @@ namespace #endif // High-level allocation functions +const size_t memory_alignment = sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*); + void* memory_allocate(size_t size) { - void* result = allocate(size + sizeof(size_t)); + void* result = allocate(size + memory_alignment); if (!result) return 0; memcpy(result, &size, sizeof(size_t)); - return static_cast(result) + 1; + return static_cast(result) + memory_alignment; } size_t memory_size(void* ptr) @@ -153,7 +155,7 @@ size_t memory_size(void* ptr) assert(ptr); size_t result; - memcpy(&result, static_cast(ptr) - 1, sizeof(size_t)); + memcpy(&result, static_cast(ptr) - memory_alignment, sizeof(size_t)); return result; } @@ -164,6 +166,6 @@ void memory_deallocate(void* ptr) size_t size = memory_size(ptr); - deallocate(static_cast(ptr) - 1, size + sizeof(size_t)); + deallocate(static_cast(ptr) - memory_alignment, size + memory_alignment); } -- cgit v1.2.3