OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/stdlib/aligned_allocator.h" |
| 16 |
| 17 #include <stdint.h> |
| 18 |
| 19 #include "gtest/gtest.h" |
| 20 |
| 21 #if defined(OS_WIN) |
| 22 #include <crtdbg.h> |
| 23 #endif |
| 24 |
| 25 namespace crashpad { |
| 26 namespace test { |
| 27 namespace { |
| 28 |
| 29 bool IsAligned(void* pointer, size_t alignment) { |
| 30 uintptr_t address = reinterpret_cast<uintptr_t>(pointer); |
| 31 return (address & (alignment - 1)) == 0; |
| 32 } |
| 33 |
| 34 TEST(AlignedAllocator, AlignedVector) { |
| 35 // Test a structure with natural alignment. |
| 36 struct UnalignedStruct { |
| 37 int i; |
| 38 }; |
| 39 |
| 40 AlignedVector<UnalignedStruct> unaligned_vector; |
| 41 unaligned_vector.push_back(UnalignedStruct()); |
| 42 EXPECT_TRUE(IsAligned(&unaligned_vector[0], ALIGNOF(UnalignedStruct))); |
| 43 |
| 44 unaligned_vector.resize(3); |
| 45 EXPECT_TRUE(IsAligned(&unaligned_vector[0], ALIGNOF(UnalignedStruct))); |
| 46 EXPECT_TRUE(IsAligned(&unaligned_vector[1], ALIGNOF(UnalignedStruct))); |
| 47 EXPECT_TRUE(IsAligned(&unaligned_vector[2], ALIGNOF(UnalignedStruct))); |
| 48 |
| 49 // Test a structure that declares its own alignment. |
| 50 struct ALIGNAS(32) AlignedStruct { |
| 51 int i; |
| 52 }; |
| 53 ASSERT_EQ(32u, ALIGNOF(AlignedStruct)); |
| 54 |
| 55 AlignedVector<AlignedStruct> aligned_vector; |
| 56 aligned_vector.push_back(AlignedStruct()); |
| 57 EXPECT_TRUE(IsAligned(&aligned_vector[0], ALIGNOF(AlignedStruct))); |
| 58 |
| 59 aligned_vector.resize(3); |
| 60 EXPECT_TRUE(IsAligned(&aligned_vector[0], ALIGNOF(AlignedStruct))); |
| 61 EXPECT_TRUE(IsAligned(&aligned_vector[1], ALIGNOF(AlignedStruct))); |
| 62 EXPECT_TRUE(IsAligned(&aligned_vector[2], ALIGNOF(AlignedStruct))); |
| 63 |
| 64 // Try a custom alignment. Since the structure itself doesn’t specify an |
| 65 // alignment constraint, only the base address will be aligned to the |
| 66 // requested boundary. |
| 67 AlignedVector<UnalignedStruct, 64> custom_aligned_vector; |
| 68 custom_aligned_vector.push_back(UnalignedStruct()); |
| 69 EXPECT_TRUE(IsAligned(&custom_aligned_vector[0], 64)); |
| 70 |
| 71 // Try a structure with a pretty big alignment request. |
| 72 struct ALIGNAS(1024) BigAlignedStruct { |
| 73 int i; |
| 74 }; |
| 75 ASSERT_EQ(1024u, ALIGNOF(BigAlignedStruct)); |
| 76 |
| 77 AlignedVector<BigAlignedStruct> big_aligned_vector; |
| 78 big_aligned_vector.push_back(BigAlignedStruct()); |
| 79 EXPECT_TRUE(IsAligned(&big_aligned_vector[0], ALIGNOF(BigAlignedStruct))); |
| 80 |
| 81 big_aligned_vector.resize(3); |
| 82 EXPECT_TRUE(IsAligned(&big_aligned_vector[0], ALIGNOF(BigAlignedStruct))); |
| 83 EXPECT_TRUE(IsAligned(&big_aligned_vector[1], ALIGNOF(BigAlignedStruct))); |
| 84 EXPECT_TRUE(IsAligned(&big_aligned_vector[2], ALIGNOF(BigAlignedStruct))); |
| 85 } |
| 86 |
| 87 void BadAlignmentTest() { |
| 88 #if defined(OS_WIN) |
| 89 // Suppress the assertion MessageBox() normally displayed by the CRT in debug |
| 90 // mode. |
| 91 int previous = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
| 92 |
| 93 // In release mode, _CrtSetReportMode() is #defined to ((int)0), so |previous| |
| 94 // would appear unused. |
| 95 ALLOW_UNUSED_LOCAL(previous); |
| 96 #endif |
| 97 |
| 98 // Alignment constraints must be powers of 2. 7 is not valid. |
| 99 AlignedVector<int, 7> bad_aligned_vector; |
| 100 bad_aligned_vector.push_back(0); |
| 101 |
| 102 #if defined(OS_WIN) |
| 103 _CrtSetReportMode(_CRT_ASSERT, previous); |
| 104 #endif |
| 105 } |
| 106 |
| 107 TEST(AlignedAllocatorDeathTest, BadAlignment) { |
| 108 ASSERT_DEATH(BadAlignmentTest(), ""); |
| 109 } |
| 110 |
| 111 } // namespace |
| 112 } // namespace test |
| 113 } // namespace crashpad |
OLD | NEW |