Commit becb44f1 by Nicolas Capens Committed by Nicolas Capens

Fix allocation of low-alignment memory

posix_memalign() returns EINVAL if the alignment argument is not a multiple of sizeof(void*). Use regular malloc() for those cases. malloc() is specified to allocate memory which is "suitably aligned for any built-in type". Bug b/128618202 Change-Id: Ibee07be89f2a5bd6be770c35710b20b0c752dfd1 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/27228 Presubmit-Ready: Nicolas Capens <nicolascapens@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com>
parent a2749f39
...@@ -54,6 +54,12 @@ void *allocateRaw(size_t bytes, size_t alignment) ...@@ -54,6 +54,12 @@ void *allocateRaw(size_t bytes, size_t alignment)
ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment. ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(LINUX_ENABLE_NAMED_MMAP)
if(alignment < sizeof(void*))
{
return malloc(bytes);
}
else
{
void *allocation; void *allocation;
int result = posix_memalign(&allocation, alignment, bytes); int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0) if(result != 0)
...@@ -62,6 +68,7 @@ void *allocateRaw(size_t bytes, size_t alignment) ...@@ -62,6 +68,7 @@ void *allocateRaw(size_t bytes, size_t alignment)
allocation = nullptr; allocation = nullptr;
} }
return allocation; return allocation;
}
#else #else
unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment]; unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
unsigned char *aligned = nullptr; unsigned char *aligned = nullptr;
......
...@@ -57,6 +57,12 @@ void *allocateRaw(size_t bytes, size_t alignment) ...@@ -57,6 +57,12 @@ void *allocateRaw(size_t bytes, size_t alignment)
ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment. ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(LINUX_ENABLE_NAMED_MMAP)
if(alignment < sizeof(void*))
{
return malloc(bytes);
}
else
{
void *allocation; void *allocation;
int result = posix_memalign(&allocation, alignment, bytes); int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0) if(result != 0)
...@@ -65,6 +71,7 @@ void *allocateRaw(size_t bytes, size_t alignment) ...@@ -65,6 +71,7 @@ void *allocateRaw(size_t bytes, size_t alignment)
allocation = nullptr; allocation = nullptr;
} }
return allocation; return allocation;
}
#else #else
unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment]; unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
unsigned char *aligned = nullptr; unsigned char *aligned = nullptr;
......
...@@ -54,6 +54,12 @@ void *allocateRaw(size_t bytes, size_t alignment) ...@@ -54,6 +54,12 @@ void *allocateRaw(size_t bytes, size_t alignment)
ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment. ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(LINUX_ENABLE_NAMED_MMAP)
if(alignment < sizeof(void*))
{
return malloc(bytes);
}
else
{
void *allocation; void *allocation;
int result = posix_memalign(&allocation, alignment, bytes); int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0) if(result != 0)
...@@ -62,6 +68,7 @@ void *allocateRaw(size_t bytes, size_t alignment) ...@@ -62,6 +68,7 @@ void *allocateRaw(size_t bytes, size_t alignment)
allocation = nullptr; allocation = nullptr;
} }
return allocation; return allocation;
}
#else #else
unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment]; unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
unsigned char *aligned = nullptr; unsigned char *aligned = nullptr;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment