Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 4 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 14 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include "linker_allocator.h" |
Dimitry Ivanov | f857211 | 2016-07-13 10:24:06 -0700 | [diff] [blame] | 30 | #include "linker_debug.h" |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 31 | #include "linker.h" |
| 32 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #include <sys/mman.h> |
Elliott Hughes | 99d5465 | 2018-08-22 10:36:23 -0700 | [diff] [blame] | 35 | #include <sys/prctl.h> |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 36 | #include <unistd.h> |
| 37 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 38 | #include <async_safe/log.h> |
| 39 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 40 | // |
| 41 | // LinkerMemeoryAllocator is general purpose allocator |
| 42 | // designed to provide the same functionality as the malloc/free/realloc |
| 43 | // libc functions. |
| 44 | // |
| 45 | // On alloc: |
| 46 | // If size is >= 1k allocator proxies malloc call directly to mmap |
| 47 | // If size < 1k allocator uses SmallObjectAllocator for the size |
| 48 | // rounded up to the nearest power of two. |
| 49 | // |
| 50 | // On free: |
| 51 | // |
| 52 | // For a pointer allocated using proxy-to-mmap allocator unmaps |
| 53 | // the memory. |
| 54 | // |
| 55 | // For a pointer allocated using SmallObjectAllocator it adds |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 56 | // the block to free_blocks_list in the corresponding page. If the number of |
| 57 | // free pages reaches 2, SmallObjectAllocator munmaps one of the pages keeping |
| 58 | // the other one in reserve. |
| 59 | |
| 60 | // Memory management for large objects is fairly straightforward, but for small |
| 61 | // objects it is more complicated. If you are changing this code, one simple |
| 62 | // way to evaluate the memory usage change is by running 'dd' and examine the |
| 63 | // memory usage by 'showmap $(pidof dd)'. 'dd' is nice in that: |
| 64 | // 1. It links in quite a few libraries, so you get some linker memory use. |
| 65 | // 2. When run with no arguments, it sits waiting for input, so it is easy to |
| 66 | // examine its memory usage with showmap. |
| 67 | // 3. Since it does nothing while waiting for input, the memory usage is |
| 68 | // determinisitic. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 69 | |
| 70 | static const char kSignature[4] = {'L', 'M', 'A', 1}; |
| 71 | |
| 72 | static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2; |
| 73 | |
| 74 | // This type is used for large allocations (with size >1k) |
| 75 | static const uint32_t kLargeObject = 111; |
| 76 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 77 | // Allocated pointers must be at least 16-byte aligned. Round up the size of |
| 78 | // page_info to multiple of 16. |
| 79 | static constexpr size_t kPageInfoSize = __BIONIC_ALIGN(sizeof(page_info), 16); |
| 80 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 81 | static inline uint16_t log2(size_t number) { |
| 82 | uint16_t result = 0; |
| 83 | number--; |
| 84 | |
| 85 | while (number != 0) { |
| 86 | result++; |
| 87 | number >>= 1; |
| 88 | } |
| 89 | |
| 90 | return result; |
| 91 | } |
| 92 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 93 | LinkerSmallObjectAllocator::LinkerSmallObjectAllocator(uint32_t type, |
| 94 | size_t block_size) |
| 95 | : type_(type), |
| 96 | block_size_(block_size), |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 97 | blocks_per_page_((PAGE_SIZE - sizeof(small_object_page_info)) / |
| 98 | block_size), |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 99 | free_pages_cnt_(0), |
| 100 | page_list_(nullptr) {} |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 101 | |
| 102 | void* LinkerSmallObjectAllocator::alloc() { |
Dimitry Ivanov | f857211 | 2016-07-13 10:24:06 -0700 | [diff] [blame] | 103 | CHECK(block_size_ != 0); |
| 104 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 105 | if (page_list_ == nullptr) { |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 106 | alloc_page(); |
| 107 | } |
| 108 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 109 | // Fully allocated pages are de-managed and removed from the page list, so |
| 110 | // every page from the page list must be useable. Let's just take the first |
| 111 | // one. |
| 112 | small_object_page_info* page = page_list_; |
| 113 | CHECK(page->free_block_list != nullptr); |
| 114 | |
| 115 | small_object_block_record* const block_record = page->free_block_list; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 116 | if (block_record->free_blocks_cnt > 1) { |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 117 | small_object_block_record* next_free = |
| 118 | reinterpret_cast<small_object_block_record*>( |
| 119 | reinterpret_cast<uint8_t*>(block_record) + block_size_); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 120 | next_free->next = block_record->next; |
| 121 | next_free->free_blocks_cnt = block_record->free_blocks_cnt - 1; |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 122 | page->free_block_list = next_free; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 123 | } else { |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 124 | page->free_block_list = block_record->next; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 127 | if (page->free_blocks_cnt == blocks_per_page_) { |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 128 | free_pages_cnt_--; |
| 129 | } |
| 130 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 131 | page->free_blocks_cnt--; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 132 | |
| 133 | memset(block_record, 0, block_size_); |
| 134 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 135 | if (page->free_blocks_cnt == 0) { |
| 136 | // De-manage fully allocated pages. These pages will be managed again if |
| 137 | // a block is freed. |
| 138 | remove_from_page_list(page); |
| 139 | } |
| 140 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 141 | return block_record; |
| 142 | } |
| 143 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 144 | void LinkerSmallObjectAllocator::free_page(small_object_page_info* page) { |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 145 | CHECK(page->free_blocks_cnt == blocks_per_page_); |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 146 | if (page->prev_page) { |
| 147 | page->prev_page->next_page = page->next_page; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 148 | } |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 149 | if (page->next_page) { |
| 150 | page->next_page->prev_page = page->prev_page; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 151 | } |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 152 | if (page_list_ == page) { |
| 153 | page_list_ = page->next_page; |
| 154 | } |
| 155 | munmap(page, PAGE_SIZE); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 156 | free_pages_cnt_--; |
| 157 | } |
| 158 | |
| 159 | void LinkerSmallObjectAllocator::free(void* ptr) { |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 160 | small_object_page_info* const page = |
| 161 | reinterpret_cast<small_object_page_info*>( |
| 162 | PAGE_START(reinterpret_cast<uintptr_t>(ptr))); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 163 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 164 | if (reinterpret_cast<uintptr_t>(ptr) % block_size_ != 0) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 165 | async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | memset(ptr, 0, block_size_); |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 169 | small_object_block_record* const block_record = |
| 170 | reinterpret_cast<small_object_block_record*>(ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 171 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 172 | block_record->next = page->free_block_list; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 173 | block_record->free_blocks_cnt = 1; |
| 174 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 175 | page->free_block_list = block_record; |
| 176 | page->free_blocks_cnt++; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 177 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 178 | if (page->free_blocks_cnt == blocks_per_page_) { |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 179 | if (++free_pages_cnt_ > 1) { |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 180 | // if we already have a free page - unmap this one. |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 181 | free_page(page); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 182 | } |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 183 | } else if (page->free_blocks_cnt == 1) { |
| 184 | // We just freed from a full page. Add this page back to the list. |
| 185 | add_to_page_list(page); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 189 | void LinkerSmallObjectAllocator::alloc_page() { |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 190 | void* const map_ptr = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, |
| 191 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 192 | if (map_ptr == MAP_FAILED) { |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 193 | async_safe_fatal("mmap failed: %s", strerror(errno)); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 196 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE, |
| 197 | "linker_alloc_small_objects"); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 198 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 199 | small_object_page_info* const page = |
| 200 | reinterpret_cast<small_object_page_info*>(map_ptr); |
| 201 | memcpy(page->info.signature, kSignature, sizeof(kSignature)); |
| 202 | page->info.type = type_; |
| 203 | page->info.allocator_addr = this; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 204 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 205 | page->free_blocks_cnt = blocks_per_page_; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 206 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 207 | // Align the first block to block_size_. |
| 208 | const uintptr_t first_block_addr = |
| 209 | __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(page + 1), block_size_); |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 210 | small_object_block_record* const first_block = |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 211 | reinterpret_cast<small_object_block_record*>(first_block_addr); |
| 212 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 213 | first_block->next = nullptr; |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 214 | first_block->free_blocks_cnt = blocks_per_page_; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 215 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 216 | page->free_block_list = first_block; |
| 217 | |
| 218 | add_to_page_list(page); |
Vic Yang | de69660 | 2018-11-27 13:34:44 -0800 | [diff] [blame] | 219 | |
| 220 | free_pages_cnt_++; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 223 | void LinkerSmallObjectAllocator::add_to_page_list(small_object_page_info* page) { |
| 224 | page->next_page = page_list_; |
| 225 | page->prev_page = nullptr; |
| 226 | if (page_list_) { |
| 227 | page_list_->prev_page = page; |
| 228 | } |
| 229 | page_list_ = page; |
| 230 | } |
| 231 | |
| 232 | void LinkerSmallObjectAllocator::remove_from_page_list( |
| 233 | small_object_page_info* page) { |
| 234 | if (page->prev_page) { |
| 235 | page->prev_page->next_page = page->next_page; |
| 236 | } |
| 237 | if (page->next_page) { |
| 238 | page->next_page->prev_page = page->prev_page; |
| 239 | } |
| 240 | if (page_list_ == page) { |
| 241 | page_list_ = page->next_page; |
| 242 | } |
| 243 | page->prev_page = nullptr; |
| 244 | page->next_page = nullptr; |
| 245 | } |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 246 | |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 247 | void LinkerMemoryAllocator::initialize_allocators() { |
| 248 | if (allocators_ != nullptr) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | LinkerSmallObjectAllocator* allocators = |
| 253 | reinterpret_cast<LinkerSmallObjectAllocator*>(allocators_buf_); |
| 254 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 255 | for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) { |
| 256 | uint32_t type = i + kSmallObjectMinSizeLog2; |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 257 | new (allocators + i) LinkerSmallObjectAllocator(type, 1 << type); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 258 | } |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 259 | |
| 260 | allocators_ = allocators; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void* LinkerMemoryAllocator::alloc_mmap(size_t size) { |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 264 | size_t allocated_size = PAGE_END(size + kPageInfoSize); |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 265 | void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, |
| 266 | -1, 0); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 267 | |
| 268 | if (map_ptr == MAP_FAILED) { |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 269 | async_safe_fatal("mmap failed: %s", strerror(errno)); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "linker_alloc_lob"); |
| 273 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 274 | page_info* info = reinterpret_cast<page_info*>(map_ptr); |
| 275 | memcpy(info->signature, kSignature, sizeof(kSignature)); |
| 276 | info->type = kLargeObject; |
| 277 | info->allocated_size = allocated_size; |
| 278 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 279 | return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(info) + |
| 280 | kPageInfoSize); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void* LinkerMemoryAllocator::alloc(size_t size) { |
| 284 | // treat alloc(0) as alloc(1) |
| 285 | if (size == 0) { |
| 286 | size = 1; |
| 287 | } |
| 288 | |
| 289 | if (size > kSmallObjectMaxSize) { |
| 290 | return alloc_mmap(size); |
| 291 | } |
| 292 | |
| 293 | uint16_t log2_size = log2(size); |
| 294 | |
| 295 | if (log2_size < kSmallObjectMinSizeLog2) { |
| 296 | log2_size = kSmallObjectMinSizeLog2; |
| 297 | } |
| 298 | |
| 299 | return get_small_object_allocator(log2_size)->alloc(); |
| 300 | } |
| 301 | |
| 302 | page_info* LinkerMemoryAllocator::get_page_info(void* ptr) { |
| 303 | page_info* info = reinterpret_cast<page_info*>(PAGE_START(reinterpret_cast<size_t>(ptr))); |
| 304 | if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 305 | async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return info; |
| 309 | } |
| 310 | |
| 311 | void* LinkerMemoryAllocator::realloc(void* ptr, size_t size) { |
| 312 | if (ptr == nullptr) { |
| 313 | return alloc(size); |
| 314 | } |
| 315 | |
| 316 | if (size == 0) { |
| 317 | free(ptr); |
| 318 | return nullptr; |
| 319 | } |
| 320 | |
| 321 | page_info* info = get_page_info(ptr); |
| 322 | |
| 323 | size_t old_size = 0; |
| 324 | |
| 325 | if (info->type == kLargeObject) { |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 326 | old_size = info->allocated_size - kPageInfoSize; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 327 | } else { |
| 328 | LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type); |
| 329 | if (allocator != info->allocator_addr) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 330 | async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | old_size = allocator->get_block_size(); |
| 334 | } |
| 335 | |
| 336 | if (old_size < size) { |
| 337 | void *result = alloc(size); |
| 338 | memcpy(result, ptr, old_size); |
| 339 | free(ptr); |
| 340 | return result; |
| 341 | } |
| 342 | |
| 343 | return ptr; |
| 344 | } |
| 345 | |
| 346 | void LinkerMemoryAllocator::free(void* ptr) { |
| 347 | if (ptr == nullptr) { |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | page_info* info = get_page_info(ptr); |
| 352 | |
| 353 | if (info->type == kLargeObject) { |
| 354 | munmap(info, info->allocated_size); |
| 355 | } else { |
| 356 | LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type); |
| 357 | if (allocator != info->allocator_addr) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 358 | async_safe_fatal("invalid pointer %p (invalid allocator address for the page)", ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | allocator->free(ptr); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | LinkerSmallObjectAllocator* LinkerMemoryAllocator::get_small_object_allocator(uint32_t type) { |
| 366 | if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 367 | async_safe_fatal("invalid type: %u", type); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 370 | initialize_allocators(); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 371 | return &allocators_[type - kSmallObjectMinSizeLog2]; |
| 372 | } |