| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 17 | #include "base/arena_allocator-inl.h" |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 18 | #include "base/arena_bit_vector.h" |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 19 | #include "base/memory_tool.h" |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | class ArenaAllocatorTest : public testing::Test { |
| 25 | protected: |
| Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 26 | size_t NumberOfArenas(ArenaAllocator* allocator) { |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 27 | size_t result = 0u; |
| Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 28 | for (Arena* a = allocator->arena_head_; a != nullptr; a = a->next_) { |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 29 | ++result; |
| 30 | } |
| 31 | return result; |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | TEST_F(ArenaAllocatorTest, Test) { |
| 36 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 37 | ArenaAllocator allocator(&pool); |
| 38 | ArenaBitVector bv(&allocator, 10, true); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 39 | bv.SetBit(5); |
| 40 | EXPECT_EQ(1U, bv.GetStorageSize()); |
| 41 | bv.SetBit(35); |
| 42 | EXPECT_EQ(2U, bv.GetStorageSize()); |
| 43 | } |
| 44 | |
| Vladimir Marko | 3f84f2c | 2016-04-25 19:40:34 +0100 | [diff] [blame] | 45 | TEST_F(ArenaAllocatorTest, MakeDefined) { |
| 46 | // Regression test to make sure we mark the allocated area defined. |
| 47 | ArenaPool pool; |
| 48 | static constexpr size_t kSmallArraySize = 10; |
| 49 | static constexpr size_t kLargeArraySize = 50; |
| 50 | uint32_t* small_array; |
| 51 | { |
| 52 | // Allocate a small array from an arena and release it. |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 53 | ArenaAllocator allocator(&pool); |
| 54 | small_array = allocator.AllocArray<uint32_t>(kSmallArraySize); |
| Vladimir Marko | 3f84f2c | 2016-04-25 19:40:34 +0100 | [diff] [blame] | 55 | ASSERT_EQ(0u, small_array[kSmallArraySize - 1u]); |
| 56 | } |
| 57 | { |
| 58 | // Reuse the previous arena and allocate more than previous allocation including red zone. |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 59 | ArenaAllocator allocator(&pool); |
| 60 | uint32_t* large_array = allocator.AllocArray<uint32_t>(kLargeArraySize); |
| Vladimir Marko | 3f84f2c | 2016-04-25 19:40:34 +0100 | [diff] [blame] | 61 | ASSERT_EQ(0u, large_array[kLargeArraySize - 1u]); |
| 62 | // Verify that the allocation was made on the same arena. |
| 63 | ASSERT_EQ(small_array, large_array); |
| 64 | } |
| 65 | } |
| 66 | |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 67 | TEST_F(ArenaAllocatorTest, LargeAllocations) { |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 68 | if (arena_allocator::kArenaAllocatorPreciseTracking) { |
| 69 | printf("WARNING: TEST DISABLED FOR precise arena tracking\n"); |
| 70 | return; |
| 71 | } |
| 72 | |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 73 | { |
| 74 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 75 | ArenaAllocator allocator(&pool); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 76 | // Note: Leaving some space for memory tool red zones. |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 77 | void* alloc1 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 5 / 8); |
| 78 | void* alloc2 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 2 / 8); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 79 | ASSERT_NE(alloc1, alloc2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 80 | ASSERT_EQ(1u, NumberOfArenas(&allocator)); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 81 | } |
| 82 | { |
| 83 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 84 | ArenaAllocator allocator(&pool); |
| 85 | void* alloc1 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 13 / 16); |
| 86 | void* alloc2 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 11 / 16); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 87 | ASSERT_NE(alloc1, alloc2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 88 | ASSERT_EQ(2u, NumberOfArenas(&allocator)); |
| 89 | void* alloc3 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 7 / 16); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 90 | ASSERT_NE(alloc1, alloc3); |
| 91 | ASSERT_NE(alloc2, alloc3); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 92 | ASSERT_EQ(3u, NumberOfArenas(&allocator)); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 93 | } |
| 94 | { |
| 95 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 96 | ArenaAllocator allocator(&pool); |
| 97 | void* alloc1 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 13 / 16); |
| 98 | void* alloc2 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 9 / 16); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 99 | ASSERT_NE(alloc1, alloc2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 100 | ASSERT_EQ(2u, NumberOfArenas(&allocator)); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 101 | // Note: Leaving some space for memory tool red zones. |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 102 | void* alloc3 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 5 / 16); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 103 | ASSERT_NE(alloc1, alloc3); |
| 104 | ASSERT_NE(alloc2, alloc3); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 105 | ASSERT_EQ(2u, NumberOfArenas(&allocator)); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 106 | } |
| 107 | { |
| 108 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 109 | ArenaAllocator allocator(&pool); |
| 110 | void* alloc1 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 9 / 16); |
| 111 | void* alloc2 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 13 / 16); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 112 | ASSERT_NE(alloc1, alloc2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 113 | ASSERT_EQ(2u, NumberOfArenas(&allocator)); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 114 | // Note: Leaving some space for memory tool red zones. |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 115 | void* alloc3 = allocator.Alloc(arena_allocator::kArenaDefaultSize * 5 / 16); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 116 | ASSERT_NE(alloc1, alloc3); |
| 117 | ASSERT_NE(alloc2, alloc3); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 118 | ASSERT_EQ(2u, NumberOfArenas(&allocator)); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 119 | } |
| 120 | { |
| 121 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 122 | ArenaAllocator allocator(&pool); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 123 | // Note: Leaving some space for memory tool red zones. |
| 124 | for (size_t i = 0; i != 15; ++i) { |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 125 | // Allocate 15 times from the same arena. |
| 126 | allocator.Alloc(arena_allocator::kArenaDefaultSize * 1 / 16); |
| 127 | ASSERT_EQ(i + 1u, NumberOfArenas(&allocator)); |
| 128 | // Allocate a separate arena. |
| 129 | allocator.Alloc(arena_allocator::kArenaDefaultSize * 17 / 16); |
| 130 | ASSERT_EQ(i + 2u, NumberOfArenas(&allocator)); |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 135 | TEST_F(ArenaAllocatorTest, AllocAlignment) { |
| 136 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 137 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 138 | for (size_t iterations = 0; iterations <= 10; ++iterations) { |
| 139 | for (size_t size = 1; size <= ArenaAllocator::kAlignment + 1; ++size) { |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 140 | void* allocation = allocator.Alloc(size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 141 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(allocation)) |
| 142 | << reinterpret_cast<uintptr_t>(allocation); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 147 | TEST_F(ArenaAllocatorTest, ReallocReuse) { |
| 148 | // Realloc does not reuse arenas when running under sanitization. So we cannot do those |
| 149 | if (RUNNING_ON_MEMORY_TOOL != 0) { |
| 150 | printf("WARNING: TEST DISABLED FOR MEMORY_TOOL\n"); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | { |
| 155 | // Case 1: small aligned allocation, aligned extend inside arena. |
| 156 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 157 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 158 | |
| 159 | const size_t original_size = ArenaAllocator::kAlignment * 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 160 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 161 | |
| 162 | const size_t new_size = ArenaAllocator::kAlignment * 3; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 163 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 164 | EXPECT_EQ(original_allocation, realloc_allocation); |
| 165 | } |
| 166 | |
| 167 | { |
| 168 | // Case 2: small aligned allocation, non-aligned extend inside arena. |
| 169 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 170 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 171 | |
| 172 | const size_t original_size = ArenaAllocator::kAlignment * 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 173 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 174 | |
| 175 | const size_t new_size = ArenaAllocator::kAlignment * 2 + (ArenaAllocator::kAlignment / 2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 176 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 177 | EXPECT_EQ(original_allocation, realloc_allocation); |
| 178 | } |
| 179 | |
| 180 | { |
| 181 | // Case 3: small non-aligned allocation, aligned extend inside arena. |
| 182 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 183 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 184 | |
| 185 | const size_t original_size = ArenaAllocator::kAlignment * 2 + (ArenaAllocator::kAlignment / 2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 186 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 187 | |
| 188 | const size_t new_size = ArenaAllocator::kAlignment * 4; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 189 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 190 | EXPECT_EQ(original_allocation, realloc_allocation); |
| 191 | } |
| 192 | |
| 193 | { |
| 194 | // Case 4: small non-aligned allocation, aligned non-extend inside arena. |
| 195 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 196 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 197 | |
| 198 | const size_t original_size = ArenaAllocator::kAlignment * 2 + (ArenaAllocator::kAlignment / 2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 199 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 200 | |
| 201 | const size_t new_size = ArenaAllocator::kAlignment * 3; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 202 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 203 | EXPECT_EQ(original_allocation, realloc_allocation); |
| 204 | } |
| 205 | |
| 206 | // The next part is brittle, as the default size for an arena is variable, and we don't know about |
| 207 | // sanitization. |
| 208 | |
| 209 | { |
| 210 | // Case 5: large allocation, aligned extend into next arena. |
| 211 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 212 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 213 | |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 214 | const size_t original_size = arena_allocator::kArenaDefaultSize - |
| 215 | ArenaAllocator::kAlignment * 5; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 216 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 217 | |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 218 | const size_t new_size = arena_allocator::kArenaDefaultSize + ArenaAllocator::kAlignment * 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 219 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 220 | EXPECT_NE(original_allocation, realloc_allocation); |
| 221 | } |
| 222 | |
| 223 | { |
| 224 | // Case 6: large allocation, non-aligned extend into next arena. |
| 225 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 226 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 227 | |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 228 | const size_t original_size = arena_allocator::kArenaDefaultSize - |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 229 | ArenaAllocator::kAlignment * 4 - |
| 230 | ArenaAllocator::kAlignment / 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 231 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 232 | |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 233 | const size_t new_size = arena_allocator::kArenaDefaultSize + |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 234 | ArenaAllocator::kAlignment * 2 + |
| 235 | ArenaAllocator::kAlignment / 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 236 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | 6206da5 | 2016-08-22 19:14:29 -0700 | [diff] [blame] | 237 | EXPECT_NE(original_allocation, realloc_allocation); |
| 238 | } |
| 239 | } |
| 240 | |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 241 | TEST_F(ArenaAllocatorTest, ReallocAlignment) { |
| 242 | { |
| 243 | // Case 1: small aligned allocation, aligned extend inside arena. |
| 244 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 245 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 246 | |
| 247 | const size_t original_size = ArenaAllocator::kAlignment * 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 248 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 249 | ASSERT_TRUE(IsAligned<ArenaAllocator::kAlignment>(original_allocation)); |
| 250 | |
| 251 | const size_t new_size = ArenaAllocator::kAlignment * 3; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 252 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 253 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(realloc_allocation)); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 254 | |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 255 | void* after_alloc = allocator.Alloc(1); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 256 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(after_alloc)); |
| 257 | } |
| 258 | |
| 259 | { |
| 260 | // Case 2: small aligned allocation, non-aligned extend inside arena. |
| 261 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 262 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 263 | |
| 264 | const size_t original_size = ArenaAllocator::kAlignment * 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 265 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 266 | ASSERT_TRUE(IsAligned<ArenaAllocator::kAlignment>(original_allocation)); |
| 267 | |
| 268 | const size_t new_size = ArenaAllocator::kAlignment * 2 + (ArenaAllocator::kAlignment / 2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 269 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 270 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(realloc_allocation)); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 271 | |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 272 | void* after_alloc = allocator.Alloc(1); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 273 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(after_alloc)); |
| 274 | } |
| 275 | |
| 276 | { |
| 277 | // Case 3: small non-aligned allocation, aligned extend inside arena. |
| 278 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 279 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 280 | |
| 281 | const size_t original_size = ArenaAllocator::kAlignment * 2 + (ArenaAllocator::kAlignment / 2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 282 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 283 | ASSERT_TRUE(IsAligned<ArenaAllocator::kAlignment>(original_allocation)); |
| 284 | |
| 285 | const size_t new_size = ArenaAllocator::kAlignment * 4; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 286 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 287 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(realloc_allocation)); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 288 | |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 289 | void* after_alloc = allocator.Alloc(1); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 290 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(after_alloc)); |
| 291 | } |
| 292 | |
| 293 | { |
| 294 | // Case 4: small non-aligned allocation, aligned non-extend inside arena. |
| 295 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 296 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 297 | |
| 298 | const size_t original_size = ArenaAllocator::kAlignment * 2 + (ArenaAllocator::kAlignment / 2); |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 299 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 300 | ASSERT_TRUE(IsAligned<ArenaAllocator::kAlignment>(original_allocation)); |
| 301 | |
| 302 | const size_t new_size = ArenaAllocator::kAlignment * 3; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 303 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 304 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(realloc_allocation)); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 305 | |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 306 | void* after_alloc = allocator.Alloc(1); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 307 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(after_alloc)); |
| 308 | } |
| 309 | |
| 310 | // The next part is brittle, as the default size for an arena is variable, and we don't know about |
| 311 | // sanitization. |
| 312 | |
| 313 | { |
| 314 | // Case 5: large allocation, aligned extend into next arena. |
| 315 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 316 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 317 | |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 318 | const size_t original_size = arena_allocator::kArenaDefaultSize - |
| 319 | ArenaAllocator::kAlignment * 5; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 320 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 321 | ASSERT_TRUE(IsAligned<ArenaAllocator::kAlignment>(original_allocation)); |
| 322 | |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 323 | const size_t new_size = arena_allocator::kArenaDefaultSize + ArenaAllocator::kAlignment * 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 324 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 325 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(realloc_allocation)); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 326 | |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 327 | void* after_alloc = allocator.Alloc(1); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 328 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(after_alloc)); |
| 329 | } |
| 330 | |
| 331 | { |
| 332 | // Case 6: large allocation, non-aligned extend into next arena. |
| 333 | ArenaPool pool; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 334 | ArenaAllocator allocator(&pool); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 335 | |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 336 | const size_t original_size = arena_allocator::kArenaDefaultSize - |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 337 | ArenaAllocator::kAlignment * 4 - |
| 338 | ArenaAllocator::kAlignment / 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 339 | void* original_allocation = allocator.Alloc(original_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 340 | ASSERT_TRUE(IsAligned<ArenaAllocator::kAlignment>(original_allocation)); |
| 341 | |
| Andreas Gampe | 121f148 | 2017-05-12 10:28:35 -0700 | [diff] [blame] | 342 | const size_t new_size = arena_allocator::kArenaDefaultSize + |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 343 | ArenaAllocator::kAlignment * 2 + |
| 344 | ArenaAllocator::kAlignment / 2; |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 345 | void* realloc_allocation = allocator.Realloc(original_allocation, original_size, new_size); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 346 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(realloc_allocation)); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 347 | |
| Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame^] | 348 | void* after_alloc = allocator.Alloc(1); |
| Andreas Gampe | c134ee7 | 2016-08-22 14:03:10 -0700 | [diff] [blame] | 349 | EXPECT_TRUE(IsAligned<ArenaAllocator::kAlignment>(after_alloc)); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | |
| Vladimir Marko | 3e0e717 | 2016-04-22 18:07:13 +0100 | [diff] [blame] | 354 | } // namespace art |