| 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 | #ifndef __LINKER_ALLOCATOR_H | 
|  | 30 | #define __LINKER_ALLOCATOR_H | 
|  | 31 |  | 
| Dan Albert | 1c78cb0 | 2017-10-11 11:25:25 -0700 | [diff] [blame] | 32 | #include <errno.h> | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 33 | #include <stdlib.h> | 
|  | 34 | #include <sys/cdefs.h> | 
|  | 35 | #include <sys/mman.h> | 
|  | 36 | #include <stddef.h> | 
|  | 37 | #include <unistd.h> | 
|  | 38 |  | 
|  | 39 | #include <vector> | 
|  | 40 |  | 
| Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 41 | #include <async_safe/log.h> | 
|  | 42 |  | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 43 | #include "private/bionic_prctl.h" | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | const uint32_t kSmallObjectMaxSizeLog2 = 10; | 
|  | 46 | const uint32_t kSmallObjectMinSizeLog2 = 4; | 
|  | 47 | const uint32_t kSmallObjectAllocatorsCount = kSmallObjectMaxSizeLog2 - kSmallObjectMinSizeLog2 + 1; | 
|  | 48 |  | 
|  | 49 | class LinkerSmallObjectAllocator; | 
|  | 50 |  | 
|  | 51 | // This structure is placed at the beginning of each addressable page | 
|  | 52 | // and has all information we need to find the corresponding memory allocator. | 
|  | 53 | struct page_info { | 
|  | 54 | char signature[4]; | 
|  | 55 | uint32_t type; | 
|  | 56 | union { | 
|  | 57 | // we use allocated_size for large objects allocator | 
|  | 58 | size_t allocated_size; | 
|  | 59 | // and allocator_addr for small ones. | 
|  | 60 | LinkerSmallObjectAllocator* allocator_addr; | 
|  | 61 | }; | 
| Dimitry Ivanov | 3edc5c4 | 2016-01-21 10:55:40 -0800 | [diff] [blame] | 62 | } __attribute__((aligned(16))); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 63 |  | 
|  | 64 | struct small_object_page_record { | 
|  | 65 | void* page_addr; | 
|  | 66 | size_t free_blocks_cnt; | 
|  | 67 | size_t allocated_blocks_cnt; | 
|  | 68 | }; | 
|  | 69 |  | 
|  | 70 | // for lower_bound... | 
|  | 71 | bool operator<(const small_object_page_record& one, const small_object_page_record& two); | 
|  | 72 |  | 
|  | 73 | struct small_object_block_record { | 
|  | 74 | small_object_block_record* next; | 
|  | 75 | size_t free_blocks_cnt; | 
|  | 76 | }; | 
|  | 77 |  | 
|  | 78 | // This is implementation for std::vector allocator | 
|  | 79 | template <typename T> | 
|  | 80 | class linker_vector_allocator { | 
|  | 81 | public: | 
|  | 82 | typedef T value_type; | 
|  | 83 | typedef T* pointer; | 
|  | 84 | typedef const T* const_pointer; | 
|  | 85 | typedef T& reference; | 
|  | 86 | typedef const T& const_reference; | 
|  | 87 | typedef size_t size_type; | 
|  | 88 | typedef ptrdiff_t difference_type; | 
|  | 89 |  | 
|  | 90 | T* allocate(size_t n, const T* hint = nullptr) { | 
|  | 91 | size_t size = n * sizeof(T); | 
| Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 92 | void* ptr = mmap(const_cast<T*>(hint), size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, | 
|  | 93 | -1, 0); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 94 | if (ptr == MAP_FAILED) { | 
|  | 95 | // Spec says we need to throw std::bad_alloc here but because our | 
|  | 96 | // code does not support exception handling anyways - we are going to abort. | 
| Elliott Hughes | 7b0af7a | 2017-09-15 16:09:22 -0700 | [diff] [blame] | 97 | async_safe_fatal("mmap failed: %s", strerror(errno)); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
|  | 100 | prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ptr, size, "linker_alloc_vector"); | 
|  | 101 |  | 
|  | 102 | return reinterpret_cast<T*>(ptr); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | void deallocate(T* ptr, size_t n) { | 
|  | 106 | munmap(ptr, n * sizeof(T)); | 
|  | 107 | } | 
|  | 108 | }; | 
|  | 109 |  | 
|  | 110 | typedef | 
|  | 111 | std::vector<small_object_page_record, linker_vector_allocator<small_object_page_record>> | 
|  | 112 | linker_vector_t; | 
|  | 113 |  | 
|  | 114 |  | 
|  | 115 | class LinkerSmallObjectAllocator { | 
|  | 116 | public: | 
| Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 117 | LinkerSmallObjectAllocator(uint32_t type, size_t block_size); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 118 | void* alloc(); | 
|  | 119 | void free(void* ptr); | 
|  | 120 |  | 
|  | 121 | size_t get_block_size() const { return block_size_; } | 
|  | 122 | private: | 
|  | 123 | void alloc_page(); | 
|  | 124 | void free_page(linker_vector_t::iterator page_record); | 
|  | 125 | linker_vector_t::iterator find_page_record(void* ptr); | 
|  | 126 | void create_page_record(void* page_addr, size_t free_blocks_cnt); | 
|  | 127 |  | 
|  | 128 | uint32_t type_; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 129 | size_t block_size_; | 
|  | 130 |  | 
|  | 131 | size_t free_pages_cnt_; | 
|  | 132 | small_object_block_record* free_blocks_list_; | 
|  | 133 |  | 
|  | 134 | // sorted vector of page records | 
|  | 135 | linker_vector_t page_records_; | 
|  | 136 | }; | 
|  | 137 |  | 
|  | 138 | class LinkerMemoryAllocator { | 
|  | 139 | public: | 
| Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 140 | constexpr LinkerMemoryAllocator() : allocators_(nullptr), allocators_buf_() {} | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 141 | void* alloc(size_t size); | 
|  | 142 |  | 
|  | 143 | // Note that this implementation of realloc never shrinks allocation | 
|  | 144 | void* realloc(void* ptr, size_t size); | 
|  | 145 | void free(void* ptr); | 
|  | 146 | private: | 
|  | 147 | void* alloc_mmap(size_t size); | 
|  | 148 | page_info* get_page_info(void* ptr); | 
|  | 149 | LinkerSmallObjectAllocator* get_small_object_allocator(uint32_t type); | 
| Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 150 | void initialize_allocators(); | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 151 |  | 
| Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 152 | LinkerSmallObjectAllocator* allocators_; | 
|  | 153 | uint8_t allocators_buf_[sizeof(LinkerSmallObjectAllocator)*kSmallObjectAllocatorsCount]; | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 154 | }; | 
|  | 155 |  | 
|  | 156 |  | 
|  | 157 | #endif  /* __LINKER_ALLOCATOR_H */ |