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