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,14 +54,21 @@ void *allocateRaw(size_t bytes, size_t alignment)
ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
#if defined(LINUX_ENABLE_NAMED_MMAP)
void *allocation;
int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0)
if(alignment < sizeof(void*))
{
errno = result;
allocation = nullptr;
return malloc(bytes);
}
else
{
void *allocation;
int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0)
{
errno = result;
allocation = nullptr;
}
return allocation;
}
return allocation;
#else
unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
unsigned char *aligned = nullptr;
......
......@@ -57,14 +57,21 @@ void *allocateRaw(size_t bytes, size_t alignment)
ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
#if defined(LINUX_ENABLE_NAMED_MMAP)
void *allocation;
int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0)
if(alignment < sizeof(void*))
{
errno = result;
allocation = nullptr;
return malloc(bytes);
}
else
{
void *allocation;
int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0)
{
errno = result;
allocation = nullptr;
}
return allocation;
}
return allocation;
#else
unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
unsigned char *aligned = nullptr;
......
......@@ -54,14 +54,21 @@ void *allocateRaw(size_t bytes, size_t alignment)
ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
#if defined(LINUX_ENABLE_NAMED_MMAP)
void *allocation;
int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0)
if(alignment < sizeof(void*))
{
errno = result;
allocation = nullptr;
return malloc(bytes);
}
else
{
void *allocation;
int result = posix_memalign(&allocation, alignment, bytes);
if(result != 0)
{
errno = result;
allocation = nullptr;
}
return allocation;
}
return allocation;
#else
unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
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