blob: 810f2e46ca7ede6378cc7ff3ae56229d9f11428c [file] [log] [blame]
[email protected]e22cb252012-07-20 14:13:431// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[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
7#include <memory>
8
avi9beac252015-12-24 08:44:479#include "build/build_config.h"
[email protected]cd924d62012-02-23 17:52:2010#include "testing/gtest/include/gtest/gtest.h"
11
brettw16289b3e2017-06-13 21:58:4012namespace base {
[email protected]cd924d62012-02-23 17:52:2013
[email protected]9f01b022012-07-26 02:22:3914TEST(AlignedMemoryTest, DynamicAllocation) {
brettw16289b3e2017-06-13 21:58:4015 void* p = AlignedAlloc(8, 8);
[email protected]9f01b022012-07-26 02:22:3916 EXPECT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4117 EXPECT_TRUE(IsAligned(p, 8));
brettw16289b3e2017-06-13 21:58:4018 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3919
brettw16289b3e2017-06-13 21:58:4020 p = AlignedAlloc(8, 16);
[email protected]9f01b022012-07-26 02:22:3921 EXPECT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4122 EXPECT_TRUE(IsAligned(p, 16));
brettw16289b3e2017-06-13 21:58:4023 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3924
brettw16289b3e2017-06-13 21:58:4025 p = AlignedAlloc(8, 256);
[email protected]9f01b022012-07-26 02:22:3926 EXPECT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4127 EXPECT_TRUE(IsAligned(p, 256));
brettw16289b3e2017-06-13 21:58:4028 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3929
brettw16289b3e2017-06-13 21:58:4030 p = AlignedAlloc(8, 4096);
[email protected]9f01b022012-07-26 02:22:3931 EXPECT_TRUE(p);
Brian Geffonae8ef4dd2020-04-06 19:48:4132 EXPECT_TRUE(IsAligned(p, 4096));
brettw16289b3e2017-06-13 21:58:4033 AlignedFree(p);
[email protected]9f01b022012-07-26 02:22:3934}
35
36TEST(AlignedMemoryTest, ScopedDynamicAllocation) {
brettw16289b3e2017-06-13 21:58:4037 std::unique_ptr<float, AlignedFreeDeleter> p(
38 static_cast<float*>(AlignedAlloc(8, 8)));
[email protected]9f01b022012-07-26 02:22:3939 EXPECT_TRUE(p.get());
Brian Geffonae8ef4dd2020-04-06 19:48:4140 EXPECT_TRUE(IsAligned(p.get(), 8));
Lei Zhangdf4345c72020-06-15 22:07:0041
42 // Make sure IsAligned() can check const pointers as well.
43 const float* const_p = p.get();
44 EXPECT_TRUE(IsAligned(const_p, 8));
Brian Geffonae8ef4dd2020-04-06 19:48:4145}
46
47TEST(AlignedMemoryTest, IsAligned) {
48 // Check alignment around powers of two.
49 for (int i = 0; i < 64; ++i) {
50 const uint64_t n = static_cast<uint64_t>(1) << i;
51
52 // Walk back down all lower powers of two checking alignment.
53 for (int j = i - 1; j >= 0; --j) {
54 // n is aligned on all powers of two less than or equal to 2^i.
55 EXPECT_TRUE(IsAligned(n, n >> j))
56 << "Expected " << n << " to be " << (n >> j) << " aligned";
57
58 // Also, n - 1 should not be aligned on ANY lower power of two except 1
59 // (but since we're starting from i - 1 we don't test that case here.
60 EXPECT_FALSE(IsAligned(n - 1, n >> j))
61 << "Expected " << (n - 1) << " to NOT be " << (n >> j) << " aligned";
62 }
63 }
64
65 // And a few hard coded smoke tests for completeness:
66 EXPECT_TRUE(IsAligned(4, 2));
67 EXPECT_TRUE(IsAligned(8, 4));
68 EXPECT_TRUE(IsAligned(8, 2));
69 EXPECT_TRUE(IsAligned(0x1000, 4 << 10));
70 EXPECT_TRUE(IsAligned(0x2000, 8 << 10));
71 EXPECT_TRUE(IsAligned(1, 1));
72 EXPECT_TRUE(IsAligned(7, 1));
73 EXPECT_TRUE(IsAligned(reinterpret_cast<void*>(0x1000), 4 << 10));
74 EXPECT_TRUE(IsAligned(reinterpret_cast<int*>(0x1000), 4 << 10));
75
76 EXPECT_FALSE(IsAligned(3, 2));
77 EXPECT_FALSE(IsAligned(7, 4));
78 EXPECT_FALSE(IsAligned(7, 2));
79 EXPECT_FALSE(IsAligned(0x1001, 4 << 10));
80 EXPECT_FALSE(IsAligned(0x999, 8 << 10));
81 EXPECT_FALSE(IsAligned(7, 8));
[email protected]9f01b022012-07-26 02:22:3982}
83
brettw16289b3e2017-06-13 21:58:4084} // namespace base