• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #include <memory>
8 
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 #define EXPECT_ALIGNED(ptr, align) \
13     EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
14 
15 namespace {
16 
17 using base::AlignedMemory;
18 
TEST(AlignedMemoryTest,StaticAlignment)19 TEST(AlignedMemoryTest, StaticAlignment) {
20   static AlignedMemory<8, 8> raw8;
21   static AlignedMemory<8, 16> raw16;
22   static AlignedMemory<8, 256> raw256;
23   static AlignedMemory<8, 4096> raw4096;
24 
25   EXPECT_EQ(8u, ALIGNOF(raw8));
26   EXPECT_EQ(16u, ALIGNOF(raw16));
27   EXPECT_EQ(256u, ALIGNOF(raw256));
28   EXPECT_EQ(4096u, ALIGNOF(raw4096));
29 
30   EXPECT_ALIGNED(raw8.void_data(), 8);
31   EXPECT_ALIGNED(raw16.void_data(), 16);
32   EXPECT_ALIGNED(raw256.void_data(), 256);
33   EXPECT_ALIGNED(raw4096.void_data(), 4096);
34 }
35 
TEST(AlignedMemoryTest,StackAlignment)36 TEST(AlignedMemoryTest, StackAlignment) {
37   AlignedMemory<8, 8> raw8;
38   AlignedMemory<8, 16> raw16;
39   AlignedMemory<8, 128> raw128;
40 
41   EXPECT_EQ(8u, ALIGNOF(raw8));
42   EXPECT_EQ(16u, ALIGNOF(raw16));
43   EXPECT_EQ(128u, ALIGNOF(raw128));
44 
45   EXPECT_ALIGNED(raw8.void_data(), 8);
46   EXPECT_ALIGNED(raw16.void_data(), 16);
47 
48   // TODO(ios): __attribute__((aligned(X))) with X >= 128 does not works on
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 }
73 
TEST(AlignedMemoryTest,DynamicAllocation)74 TEST(AlignedMemoryTest, DynamicAllocation) {
75   void* p = base::AlignedAlloc(8, 8);
76   EXPECT_TRUE(p);
77   EXPECT_ALIGNED(p, 8);
78   base::AlignedFree(p);
79 
80   p = base::AlignedAlloc(8, 16);
81   EXPECT_TRUE(p);
82   EXPECT_ALIGNED(p, 16);
83   base::AlignedFree(p);
84 
85   p = base::AlignedAlloc(8, 256);
86   EXPECT_TRUE(p);
87   EXPECT_ALIGNED(p, 256);
88   base::AlignedFree(p);
89 
90   p = base::AlignedAlloc(8, 4096);
91   EXPECT_TRUE(p);
92   EXPECT_ALIGNED(p, 4096);
93   base::AlignedFree(p);
94 }
95 
TEST(AlignedMemoryTest,ScopedDynamicAllocation)96 TEST(AlignedMemoryTest, ScopedDynamicAllocation) {
97   std::unique_ptr<float, base::AlignedFreeDeleter> p(
98       static_cast<float*>(base::AlignedAlloc(8, 8)));
99   EXPECT_TRUE(p.get());
100   EXPECT_ALIGNED(p.get(), 8);
101 }
102 
103 }  // namespace
104