blob: f9b2bc6f0ca4aaef094904a4f1ce380833a3213a [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]cd924d62012-02-23 17:52:202// 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"
dcheng093de9b2016-04-04 21:25:516
danakjb4705072024-11-06 20:54:097#include <stdint.h>
Tom Sepeze29ac692023-10-30 23:38:238#include <string.h>
9
dcheng093de9b2016-04-04 21:25:5110#include <memory>
11
avi9beac252015-12-24 08:44:4712#include "build/build_config.h"
[email protected]cd924d62012-02-23 17:52:2013#include "testing/gtest/include/gtest/gtest.h"
14
brettw16289b3e2017-06-13 21:58:4015namespace base {
[email protected]cd924d62012-02-23 17:52:2016
danakjb4705072024-11-06 20:54:0917TEST(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
30TEST(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
[email protected]9f01b022012-07-26 02:22:3940TEST(AlignedMemoryTest, DynamicAllocation) {
brettw16289b3e2017-06-13 21:58:4041 void* p = AlignedAlloc(8, 8);
Tom Sepeze29ac692023-10-30 23:38:2342 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4143 EXPECT_TRUE(IsAligned(p, 8));
Tom Sepeze29ac692023-10-30 23:38:2344 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4045 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3946
brettw16289b3e2017-06-13 21:58:4047 p = AlignedAlloc(8, 16);
Tom Sepeze29ac692023-10-30 23:38:2348 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4149 EXPECT_TRUE(IsAligned(p, 16));
Tom Sepeze29ac692023-10-30 23:38:2350 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4051 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3952
brettw16289b3e2017-06-13 21:58:4053 p = AlignedAlloc(8, 256);
Tom Sepeze29ac692023-10-30 23:38:2354 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4155 EXPECT_TRUE(IsAligned(p, 256));
Tom Sepeze29ac692023-10-30 23:38:2356 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4057 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3958
brettw16289b3e2017-06-13 21:58:4059 p = AlignedAlloc(8, 4096);
Tom Sepeze29ac692023-10-30 23:38:2360 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4161 EXPECT_TRUE(IsAligned(p, 4096));
Tom Sepeze29ac692023-10-30 23:38:2362 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4063 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3964}
65
66TEST(AlignedMemoryTest, ScopedDynamicAllocation) {
brettw16289b3e2017-06-13 21:58:4067 std::unique_ptr<float, AlignedFreeDeleter> p(
68 static_cast<float*>(AlignedAlloc(8, 8)));
[email protected]9f01b022012-07-26 02:22:3969 EXPECT_TRUE(p.get());
Brian Geffonae8ef4dd2020-04-06 19:48:4170 EXPECT_TRUE(IsAligned(p.get(), 8));
Lei Zhangdf4345c72020-06-15 22:07:0071
72 // Make sure IsAligned() can check const pointers as well.
73 const float* const_p = p.get();
74 EXPECT_TRUE(IsAligned(const_p, 8));
Brian Geffonae8ef4dd2020-04-06 19:48:4175}
76
77TEST(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));
[email protected]9f01b022012-07-26 02:22:39112}
113
brettw16289b3e2017-06-13 21:58:40114} // namespace base