blob: 16ee0f0aacf7f0e69b6104f3937ad22fadd6b9aa [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
Tom Sepeze29ac692023-10-30 23:38:237#include <string.h>
8
dcheng093de9b2016-04-04 21:25:519#include <memory>
10
avi9beac252015-12-24 08:44:4711#include "build/build_config.h"
[email protected]cd924d62012-02-23 17:52:2012#include "testing/gtest/include/gtest/gtest.h"
13
brettw16289b3e2017-06-13 21:58:4014namespace base {
[email protected]cd924d62012-02-23 17:52:2015
[email protected]9f01b022012-07-26 02:22:3916TEST(AlignedMemoryTest, DynamicAllocation) {
brettw16289b3e2017-06-13 21:58:4017 void* p = AlignedAlloc(8, 8);
Tom Sepeze29ac692023-10-30 23:38:2318 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4119 EXPECT_TRUE(IsAligned(p, 8));
Tom Sepeze29ac692023-10-30 23:38:2320 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4021 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3922
brettw16289b3e2017-06-13 21:58:4023 p = AlignedAlloc(8, 16);
Tom Sepeze29ac692023-10-30 23:38:2324 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4125 EXPECT_TRUE(IsAligned(p, 16));
Tom Sepeze29ac692023-10-30 23:38:2326 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4027 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3928
brettw16289b3e2017-06-13 21:58:4029 p = AlignedAlloc(8, 256);
Tom Sepeze29ac692023-10-30 23:38:2330 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4131 EXPECT_TRUE(IsAligned(p, 256));
Tom Sepeze29ac692023-10-30 23:38:2332 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4033 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3934
brettw16289b3e2017-06-13 21:58:4035 p = AlignedAlloc(8, 4096);
Tom Sepeze29ac692023-10-30 23:38:2336 ASSERT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4137 EXPECT_TRUE(IsAligned(p, 4096));
Tom Sepeze29ac692023-10-30 23:38:2338 memset(p, 0, 8); // Fill to check allocated size under ASAN.
brettw16289b3e2017-06-13 21:58:4039 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3940}
41
42TEST(AlignedMemoryTest, ScopedDynamicAllocation) {
brettw16289b3e2017-06-13 21:58:4043 std::unique_ptr<float, AlignedFreeDeleter> p(
44 static_cast<float*>(AlignedAlloc(8, 8)));
[email protected]9f01b022012-07-26 02:22:3945 EXPECT_TRUE(p.get());
Brian Geffonae8ef4dd2020-04-06 19:48:4146 EXPECT_TRUE(IsAligned(p.get(), 8));
Lei Zhangdf4345c72020-06-15 22:07:0047
48 // Make sure IsAligned() can check const pointers as well.
49 const float* const_p = p.get();
50 EXPECT_TRUE(IsAligned(const_p, 8));
Brian Geffonae8ef4dd2020-04-06 19:48:4151}
52
53TEST(AlignedMemoryTest, IsAligned) {
54 // Check alignment around powers of two.
55 for (int i = 0; i < 64; ++i) {
56 const uint64_t n = static_cast<uint64_t>(1) << i;
57
58 // Walk back down all lower powers of two checking alignment.
59 for (int j = i - 1; j >= 0; --j) {
60 // n is aligned on all powers of two less than or equal to 2^i.
61 EXPECT_TRUE(IsAligned(n, n >> j))
62 << "Expected " << n << " to be " << (n >> j) << " aligned";
63
64 // Also, n - 1 should not be aligned on ANY lower power of two except 1
65 // (but since we're starting from i - 1 we don't test that case here.
66 EXPECT_FALSE(IsAligned(n - 1, n >> j))
67 << "Expected " << (n - 1) << " to NOT be " << (n >> j) << " aligned";
68 }
69 }
70
71 // And a few hard coded smoke tests for completeness:
72 EXPECT_TRUE(IsAligned(4, 2));
73 EXPECT_TRUE(IsAligned(8, 4));
74 EXPECT_TRUE(IsAligned(8, 2));
75 EXPECT_TRUE(IsAligned(0x1000, 4 << 10));
76 EXPECT_TRUE(IsAligned(0x2000, 8 << 10));
77 EXPECT_TRUE(IsAligned(1, 1));
78 EXPECT_TRUE(IsAligned(7, 1));
79 EXPECT_TRUE(IsAligned(reinterpret_cast<void*>(0x1000), 4 << 10));
80 EXPECT_TRUE(IsAligned(reinterpret_cast<int*>(0x1000), 4 << 10));
81
82 EXPECT_FALSE(IsAligned(3, 2));
83 EXPECT_FALSE(IsAligned(7, 4));
84 EXPECT_FALSE(IsAligned(7, 2));
85 EXPECT_FALSE(IsAligned(0x1001, 4 << 10));
86 EXPECT_FALSE(IsAligned(0x999, 8 << 10));
87 EXPECT_FALSE(IsAligned(7, 8));
[email protected]9f01b022012-07-26 02:22:3988}
89
brettw16289b3e2017-06-13 21:58:4090} // namespace base