OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/aligned_memory.h" | 5 #include "base/memory/aligned_memory.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 #define EXPECT_ALIGNED(ptr, align) \ | 12 #define EXPECT_ALIGNED(ptr, align) \ |
13 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) | 13 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 using base::AlignedMemory; | 17 using base::AlignedMemory; |
18 | 18 |
| 19 // Note: there are a number of compatibility issues with large alignments. |
| 20 // On ARM, gcc doesn't support alignment >64 bytes. In addition, the NaCl x86-64 |
| 21 // compiler emits non-validating instructions for >128 bytes alignment. See |
| 22 // https://www.chromium.org/nativeclient/design-documents/nacl-sfi-model-on-x86-
64-systems |
| 23 // and https://code.google.com/p/nativeclient/issues/detail?id=3463 |
19 TEST(AlignedMemoryTest, StaticAlignment) { | 24 TEST(AlignedMemoryTest, StaticAlignment) { |
20 static AlignedMemory<8, 8> raw8; | 25 static AlignedMemory<8, 8> raw8; |
21 static AlignedMemory<8, 16> raw16; | 26 static AlignedMemory<8, 16> raw16; |
22 static AlignedMemory<8, 256> raw256; | 27 static AlignedMemory<8, 32> raw32; |
23 static AlignedMemory<8, 4096> raw4096; | 28 static AlignedMemory<8, 64> raw64; |
24 | 29 |
25 EXPECT_EQ(8u, ALIGNOF(raw8)); | 30 EXPECT_EQ(8u, alignof(decltype(raw8))); |
26 EXPECT_EQ(16u, ALIGNOF(raw16)); | 31 EXPECT_EQ(16u, alignof(decltype(raw16))); |
27 EXPECT_EQ(256u, ALIGNOF(raw256)); | 32 EXPECT_EQ(32u, alignof(decltype(raw32))); |
28 EXPECT_EQ(4096u, ALIGNOF(raw4096)); | 33 EXPECT_EQ(64u, alignof(decltype(raw64))); |
29 | 34 |
30 EXPECT_ALIGNED(raw8.void_data(), 8); | 35 EXPECT_ALIGNED(raw8.void_data(), 8); |
31 EXPECT_ALIGNED(raw16.void_data(), 16); | 36 EXPECT_ALIGNED(raw16.void_data(), 16); |
32 EXPECT_ALIGNED(raw256.void_data(), 256); | 37 EXPECT_ALIGNED(raw32.void_data(), 32); |
33 EXPECT_ALIGNED(raw4096.void_data(), 4096); | 38 EXPECT_ALIGNED(raw64.void_data(), 64); |
34 } | 39 } |
35 | 40 |
36 TEST(AlignedMemoryTest, StackAlignment) { | 41 TEST(AlignedMemoryTest, StackAlignment) { |
37 AlignedMemory<8, 8> raw8; | 42 AlignedMemory<8, 8> raw8; |
38 AlignedMemory<8, 16> raw16; | 43 AlignedMemory<8, 16> raw16; |
39 AlignedMemory<8, 128> raw128; | 44 AlignedMemory<8, 32> raw32; |
| 45 AlignedMemory<8, 64> raw64; |
40 | 46 |
41 EXPECT_EQ(8u, ALIGNOF(raw8)); | 47 EXPECT_EQ(8u, alignof(decltype(raw8))); |
42 EXPECT_EQ(16u, ALIGNOF(raw16)); | 48 EXPECT_EQ(16u, alignof(decltype(raw16))); |
43 EXPECT_EQ(128u, ALIGNOF(raw128)); | 49 EXPECT_EQ(32u, alignof(decltype(raw32))); |
| 50 EXPECT_EQ(64u, alignof(decltype(raw64))); |
44 | 51 |
45 EXPECT_ALIGNED(raw8.void_data(), 8); | 52 EXPECT_ALIGNED(raw8.void_data(), 8); |
46 EXPECT_ALIGNED(raw16.void_data(), 16); | 53 EXPECT_ALIGNED(raw16.void_data(), 16); |
47 | 54 EXPECT_ALIGNED(raw32.void_data(), 32); |
48 // TODO(ios): __attribute__((aligned(X))) with X >= 128 does not works on | 55 EXPECT_ALIGNED(raw64.void_data(), 64); |
49 // the stack when building for arm64 on iOS, http://crbug.com/349003 | |
50 #if !(defined(OS_IOS) && defined(ARCH_CPU_ARM64)) | |
51 EXPECT_ALIGNED(raw128.void_data(), 128); | |
52 | |
53 // NaCl x86-64 compiler emits non-validating instructions for >128 | |
54 // bytes alignment. | |
55 // http://www.chromium.org/nativeclient/design-documents/nacl-sfi-model-on-x86
-64-systems | |
56 // TODO(hamaji): Ideally, NaCl compiler for x86-64 should workaround | |
57 // this limitation and this #if should be removed. | |
58 // https://code.google.com/p/nativeclient/issues/detail?id=3463 | |
59 #if !(defined(OS_NACL) && defined(ARCH_CPU_X86_64)) | |
60 AlignedMemory<8, 256> raw256; | |
61 EXPECT_EQ(256u, ALIGNOF(raw256)); | |
62 EXPECT_ALIGNED(raw256.void_data(), 256); | |
63 | |
64 // TODO(ios): This test hits an armv7 bug in clang. crbug.com/138066 | |
65 #if !(defined(OS_IOS) && defined(ARCH_CPU_ARM_FAMILY)) | |
66 AlignedMemory<8, 4096> raw4096; | |
67 EXPECT_EQ(4096u, ALIGNOF(raw4096)); | |
68 EXPECT_ALIGNED(raw4096.void_data(), 4096); | |
69 #endif // !(defined(OS_IOS) && defined(ARCH_CPU_ARM_FAMILY)) | |
70 #endif // !(defined(OS_NACL) && defined(ARCH_CPU_X86_64)) | |
71 #endif // !(defined(OS_IOS) && defined(ARCH_CPU_ARM64)) | |
72 } | 56 } |
73 | 57 |
74 TEST(AlignedMemoryTest, DynamicAllocation) { | 58 TEST(AlignedMemoryTest, DynamicAllocation) { |
75 void* p = base::AlignedAlloc(8, 8); | 59 void* p = base::AlignedAlloc(8, 8); |
76 EXPECT_TRUE(p); | 60 EXPECT_TRUE(p); |
77 EXPECT_ALIGNED(p, 8); | 61 EXPECT_ALIGNED(p, 8); |
78 base::AlignedFree(p); | 62 base::AlignedFree(p); |
79 | 63 |
80 p = base::AlignedAlloc(8, 16); | 64 p = base::AlignedAlloc(8, 16); |
81 EXPECT_TRUE(p); | 65 EXPECT_TRUE(p); |
(...skipping 12 matching lines...) Expand all Loading... |
94 } | 78 } |
95 | 79 |
96 TEST(AlignedMemoryTest, ScopedDynamicAllocation) { | 80 TEST(AlignedMemoryTest, ScopedDynamicAllocation) { |
97 std::unique_ptr<float, base::AlignedFreeDeleter> p( | 81 std::unique_ptr<float, base::AlignedFreeDeleter> p( |
98 static_cast<float*>(base::AlignedAlloc(8, 8))); | 82 static_cast<float*>(base::AlignedAlloc(8, 8))); |
99 EXPECT_TRUE(p.get()); | 83 EXPECT_TRUE(p.get()); |
100 EXPECT_ALIGNED(p.get(), 8); | 84 EXPECT_ALIGNED(p.get(), 8); |
101 } | 85 } |
102 | 86 |
103 } // namespace | 87 } // namespace |
OLD | NEW |