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 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 #define EXPECT_ALIGNED(ptr, align) \ | 9 #define EXPECT_ALIGNED(ptr, align) \ |
10 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) | 10 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 using base::AlignedMemory; | 14 using base::AlignedMemory; |
15 | 15 |
16 TEST(AlignedMemoryTest, StaticAlignment) { | 16 TEST(AlignedMemoryTest, StaticAlignment) { |
17 static AlignedMemory<8, 8> raw8; | 17 static AlignedMemory<8, 8> raw8; |
18 static AlignedMemory<8, 16> raw16; | 18 static AlignedMemory<8, 16> raw16; |
19 static AlignedMemory<8, 256> raw256; | 19 static AlignedMemory<8, 256> raw256; |
20 static AlignedMemory<8, 4096> raw4096; | 20 static AlignedMemory<8, 4096> raw4096; |
21 | 21 |
22 EXPECT_EQ(8u, ALIGNOF(raw8)); | 22 EXPECT_EQ(8u, ALIGNOF(decltype(raw8))); |
23 EXPECT_EQ(16u, ALIGNOF(raw16)); | 23 EXPECT_EQ(16u, ALIGNOF(decltype(raw16))); |
24 EXPECT_EQ(256u, ALIGNOF(raw256)); | 24 EXPECT_EQ(256u, ALIGNOF(decltype(raw256))); |
25 EXPECT_EQ(4096u, ALIGNOF(raw4096)); | 25 EXPECT_EQ(4096u, ALIGNOF(decltype(raw4096))); |
26 | 26 |
27 EXPECT_ALIGNED(raw8.void_data(), 8); | 27 EXPECT_ALIGNED(raw8.void_data(), 8); |
28 EXPECT_ALIGNED(raw16.void_data(), 16); | 28 EXPECT_ALIGNED(raw16.void_data(), 16); |
29 EXPECT_ALIGNED(raw256.void_data(), 256); | 29 EXPECT_ALIGNED(raw256.void_data(), 256); |
30 EXPECT_ALIGNED(raw4096.void_data(), 4096); | 30 EXPECT_ALIGNED(raw4096.void_data(), 4096); |
31 } | 31 } |
32 | 32 |
33 TEST(AlignedMemoryTest, StackAlignment) { | 33 TEST(AlignedMemoryTest, StackAlignment) { |
34 AlignedMemory<8, 8> raw8; | 34 AlignedMemory<8, 8> raw8; |
35 AlignedMemory<8, 16> raw16; | 35 AlignedMemory<8, 16> raw16; |
36 AlignedMemory<8, 128> raw128; | 36 AlignedMemory<8, 128> raw128; |
37 | 37 |
38 EXPECT_EQ(8u, ALIGNOF(raw8)); | 38 EXPECT_EQ(8u, ALIGNOF(decltype(raw8))); |
39 EXPECT_EQ(16u, ALIGNOF(raw16)); | 39 EXPECT_EQ(16u, ALIGNOF(decltype(raw16))); |
40 EXPECT_EQ(128u, ALIGNOF(raw128)); | 40 EXPECT_EQ(128u, ALIGNOF(decltype(raw128))); |
41 | 41 |
42 EXPECT_ALIGNED(raw8.void_data(), 8); | 42 EXPECT_ALIGNED(raw8.void_data(), 8); |
43 EXPECT_ALIGNED(raw16.void_data(), 16); | 43 EXPECT_ALIGNED(raw16.void_data(), 16); |
44 | 44 |
45 // TODO(ios): __attribute__((aligned(X))) with X >= 128 does not works on | 45 // TODO(ios): __attribute__((aligned(X))) with X >= 128 does not works on |
46 // the stack when building for arm64 on iOS, http://crbug.com/349003 | 46 // the stack when building for arm64 on iOS, http://crbug.com/349003 |
47 #if !(defined(OS_IOS) && defined(ARCH_CPU_ARM64)) | 47 #if !(defined(OS_IOS) && defined(ARCH_CPU_ARM64)) |
48 EXPECT_ALIGNED(raw128.void_data(), 128); | 48 EXPECT_ALIGNED(raw128.void_data(), 128); |
49 | 49 |
50 // NaCl x86-64 compiler emits non-validating instructions for >128 | 50 // NaCl x86-64 compiler emits non-validating instructions for >128 |
51 // bytes alignment. | 51 // bytes alignment. |
52 // http://www.chromium.org/nativeclient/design-documents/nacl-sfi-model-on-x86
-64-systems | 52 // http://www.chromium.org/nativeclient/design-documents/nacl-sfi-model-on-x86
-64-systems |
53 // TODO(hamaji): Ideally, NaCl compiler for x86-64 should workaround | 53 // TODO(hamaji): Ideally, NaCl compiler for x86-64 should workaround |
54 // this limitation and this #if should be removed. | 54 // this limitation and this #if should be removed. |
55 // https://code.google.com/p/nativeclient/issues/detail?id=3463 | 55 // https://code.google.com/p/nativeclient/issues/detail?id=3463 |
56 #if !(defined(OS_NACL) && defined(ARCH_CPU_X86_64)) | 56 #if !(defined(OS_NACL) && defined(ARCH_CPU_X86_64)) |
57 AlignedMemory<8, 256> raw256; | 57 AlignedMemory<8, 256> raw256; |
58 EXPECT_EQ(256u, ALIGNOF(raw256)); | 58 EXPECT_EQ(256u, ALIGNOF(decltype(raw256))); |
59 EXPECT_ALIGNED(raw256.void_data(), 256); | 59 EXPECT_ALIGNED(raw256.void_data(), 256); |
60 | 60 |
61 // TODO(ios): This test hits an armv7 bug in clang. crbug.com/138066 | 61 // TODO(ios): This test hits an armv7 bug in clang. crbug.com/138066 |
62 #if !(defined(OS_IOS) && defined(ARCH_CPU_ARM_FAMILY)) | 62 #if !(defined(OS_IOS) && defined(ARCH_CPU_ARM_FAMILY)) |
63 AlignedMemory<8, 4096> raw4096; | 63 AlignedMemory<8, 4096> raw4096; |
64 EXPECT_EQ(4096u, ALIGNOF(raw4096)); | 64 EXPECT_EQ(4096u, ALIGNOF(decltype(raw4096))); |
65 EXPECT_ALIGNED(raw4096.void_data(), 4096); | 65 EXPECT_ALIGNED(raw4096.void_data(), 4096); |
66 #endif // !(defined(OS_IOS) && defined(ARCH_CPU_ARM_FAMILY)) | 66 #endif // !(defined(OS_IOS) && defined(ARCH_CPU_ARM_FAMILY)) |
67 #endif // !(defined(OS_NACL) && defined(ARCH_CPU_X86_64)) | 67 #endif // !(defined(OS_NACL) && defined(ARCH_CPU_X86_64)) |
68 #endif // !(defined(OS_IOS) && defined(ARCH_CPU_ARM64)) | 68 #endif // !(defined(OS_IOS) && defined(ARCH_CPU_ARM64)) |
69 } | 69 } |
70 | 70 |
71 TEST(AlignedMemoryTest, DynamicAllocation) { | 71 TEST(AlignedMemoryTest, DynamicAllocation) { |
72 void* p = base::AlignedAlloc(8, 8); | 72 void* p = base::AlignedAlloc(8, 8); |
73 EXPECT_TRUE(p); | 73 EXPECT_TRUE(p); |
74 EXPECT_ALIGNED(p, 8); | 74 EXPECT_ALIGNED(p, 8); |
(...skipping 16 matching lines...) Expand all Loading... |
91 } | 91 } |
92 | 92 |
93 TEST(AlignedMemoryTest, ScopedDynamicAllocation) { | 93 TEST(AlignedMemoryTest, ScopedDynamicAllocation) { |
94 scoped_ptr<float, base::AlignedFreeDeleter> p( | 94 scoped_ptr<float, base::AlignedFreeDeleter> p( |
95 static_cast<float*>(base::AlignedAlloc(8, 8))); | 95 static_cast<float*>(base::AlignedAlloc(8, 8))); |
96 EXPECT_TRUE(p.get()); | 96 EXPECT_TRUE(p.get()); |
97 EXPECT_ALIGNED(p.get(), 8); | 97 EXPECT_ALIGNED(p.get(), 8); |
98 } | 98 } |
99 | 99 |
100 } // namespace | 100 } // namespace |
OLD | NEW |