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 | |
Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 30 | |
Dan Albert | 1c78cb0 | 2017-10-11 11:25:25 -0700 | [diff] [blame] | 31 | #include <errno.h> |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <sys/cdefs.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 <stddef.h> |
| 37 | #include <unistd.h> |
| 38 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 39 | #include <async_safe/log.h> |
| 40 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 41 | const uint32_t kSmallObjectMaxSizeLog2 = 10; |
| 42 | const uint32_t kSmallObjectMinSizeLog2 = 4; |
| 43 | const uint32_t kSmallObjectAllocatorsCount = kSmallObjectMaxSizeLog2 - kSmallObjectMinSizeLog2 + 1; |
| 44 | |
| 45 | class LinkerSmallObjectAllocator; |
| 46 | |
| 47 | // This structure is placed at the beginning of each addressable page |
| 48 | // and has all information we need to find the corresponding memory allocator. |
| 49 | struct page_info { |
| 50 | char signature[4]; |
| 51 | uint32_t type; |
| 52 | union { |
| 53 | // we use allocated_size for large objects allocator |
| 54 | size_t allocated_size; |
| 55 | // and allocator_addr for small ones. |
| 56 | LinkerSmallObjectAllocator* allocator_addr; |
| 57 | }; |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 58 | }; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 59 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 60 | struct small_object_block_record { |
| 61 | small_object_block_record* next; |
| 62 | size_t free_blocks_cnt; |
| 63 | }; |
| 64 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 65 | // This structure is placed at the beginning of each page managed by |
| 66 | // LinkerSmallObjectAllocator. Note that a page_info struct is expected at the |
| 67 | // beginning of each page as well, and therefore this structure contains a |
| 68 | // page_info as its *first* field. |
| 69 | struct small_object_page_info { |
| 70 | page_info info; // Must be the first field. |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 71 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 72 | // Doubly linked list for traversing all pages allocated by a |
| 73 | // LinkerSmallObjectAllocator. |
| 74 | small_object_page_info* next_page; |
| 75 | small_object_page_info* prev_page; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 76 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 77 | // Linked list containing all free blocks in this page. |
| 78 | small_object_block_record* free_block_list; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 79 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 80 | // Free blocks counter. |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 81 | size_t free_blocks_cnt; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 84 | class LinkerSmallObjectAllocator { |
| 85 | public: |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 86 | LinkerSmallObjectAllocator(uint32_t type, size_t block_size); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 87 | void* alloc(); |
| 88 | void free(void* ptr); |
| 89 | |
| 90 | size_t get_block_size() const { return block_size_; } |
| 91 | private: |
| 92 | void alloc_page(); |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 93 | void free_page(small_object_page_info* page); |
| 94 | void add_to_page_list(small_object_page_info* page); |
| 95 | void remove_from_page_list(small_object_page_info* page); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 96 | |
Vic Yang | 259429b | 2018-12-04 23:59:57 -0800 | [diff] [blame^] | 97 | const uint32_t type_; |
| 98 | const size_t block_size_; |
| 99 | const size_t blocks_per_page_; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 100 | |
| 101 | size_t free_pages_cnt_; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 102 | |
Vic Yang | 5493851 | 2018-12-02 23:46:26 -0800 | [diff] [blame] | 103 | small_object_page_info* page_list_; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | class LinkerMemoryAllocator { |
| 107 | public: |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 108 | constexpr LinkerMemoryAllocator() : allocators_(nullptr), allocators_buf_() {} |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 109 | void* alloc(size_t size); |
| 110 | |
| 111 | // Note that this implementation of realloc never shrinks allocation |
| 112 | void* realloc(void* ptr, size_t size); |
| 113 | void free(void* ptr); |
| 114 | private: |
| 115 | void* alloc_mmap(size_t size); |
| 116 | page_info* get_page_info(void* ptr); |
| 117 | LinkerSmallObjectAllocator* get_small_object_allocator(uint32_t type); |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 118 | void initialize_allocators(); |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 119 | |
Dimitry Ivanov | 65707b6 | 2016-07-29 13:25:33 -0700 | [diff] [blame] | 120 | LinkerSmallObjectAllocator* allocators_; |
| 121 | uint8_t allocators_buf_[sizeof(LinkerSmallObjectAllocator)*kSmallObjectAllocatorsCount]; |
Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 122 | }; |