1 // Copyright 2012 The Chromium Authors
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 <stdint.h>
8 #include <string.h>
9
10 #include <memory>
11
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16
TEST(AlignedMemoryTest,AlignedUninit)17 TEST(AlignedMemoryTest, AlignedUninit) {
18 {
19 base::AlignedHeapArray<char> h = AlignedUninit<char>(8, 32);
20 EXPECT_EQ(h.size(), 8u);
21 EXPECT_TRUE(IsAligned(h.data(), 32));
22 }
23 {
24 base::AlignedHeapArray<int16_t> h = AlignedUninit<int16_t>(8, 32);
25 EXPECT_EQ(h.size(), 8u);
26 EXPECT_TRUE(IsAligned(h.data(), 32));
27 }
28 }
29
TEST(AlignedMemoryTest,AlignedUninitCharArray)30 TEST(AlignedMemoryTest, AlignedUninitCharArray) {
31 auto [h, s] = AlignedUninitCharArray<int16_t>(8, 32);
32 static_assert(std::same_as<base::AlignedHeapArray<char>, decltype(h)>);
33 static_assert(std::same_as<base::span<int16_t>, decltype(s)>);
34 EXPECT_EQ(h.size(), 8u * sizeof(int16_t));
35 EXPECT_TRUE(IsAligned(h.data(), 32));
36 EXPECT_EQ(s.size(), 8u);
37 EXPECT_TRUE(IsAligned(s.data(), 32));
38 }
39
TEST(AlignedMemoryTest,DynamicAllocation)40 TEST(AlignedMemoryTest, DynamicAllocation) {
41 void* p = AlignedAlloc(8, 8);
42 ASSERT_TRUE(p);
43 EXPECT_TRUE(IsAligned(p, 8));
44 memset(p, 0, 8); // Fill to check allocated size under ASAN.
45 AlignedFree(p);
46
47 p = AlignedAlloc(8, 16);
48 ASSERT_TRUE(p);
49 EXPECT_TRUE(IsAligned(p, 16));
50 memset(p, 0, 8); // Fill to check allocated size under ASAN.
51 AlignedFree(p);
52
53 p = AlignedAlloc(8, 256);
54 ASSERT_TRUE(p);
55 EXPECT_TRUE(IsAligned(p, 256));
56 memset(p, 0, 8); // Fill to check allocated size under ASAN.
57 AlignedFree(p);
58
59 p = AlignedAlloc(8, 4096);
60 ASSERT_TRUE(p);
61 EXPECT_TRUE(IsAligned(p, 4096));
62 memset(p, 0, 8); // Fill to check allocated size under ASAN.
63 AlignedFree(p);
64 }
65
TEST(AlignedMemoryTest,ScopedDynamicAllocation)66 TEST(AlignedMemoryTest, ScopedDynamicAllocation) {
67 std::unique_ptr<float, AlignedFreeDeleter> p(
68 static_cast<float*>(AlignedAlloc(8, 8)));
69 EXPECT_TRUE(p.get());
70 EXPECT_TRUE(IsAligned(p.get(), 8));
71
72 // Make sure IsAligned() can check const pointers as well.
73 const float* const_p = p.get();
74 EXPECT_TRUE(IsAligned(const_p, 8));
75 }
76
TEST(AlignedMemoryTest,IsAligned)77 TEST(AlignedMemoryTest, IsAligned) {
78 // Check alignment around powers of two.
79 for (int i = 0; i < 64; ++i) {
80 const uint64_t n = static_cast<uint64_t>(1) << i;
81
82 // Walk back down all lower powers of two checking alignment.
83 for (int j = i - 1; j >= 0; --j) {
84 // n is aligned on all powers of two less than or equal to 2^i.
85 EXPECT_TRUE(IsAligned(n, n >> j))
86 << "Expected " << n << " to be " << (n >> j) << " aligned";
87
88 // Also, n - 1 should not be aligned on ANY lower power of two except 1
89 // (but since we're starting from i - 1 we don't test that case here.
90 EXPECT_FALSE(IsAligned(n - 1, n >> j))
91 << "Expected " << (n - 1) << " to NOT be " << (n >> j) << " aligned";
92 }
93 }
94
95 // And a few hard coded smoke tests for completeness:
96 EXPECT_TRUE(IsAligned(4, 2));
97 EXPECT_TRUE(IsAligned(8, 4));
98 EXPECT_TRUE(IsAligned(8, 2));
99 EXPECT_TRUE(IsAligned(0x1000, 4 << 10));
100 EXPECT_TRUE(IsAligned(0x2000, 8 << 10));
101 EXPECT_TRUE(IsAligned(1, 1));
102 EXPECT_TRUE(IsAligned(7, 1));
103 EXPECT_TRUE(IsAligned(reinterpret_cast<void*>(0x1000), 4 << 10));
104 EXPECT_TRUE(IsAligned(reinterpret_cast<int*>(0x1000), 4 << 10));
105
106 EXPECT_FALSE(IsAligned(3, 2));
107 EXPECT_FALSE(IsAligned(7, 4));
108 EXPECT_FALSE(IsAligned(7, 2));
109 EXPECT_FALSE(IsAligned(0x1001, 4 << 10));
110 EXPECT_FALSE(IsAligned(0x999, 8 << 10));
111 EXPECT_FALSE(IsAligned(7, 8));
112 }
113
114 } // namespace base
115