summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArseny Kapoulkine <arseny.kapoulkine@gmail.com>2015-07-25 17:04:17 -0400
committerArseny Kapoulkine <arseny.kapoulkine@gmail.com>2015-07-25 17:04:17 -0400
commite8fdd1303c6c28ebd2183a77b507c26a570fe4f5 (patch)
treed7956d6836e361ef3e44f03323ca5ffae60a6219
parent66f242a4a97626c995cfc3635c26b55236a48325 (diff)
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.
-rw-r--r--tests/allocator.cpp10
1 files changed, 6 insertions, 4 deletions
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<size_t*>(result) + 1;
+ return static_cast<char*>(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<size_t*>(ptr) - 1, sizeof(size_t));
+ memcpy(&result, static_cast<char*>(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<size_t*>(ptr) - 1, size + sizeof(size_t));
+ deallocate(static_cast<char*>(ptr) - memory_alignment, size + memory_alignment);
}