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