Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: util/stdlib/aligned_allocator_test.cc

Issue 1498133002: Add AlignedVector and use it for vector<MEMORY_BASIC_INFORMATION64> (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Address review feedback Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/stdlib/aligned_allocator.cc ('k') | util/util.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 NaturalAlignedStruct {
37 int i;
38 };
39
40 AlignedVector<NaturalAlignedStruct> natural_aligned_vector;
41 natural_aligned_vector.push_back(NaturalAlignedStruct());
42 EXPECT_TRUE(
43 IsAligned(&natural_aligned_vector[0], ALIGNOF(NaturalAlignedStruct)));
44
45 natural_aligned_vector.resize(3);
46 EXPECT_TRUE(
47 IsAligned(&natural_aligned_vector[0], ALIGNOF(NaturalAlignedStruct)));
48 EXPECT_TRUE(
49 IsAligned(&natural_aligned_vector[1], ALIGNOF(NaturalAlignedStruct)));
50 EXPECT_TRUE(
51 IsAligned(&natural_aligned_vector[2], ALIGNOF(NaturalAlignedStruct)));
52
53 // Test a structure that declares its own alignment.
54 struct ALIGNAS(32) AlignedStruct {
55 int i;
56 };
57 ASSERT_EQ(32u, ALIGNOF(AlignedStruct));
58
59 AlignedVector<AlignedStruct> aligned_vector;
60 aligned_vector.push_back(AlignedStruct());
61 EXPECT_TRUE(IsAligned(&aligned_vector[0], ALIGNOF(AlignedStruct)));
62
63 aligned_vector.resize(3);
64 EXPECT_TRUE(IsAligned(&aligned_vector[0], ALIGNOF(AlignedStruct)));
65 EXPECT_TRUE(IsAligned(&aligned_vector[1], ALIGNOF(AlignedStruct)));
66 EXPECT_TRUE(IsAligned(&aligned_vector[2], ALIGNOF(AlignedStruct)));
67
68 // Try a custom alignment. Since the structure itself doesn’t specify an
69 // alignment constraint, only the base address will be aligned to the
70 // requested boundary.
71 AlignedVector<NaturalAlignedStruct, 64> custom_aligned_vector;
72 custom_aligned_vector.push_back(NaturalAlignedStruct());
73 EXPECT_TRUE(IsAligned(&custom_aligned_vector[0], 64));
74
75 // Try a structure with a pretty big alignment request.
76 struct ALIGNAS(1024) BigAlignedStruct {
77 int i;
78 };
79 ASSERT_EQ(1024u, ALIGNOF(BigAlignedStruct));
80
81 AlignedVector<BigAlignedStruct> big_aligned_vector;
82 big_aligned_vector.push_back(BigAlignedStruct());
83 EXPECT_TRUE(IsAligned(&big_aligned_vector[0], ALIGNOF(BigAlignedStruct)));
84
85 big_aligned_vector.resize(3);
86 EXPECT_TRUE(IsAligned(&big_aligned_vector[0], ALIGNOF(BigAlignedStruct)));
87 EXPECT_TRUE(IsAligned(&big_aligned_vector[1], ALIGNOF(BigAlignedStruct)));
88 EXPECT_TRUE(IsAligned(&big_aligned_vector[2], ALIGNOF(BigAlignedStruct)));
89 }
90
91 void BadAlignmentTest() {
92 #if defined(OS_WIN)
93 // Suppress the assertion MessageBox() normally displayed by the CRT in debug
94 // mode.
95 int previous = _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
96
97 // In release mode, _CrtSetReportMode() is #defined to ((int)0), so |previous|
98 // would appear unused.
99 ALLOW_UNUSED_LOCAL(previous);
100 #endif
101
102 // Alignment constraints must be powers of 2. 7 is not valid.
103 AlignedVector<int, 7> bad_aligned_vector;
104 bad_aligned_vector.push_back(0);
105
106 #if defined(OS_WIN)
107 _CrtSetReportMode(_CRT_ASSERT, previous);
108 #endif
109 }
110
111 TEST(AlignedAllocatorDeathTest, BadAlignment) {
112 ASSERT_DEATH(BadAlignmentTest(), "");
113 }
114
115 } // namespace
116 } // namespace test
117 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/stdlib/aligned_allocator.cc ('k') | util/util.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698