OLD | NEW |
1 // Copyright 2014 Google Inc. All Rights Reserved. | 1 // Copyright 2014 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 TestLargeBlockHeap h; | 63 TestLargeBlockHeap h; |
64 EXPECT_EQ(0u, h.size()); | 64 EXPECT_EQ(0u, h.size()); |
65 | 65 |
66 BlockLayout layout = {}; | 66 BlockLayout layout = {}; |
67 BlockInfo block = {}; | 67 BlockInfo block = {}; |
68 | 68 |
69 // Allocate and free a zero-sized allocation. This should succeed by | 69 // Allocate and free a zero-sized allocation. This should succeed by |
70 // definition. | 70 // definition. |
71 void* alloc = h.AllocateBlock(0, 0, 0, &layout); | 71 void* alloc = h.AllocateBlock(0, 0, 0, &layout); |
72 EXPECT_EQ(1u, h.size()); | 72 EXPECT_EQ(1u, h.size()); |
73 BlockInitialize(layout, alloc, false, &block); | 73 BlockInitialize(layout, alloc, &block); |
74 EXPECT_TRUE(h.FreeBlock(block)); | 74 EXPECT_TRUE(h.FreeBlock(block)); |
75 EXPECT_EQ(0u, h.size()); | 75 EXPECT_EQ(0u, h.size()); |
76 | 76 |
77 // Make a bunch of different sized allocations. | 77 // Make a bunch of different sized allocations. |
78 BlockInfoSet blocks; | 78 BlockInfoSet blocks; |
79 for (uint32_t i = 1, j = 1; i < 1024 * 1024; i <<= 1, ++j) { | 79 for (uint32_t i = 1, j = 1; i < 1024 * 1024; i <<= 1, ++j) { |
80 void* alloc = h.AllocateBlock(i, 0, 0, &layout); | 80 void* alloc = h.AllocateBlock(i, 0, 0, &layout); |
81 EXPECT_EQ(j, h.size()); | 81 EXPECT_EQ(j, h.size()); |
82 EXPECT_EQ(0u, layout.block_size % GetPageSize()); | 82 EXPECT_EQ(0u, layout.block_size % GetPageSize()); |
83 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(alloc) % GetPageSize()); | 83 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(alloc) % GetPageSize()); |
84 EXPECT_LE(GetPageSize(), layout.header_size + layout.header_padding_size); | 84 EXPECT_LE(GetPageSize(), layout.header_size + layout.header_padding_size); |
85 EXPECT_EQ(i, layout.body_size); | 85 EXPECT_EQ(i, layout.body_size); |
86 EXPECT_LE(GetPageSize(), layout.trailer_padding_size + layout.trailer_size); | 86 EXPECT_LE(GetPageSize(), layout.trailer_padding_size + layout.trailer_size); |
87 BlockInitialize(layout, alloc, false, &block); | 87 BlockInitialize(layout, alloc, &block); |
88 blocks.insert(block); | 88 blocks.insert(block); |
89 } | 89 } |
90 | 90 |
91 // Now free them. | 91 // Now free them. |
92 BlockInfoSet::const_iterator it = blocks.begin(); | 92 BlockInfoSet::const_iterator it = blocks.begin(); |
93 for (; it != blocks.end(); ++it) | 93 for (; it != blocks.end(); ++it) |
94 EXPECT_TRUE(h.FreeBlock(*it)); | 94 EXPECT_TRUE(h.FreeBlock(*it)); |
95 EXPECT_EQ(0u, h.size()); | 95 EXPECT_EQ(0u, h.size()); |
96 } | 96 } |
97 | 97 |
98 TEST(LargeBlockHeapTest, ZeroSizedAllocationsHaveDistinctAddresses) { | 98 TEST(LargeBlockHeapTest, ZeroSizedAllocationsHaveDistinctAddresses) { |
99 TestLargeBlockHeap h; | 99 TestLargeBlockHeap h; |
100 | 100 |
101 void* a1 = h.Allocate(0); | 101 void* a1 = h.Allocate(0); |
102 EXPECT_TRUE(a1 != NULL); | 102 EXPECT_TRUE(a1 != NULL); |
103 void* a2 = h.Allocate(0); | 103 void* a2 = h.Allocate(0); |
104 EXPECT_TRUE(a2 != NULL); | 104 EXPECT_TRUE(a2 != NULL); |
105 EXPECT_NE(a1, a2); | 105 EXPECT_NE(a1, a2); |
106 EXPECT_TRUE(h.Free(a1)); | 106 EXPECT_TRUE(h.Free(a1)); |
107 EXPECT_TRUE(h.Free(a2)); | 107 EXPECT_TRUE(h.Free(a2)); |
108 | 108 |
109 BlockLayout layout = {}; | 109 BlockLayout layout = {}; |
110 | 110 |
111 BlockInfo b1 = {}; | 111 BlockInfo b1 = {}; |
112 a1 = h.AllocateBlock(0, 0, 0, &layout); | 112 a1 = h.AllocateBlock(0, 0, 0, &layout); |
113 EXPECT_TRUE(a1 != NULL); | 113 EXPECT_TRUE(a1 != NULL); |
114 BlockInitialize(layout, a1, false, &b1); | 114 BlockInitialize(layout, a1, &b1); |
115 | 115 |
116 BlockInfo b2 = {}; | 116 BlockInfo b2 = {}; |
117 a2 = h.AllocateBlock(0, 0, 0, &layout); | 117 a2 = h.AllocateBlock(0, 0, 0, &layout); |
118 EXPECT_TRUE(a2 != NULL); | 118 EXPECT_TRUE(a2 != NULL); |
119 BlockInitialize(layout, a2, false, &b2); | 119 BlockInitialize(layout, a2, &b2); |
120 | 120 |
121 EXPECT_NE(a1, a2); | 121 EXPECT_NE(a1, a2); |
122 EXPECT_NE(b1.header, b2.header); | 122 EXPECT_NE(b1.header, b2.header); |
123 | 123 |
124 EXPECT_TRUE(h.FreeBlock(b1)); | 124 EXPECT_TRUE(h.FreeBlock(b1)); |
125 EXPECT_TRUE(h.FreeBlock(b2)); | 125 EXPECT_TRUE(h.FreeBlock(b2)); |
126 } | 126 } |
127 | 127 |
128 TEST(LargeBlockHeapTest, IsAllocated) { | 128 TEST(LargeBlockHeapTest, IsAllocated) { |
129 TestLargeBlockHeap h; | 129 TestLargeBlockHeap h; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // fail due to a CHECK in the LargeBlockHeap that ensure that there's no more | 175 // fail due to a CHECK in the LargeBlockHeap that ensure that there's no more |
176 // alive allocations after calling FreeAllAllocations. | 176 // alive allocations after calling FreeAllAllocations. |
177 for (size_t i = 0; i < kAllocCount; ++i) | 177 for (size_t i = 0; i < kAllocCount; ++i) |
178 h.Allocate(42); | 178 h.Allocate(42); |
179 EXPECT_EQ(kAllocCount, h.size()); | 179 EXPECT_EQ(kAllocCount, h.size()); |
180 } | 180 } |
181 | 181 |
182 } // namespace heaps | 182 } // namespace heaps |
183 } // namespace asan | 183 } // namespace asan |
184 } // namespace agent | 184 } // namespace agent |
OLD | NEW |