Skip to content
Snippets Groups Projects
Commit e8fdd130 authored by Arseny Kapoulkine's avatar Arseny Kapoulkine
Browse files

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.
parent 66f242a4
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment