Commit 2cc2a727 by Jamie Madill Committed by Commit Bot

common: Add aligned memory allocator.

This will be useful for the Vulkan pipeline state cache. This cache wants to pack the pipeline description very carefully into a specific number of bits, using the "alignas" keyword. Using this keyword requires an aligned allocator, which we can lift from Chromium's base/memory submodule. Also includes a unittest copied from Chrome. Bug: angleproject:2163 Change-Id: I25976be4610636db3f43552bba23d823f5a49a24 Reviewed-on: https://chromium-review.googlesource.com/837944Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Reviewed-by: 's avatarFrank Henigman <fjhenigman@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent 5b6b9c63
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// aligned_memory: An aligned memory allocator. Based on Chrome's base/memory/aligned_memory.
//
#include "common/aligned_memory.h"
#include "common/debug.h"
#if defined(COMPILER_MSVC)
#include <malloc.h>
#else
#include <stdlib.h>
#endif
namespace angle
{
void *AlignedAlloc(size_t size, size_t alignment)
{
ASSERT(size > 0);
ASSERT((alignment & (alignment - 1)) == 0);
ASSERT((alignment % sizeof(void *)) == 0);
void *ptr = nullptr;
#if defined(_MSC_VER)
ptr = _aligned_malloc(size, alignment);
// Android technically supports posix_memalign(), but does not expose it in
// the current version of the library headers used by Chrome. Luckily,
// memalign() on Android returns pointers which can safely be used with
// free(), so we can use it instead. Issue filed to document this:
// http://code.google.com/p/android/issues/detail?id=35391
#elif defined(ANGLE_PLATFORM_ANDROID)
ptr = memalign(alignment, size);
#else
if (posix_memalign(&ptr, alignment, size))
ptr = nullptr;
#endif
// Since aligned allocations may fail for non-memory related reasons, force a
// crash if we encounter a failed allocation.
if (!ptr)
{
ERR() << "If you crashed here, your aligned allocation is incorrect: "
<< "size=" << size << ", alignment=" << alignment;
ASSERT(false);
}
// Sanity check alignment just to be safe.
ASSERT((reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)) == 0);
return ptr;
}
void AlignedFree(void *ptr)
{
#if defined(_MSC_VER)
_aligned_free(ptr);
#else
free(ptr);
#endif
}
} // namespace angle
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// aligned_memory: An aligned memory allocator. Based on Chrome's base/memory/aligned_memory.
//
#ifndef COMMON_ALIGNED_MEMORY_H_
#define COMMON_ALIGNED_MEMORY_H_
#include <cstddef>
namespace angle
{
// This can be replaced with std::aligned_malloc when we have C++17.
void *AlignedAlloc(size_t size, size_t alignment);
void AlignedFree(void *ptr);
} // namespace angle
#endif // COMMON_ALIGNED_MEMORY_H_
//
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// aligned_memory_unittests: Tests for the aligned memory allocator.
// Tests copied from Chrome: src/base/memory/aligned_memory_unittests.cc.
//
#include "common/aligned_memory.h"
#include <memory>
#include "testing/gtest/include/gtest/gtest.h"
#define EXPECT_ALIGNED(ptr, align) EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
namespace angle
{
// Test that dynamic allocation works as expected.
TEST(AlignedMemoryTest, DynamicAllocation)
{
void *p = AlignedAlloc(8, 8);
EXPECT_TRUE(p);
EXPECT_ALIGNED(p, 8);
AlignedFree(p);
p = AlignedAlloc(8, 16);
EXPECT_TRUE(p);
EXPECT_ALIGNED(p, 16);
AlignedFree(p);
p = AlignedAlloc(8, 256);
EXPECT_TRUE(p);
EXPECT_ALIGNED(p, 256);
AlignedFree(p);
p = AlignedAlloc(8, 4096);
EXPECT_TRUE(p);
EXPECT_ALIGNED(p, 4096);
AlignedFree(p);
}
} // namespace base
\ No newline at end of file
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
'common/MemoryBuffer.cpp', 'common/MemoryBuffer.cpp',
'common/MemoryBuffer.h', 'common/MemoryBuffer.h',
'common/Optional.h', 'common/Optional.h',
'common/aligned_memory.cpp',
'common/aligned_memory.h',
'common/angleutils.cpp', 'common/angleutils.cpp',
'common/angleutils.h', 'common/angleutils.h',
'common/bitset_utils.h', 'common/bitset_utils.h',
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
'angle_unittests_sources': 'angle_unittests_sources':
[ [
'<(angle_path)/src/common/Optional_unittest.cpp', '<(angle_path)/src/common/Optional_unittest.cpp',
'<(angle_path)/src/common/aligned_memory_unittest.cpp',
'<(angle_path)/src/common/angleutils_unittest.cpp', '<(angle_path)/src/common/angleutils_unittest.cpp',
'<(angle_path)/src/common/bitset_utils_unittest.cpp', '<(angle_path)/src/common/bitset_utils_unittest.cpp',
'<(angle_path)/src/common/mathutil_unittest.cpp', '<(angle_path)/src/common/mathutil_unittest.cpp',
......
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