Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: base/memory/aligned_memory_unittest.cc

Issue 2932053002: Use C++11 alignment primitives (Closed)
Patch Set: Fix merge Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/memory/aligned_memory.h ('k') | base/memory/manual_constructor.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 base {
16
17 using base::AlignedMemory;
18
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
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 EXPECT_ALIGNED(raw128.void_data(), 128);
48
49 // NaCl x86-64 compiler emits non-validating instructions for >128
50 // bytes alignment.
51 // http://www.chromium.org/nativeclient/design-documents/nacl-sfi-model-on-x86 -64-systems
52 // TODO(hamaji): Ideally, NaCl compiler for x86-64 should workaround
53 // this limitation and this #if should be removed.
54 // https://code.google.com/p/nativeclient/issues/detail?id=3463
55 #if !(defined(OS_NACL) && defined(ARCH_CPU_X86_64))
56 AlignedMemory<8, 256> raw256;
57 EXPECT_EQ(256u, ALIGNOF(raw256));
58 EXPECT_ALIGNED(raw256.void_data(), 256);
59
60 AlignedMemory<8, 4096> raw4096;
61 EXPECT_EQ(4096u, ALIGNOF(raw4096));
62 EXPECT_ALIGNED(raw4096.void_data(), 4096);
63 #endif // !(defined(OS_NACL) && defined(ARCH_CPU_X86_64))
64 }
65 16
66 TEST(AlignedMemoryTest, DynamicAllocation) { 17 TEST(AlignedMemoryTest, DynamicAllocation) {
67 void* p = base::AlignedAlloc(8, 8); 18 void* p = AlignedAlloc(8, 8);
68 EXPECT_TRUE(p); 19 EXPECT_TRUE(p);
69 EXPECT_ALIGNED(p, 8); 20 EXPECT_ALIGNED(p, 8);
70 base::AlignedFree(p); 21 AlignedFree(p);
71 22
72 p = base::AlignedAlloc(8, 16); 23 p = AlignedAlloc(8, 16);
73 EXPECT_TRUE(p); 24 EXPECT_TRUE(p);
74 EXPECT_ALIGNED(p, 16); 25 EXPECT_ALIGNED(p, 16);
75 base::AlignedFree(p); 26 AlignedFree(p);
76 27
77 p = base::AlignedAlloc(8, 256); 28 p = AlignedAlloc(8, 256);
78 EXPECT_TRUE(p); 29 EXPECT_TRUE(p);
79 EXPECT_ALIGNED(p, 256); 30 EXPECT_ALIGNED(p, 256);
80 base::AlignedFree(p); 31 AlignedFree(p);
81 32
82 p = base::AlignedAlloc(8, 4096); 33 p = AlignedAlloc(8, 4096);
83 EXPECT_TRUE(p); 34 EXPECT_TRUE(p);
84 EXPECT_ALIGNED(p, 4096); 35 EXPECT_ALIGNED(p, 4096);
85 base::AlignedFree(p); 36 AlignedFree(p);
86 } 37 }
87 38
88 TEST(AlignedMemoryTest, ScopedDynamicAllocation) { 39 TEST(AlignedMemoryTest, ScopedDynamicAllocation) {
89 std::unique_ptr<float, base::AlignedFreeDeleter> p( 40 std::unique_ptr<float, AlignedFreeDeleter> p(
90 static_cast<float*>(base::AlignedAlloc(8, 8))); 41 static_cast<float*>(AlignedAlloc(8, 8)));
91 EXPECT_TRUE(p.get()); 42 EXPECT_TRUE(p.get());
92 EXPECT_ALIGNED(p.get(), 8); 43 EXPECT_ALIGNED(p.get(), 8);
93 } 44 }
94 45
95 } // namespace 46 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/aligned_memory.h ('k') | base/memory/manual_constructor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698