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