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 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 29 | #include "private/bionic_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> |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 34 | #include <sys/param.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 | |
Ryan Prichard | 52165b3 | 2019-01-23 17:46:24 -0800 | [diff] [blame] | 38 | #include <new> |
| 39 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 40 | #include <async_safe/log.h> |
Elliott Hughes | 3019d78 | 2019-02-13 12:39:07 -0800 | [diff] [blame] | 41 | #include <async_safe/CHECK.h> |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | cdb52fc | 2019-12-12 15:26:14 -0800 | [diff] [blame] | 43 | #include "platform/bionic/page.h" |
Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 44 | #include "platform/bionic/macros.h" |
Ryan Prichard | 52165b3 | 2019-01-23 17:46:24 -0800 | [diff] [blame] | 45 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 46 | // |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 47 | // BionicAllocator is a general purpose allocator designed to provide the same |
Ryan Prichard | db6edcc | 2019-04-01 16:16:05 -0700 | [diff] [blame] | 48 | // functionality as the malloc/free/realloc/memalign libc functions. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 49 | // |
| 50 | // On alloc: |
Ryan Prichard | db6edcc | 2019-04-01 16:16:05 -0700 | [diff] [blame] | 51 | // If size is > 1k allocator proxies malloc call directly to mmap. |
| 52 | // If size <= 1k allocator uses BionicSmallObjectAllocator for the size |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 53 | // rounded up to the nearest power of two. |
| 54 | // |
| 55 | // On free: |
| 56 | // |
| 57 | // For a pointer allocated using proxy-to-mmap allocator unmaps |
| 58 | // the memory. |
| 59 | // |
Ryan Prichard | db6edcc | 2019-04-01 16:16:05 -0700 | [diff] [blame] | 60 | // For a pointer allocated using BionicSmallObjectAllocator it adds |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 61 | // the block to free_blocks_list in the corresponding page. If the number of |
Ryan Prichard | db6edcc | 2019-04-01 16:16:05 -0700 | [diff] [blame] | 62 | // free pages reaches 2, BionicSmallObjectAllocator munmaps one of the pages |
| 63 | // keeping the other one in reserve. |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 64 | |
| 65 | // Memory management for large objects is fairly straightforward, but for small |
| 66 | // objects it is more complicated. If you are changing this code, one simple |
| 67 | // way to evaluate the memory usage change is by running 'dd' and examine the |
| 68 | // memory usage by 'showmap $(pidof dd)'. 'dd' is nice in that: |
| 69 | // 1. It links in quite a few libraries, so you get some linker memory use. |
| 70 | // 2. When run with no arguments, it sits waiting for input, so it is easy to |
| 71 | // examine its memory usage with showmap. |
| 72 | // 3. Since it does nothing while waiting for input, the memory usage is |
| 73 | // determinisitic. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 74 | |
| 75 | static const char kSignature[4] = {'L', 'M', 'A', 1}; |
| 76 | |
| 77 | static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2; |
| 78 | |
| 79 | // This type is used for large allocations (with size >1k) |
| 80 | static const uint32_t kLargeObject = 111; |
| 81 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 82 | // Allocated pointers must be at least 16-byte aligned. Round up the size of |
| 83 | // page_info to multiple of 16. |
| 84 | static constexpr size_t kPageInfoSize = __BIONIC_ALIGN(sizeof(page_info), 16); |
| 85 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 86 | static inline uint16_t log2(size_t number) { |
| 87 | uint16_t result = 0; |
| 88 | number--; |
| 89 | |
| 90 | while (number != 0) { |
| 91 | result++; |
| 92 | number >>= 1; |
| 93 | } |
| 94 | |
| 95 | return result; |
| 96 | } |
| 97 | |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 98 | BionicSmallObjectAllocator::BionicSmallObjectAllocator(uint32_t type, size_t block_size) |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 99 | : type_(type), |
| 100 | block_size_(block_size), |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 101 | blocks_per_page_((page_size() - sizeof(small_object_page_info)) / 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 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 105 | void* BionicSmallObjectAllocator::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 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 147 | void BionicSmallObjectAllocator::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 | } |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 158 | munmap(page, page_size()); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 159 | free_pages_cnt_--; |
| 160 | } |
| 161 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 162 | void BionicSmallObjectAllocator::free(void* ptr) { |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 163 | small_object_page_info* const page = |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 164 | reinterpret_cast<small_object_page_info*>(page_start(reinterpret_cast<uintptr_t>(ptr))); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 165 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 166 | if (reinterpret_cast<uintptr_t>(ptr) % block_size_ != 0) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 167 | async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | memset(ptr, 0, block_size_); |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 171 | small_object_block_record* const block_record = |
| 172 | reinterpret_cast<small_object_block_record*>(ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 173 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 174 | block_record->next = page->free_block_list; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 175 | block_record->free_blocks_cnt = 1; |
| 176 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 177 | page->free_block_list = block_record; |
| 178 | page->free_blocks_cnt++; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 179 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 180 | if (page->free_blocks_cnt == blocks_per_page_) { |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 181 | if (++free_pages_cnt_ > 1) { |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 182 | // if we already have a free page - unmap this one. |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 183 | free_page(page); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 184 | } |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 185 | } else if (page->free_blocks_cnt == 1) { |
| 186 | // We just freed from a full page. Add this page back to the list. |
| 187 | add_to_page_list(page); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 191 | void BionicSmallObjectAllocator::alloc_page() { |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 192 | void* const map_ptr = |
| 193 | mmap(nullptr, page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 194 | if (map_ptr == MAP_FAILED) { |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 195 | async_safe_fatal("mmap failed: %s", strerror(errno)); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 198 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, page_size(), "bionic_alloc_small_objects"); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 199 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 200 | small_object_page_info* const page = |
| 201 | reinterpret_cast<small_object_page_info*>(map_ptr); |
| 202 | memcpy(page->info.signature, kSignature, sizeof(kSignature)); |
| 203 | page->info.type = type_; |
| 204 | page->info.allocator_addr = this; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 205 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 206 | page->free_blocks_cnt = blocks_per_page_; |
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 | // Align the first block to block_size_. |
| 209 | const uintptr_t first_block_addr = |
| 210 | __BIONIC_ALIGN(reinterpret_cast<uintptr_t>(page + 1), block_size_); |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 211 | small_object_block_record* const first_block = |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 212 | reinterpret_cast<small_object_block_record*>(first_block_addr); |
| 213 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 214 | first_block->next = nullptr; |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame] | 215 | first_block->free_blocks_cnt = blocks_per_page_; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 216 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 217 | page->free_block_list = first_block; |
| 218 | |
| 219 | add_to_page_list(page); |
Vic Yang | de69660 | 2018-11-27 13:34:44 -0800 | [diff] [blame] | 220 | |
| 221 | free_pages_cnt_++; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 224 | void BionicSmallObjectAllocator::add_to_page_list(small_object_page_info* page) { |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 225 | page->next_page = page_list_; |
| 226 | page->prev_page = nullptr; |
| 227 | if (page_list_) { |
| 228 | page_list_->prev_page = page; |
| 229 | } |
| 230 | page_list_ = page; |
| 231 | } |
| 232 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 233 | void BionicSmallObjectAllocator::remove_from_page_list( |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 234 | small_object_page_info* page) { |
| 235 | if (page->prev_page) { |
| 236 | page->prev_page->next_page = page->next_page; |
| 237 | } |
| 238 | if (page->next_page) { |
| 239 | page->next_page->prev_page = page->prev_page; |
| 240 | } |
| 241 | if (page_list_ == page) { |
| 242 | page_list_ = page->next_page; |
| 243 | } |
| 244 | page->prev_page = nullptr; |
| 245 | page->next_page = nullptr; |
| 246 | } |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 247 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 248 | void BionicAllocator::initialize_allocators() { |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 249 | if (allocators_ != nullptr) { |
| 250 | return; |
| 251 | } |
| 252 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 253 | BionicSmallObjectAllocator* allocators = |
| 254 | reinterpret_cast<BionicSmallObjectAllocator*>(allocators_buf_); |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 255 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 256 | for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) { |
| 257 | uint32_t type = i + kSmallObjectMinSizeLog2; |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 258 | new (allocators + i) BionicSmallObjectAllocator(type, 1 << type); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 259 | } |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 260 | |
| 261 | allocators_ = allocators; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 264 | void* BionicAllocator::alloc_mmap(size_t align, size_t size) { |
| 265 | size_t header_size = __BIONIC_ALIGN(kPageInfoSize, align); |
| 266 | size_t allocated_size; |
| 267 | if (__builtin_add_overflow(header_size, size, &allocated_size) || |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 268 | page_end(allocated_size) < allocated_size) { |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 269 | async_safe_fatal("overflow trying to alloc %zu bytes", size); |
| 270 | } |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 271 | allocated_size = page_end(allocated_size); |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 272 | void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, |
| 273 | -1, 0); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 274 | |
| 275 | if (map_ptr == MAP_FAILED) { |
Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 276 | async_safe_fatal("mmap failed: %s", strerror(errno)); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 279 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "bionic_alloc_lob"); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 280 | |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 281 | void* result = static_cast<char*>(map_ptr) + header_size; |
| 282 | page_info* info = get_page_info_unchecked(result); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 283 | memcpy(info->signature, kSignature, sizeof(kSignature)); |
| 284 | info->type = kLargeObject; |
| 285 | info->allocated_size = allocated_size; |
| 286 | |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 287 | return result; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 290 | |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 291 | inline void* BionicAllocator::alloc_impl(size_t align, size_t size) { |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 292 | if (size > kSmallObjectMaxSize) { |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 293 | return alloc_mmap(align, size); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 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 | |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 305 | void* BionicAllocator::alloc(size_t size) { |
| 306 | // treat alloc(0) as alloc(1) |
| 307 | if (size == 0) { |
| 308 | size = 1; |
| 309 | } |
| 310 | return alloc_impl(16, size); |
| 311 | } |
| 312 | |
| 313 | void* BionicAllocator::memalign(size_t align, size_t size) { |
| 314 | // The Bionic allocator only supports alignment up to one page, which is good |
| 315 | // enough for ELF TLS. |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 316 | align = MIN(align, page_size()); |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 317 | align = MAX(align, 16); |
| 318 | if (!powerof2(align)) { |
| 319 | align = BIONIC_ROUND_UP_POWER_OF_2(align); |
| 320 | } |
| 321 | size = MAX(size, align); |
| 322 | return alloc_impl(align, size); |
| 323 | } |
| 324 | |
| 325 | inline page_info* BionicAllocator::get_page_info_unchecked(void* ptr) { |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 326 | uintptr_t header_page = page_start(reinterpret_cast<size_t>(ptr) - kPageInfoSize); |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 327 | return reinterpret_cast<page_info*>(header_page); |
| 328 | } |
| 329 | |
| 330 | inline page_info* BionicAllocator::get_page_info(void* ptr) { |
| 331 | page_info* info = get_page_info_unchecked(ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 332 | if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) { |
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 | return info; |
| 337 | } |
| 338 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 339 | void* BionicAllocator::realloc(void* ptr, size_t size) { |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 340 | if (ptr == nullptr) { |
| 341 | return alloc(size); |
| 342 | } |
| 343 | |
| 344 | if (size == 0) { |
| 345 | free(ptr); |
| 346 | return nullptr; |
| 347 | } |
| 348 | |
| 349 | page_info* info = get_page_info(ptr); |
| 350 | |
| 351 | size_t old_size = 0; |
| 352 | |
| 353 | if (info->type == kLargeObject) { |
Ryan Prichard | 96773a2 | 2019-01-24 15:22:50 -0800 | [diff] [blame] | 354 | old_size = info->allocated_size - (static_cast<char*>(ptr) - reinterpret_cast<char*>(info)); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 355 | } else { |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 356 | BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 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 (page signature mismatch)", ptr); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | old_size = allocator->get_block_size(); |
| 362 | } |
| 363 | |
| 364 | if (old_size < size) { |
| 365 | void *result = alloc(size); |
| 366 | memcpy(result, ptr, old_size); |
| 367 | free(ptr); |
| 368 | return result; |
| 369 | } |
| 370 | |
| 371 | return ptr; |
| 372 | } |
| 373 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 374 | void BionicAllocator::free(void* ptr) { |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 375 | if (ptr == nullptr) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | page_info* info = get_page_info(ptr); |
| 380 | |
| 381 | if (info->type == kLargeObject) { |
| 382 | munmap(info, info->allocated_size); |
| 383 | } else { |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 384 | BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 385 | if (allocator != info->allocator_addr) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 386 | 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] | 387 | } |
| 388 | |
| 389 | allocator->free(ptr); |
| 390 | } |
| 391 | } |
| 392 | |
Vy Nguyen | d500751 | 2020-07-14 17:37:04 -0400 | [diff] [blame] | 393 | size_t BionicAllocator::get_chunk_size(void* ptr) { |
| 394 | if (ptr == nullptr) return 0; |
| 395 | |
| 396 | page_info* info = get_page_info_unchecked(ptr); |
| 397 | if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) { |
| 398 | // Invalid pointer (mismatched signature) |
| 399 | return 0; |
| 400 | } |
| 401 | if (info->type == kLargeObject) { |
| 402 | return info->allocated_size - (static_cast<char*>(ptr) - reinterpret_cast<char*>(info)); |
| 403 | } |
| 404 | |
| 405 | BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type); |
| 406 | if (allocator != info->allocator_addr) { |
| 407 | // Invalid pointer. |
| 408 | return 0; |
| 409 | } |
| 410 | return allocator->get_block_size(); |
| 411 | } |
| 412 | |
Ryan Prichard | 083d850 | 2019-01-24 13:47:13 -0800 | [diff] [blame] | 413 | BionicSmallObjectAllocator* BionicAllocator::get_small_object_allocator(uint32_t type) { |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 414 | if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) { |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 415 | async_safe_fatal("invalid type: %u", type); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 418 | initialize_allocators(); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 419 | return &allocators_[type - kSmallObjectMinSizeLog2]; |
| 420 | } |