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