Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "linker_allocator.h" |
| 17 | #include <inttypes.h> |
| 18 | #include <sys/mman.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | struct LinkerAllocatorPage { |
| 22 | LinkerAllocatorPage* next; |
| 23 | uint8_t bytes[PAGE_SIZE-sizeof(LinkerAllocatorPage*)]; |
| 24 | }; |
| 25 | |
| 26 | struct FreeBlockInfo { |
| 27 | void* next_block; |
| 28 | size_t num_free_blocks; |
| 29 | }; |
| 30 | |
| 31 | LinkerBlockAllocator::LinkerBlockAllocator() |
| 32 | : block_size_(0), |
| 33 | page_list_(nullptr), |
| 34 | free_block_list_(nullptr) |
| 35 | {} |
| 36 | |
| 37 | void LinkerBlockAllocator::init(size_t block_size) { |
| 38 | block_size_ = block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | void* LinkerBlockAllocator::alloc() { |
| 43 | if (free_block_list_ == nullptr) { |
| 44 | create_new_page(); |
| 45 | } else { |
| 46 | protect_page(free_block_list_, PROT_READ | PROT_WRITE); |
| 47 | } |
| 48 | |
| 49 | FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_); |
| 50 | if (block_info->num_free_blocks > 1) { |
| 51 | FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>( |
| 52 | reinterpret_cast<char*>(free_block_list_) + block_size_); |
| 53 | next_block_info->next_block = block_info->next_block; |
| 54 | next_block_info->num_free_blocks = block_info->num_free_blocks - 1; |
| 55 | free_block_list_ = next_block_info; |
| 56 | } else { |
| 57 | free_block_list_ = block_info->next_block; |
| 58 | } |
| 59 | |
| 60 | block_info->next_block = nullptr; |
| 61 | block_info->num_free_blocks = 0; |
| 62 | |
| 63 | return block_info; |
| 64 | } |
| 65 | |
| 66 | void LinkerBlockAllocator::free(void* block) { |
| 67 | if (block == nullptr) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | LinkerAllocatorPage* page = find_page(block); |
| 72 | |
| 73 | if (page == nullptr) { |
| 74 | abort(); |
| 75 | } |
| 76 | |
| 77 | ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes; |
| 78 | |
| 79 | if (offset % block_size_ != 0) { |
| 80 | abort(); |
| 81 | } |
| 82 | |
| 83 | FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block); |
| 84 | |
| 85 | protect_page(block_info, PROT_READ | PROT_WRITE); |
| 86 | block_info->next_block = free_block_list_; |
| 87 | block_info->num_free_blocks = 1; |
| 88 | protect_page(block_info, PROT_READ); |
| 89 | |
| 90 | free_block_list_ = block_info; |
| 91 | } |
| 92 | |
| 93 | void LinkerBlockAllocator::protect_all(int prot) { |
| 94 | for (LinkerAllocatorPage* page = page_list_; page != nullptr; page = page->next) { |
| 95 | if (mprotect(page, PAGE_SIZE, prot) == -1) { |
| 96 | abort(); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void LinkerBlockAllocator::protect_page(void* block, int prot) { |
| 102 | LinkerAllocatorPage* page = find_page(block); |
| 103 | if (page == nullptr || mprotect(page, PAGE_SIZE, prot) == -1) { |
| 104 | abort(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void LinkerBlockAllocator::create_new_page() { |
| 110 | LinkerAllocatorPage* page = reinterpret_cast<LinkerAllocatorPage*>(mmap(nullptr, PAGE_SIZE, |
| 111 | PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0)); |
| 112 | if (page == MAP_FAILED) { |
| 113 | abort(); // oom |
| 114 | } |
| 115 | |
| 116 | FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes); |
| 117 | first_block->next_block = free_block_list_; |
| 118 | first_block->num_free_blocks = (PAGE_SIZE - sizeof(LinkerAllocatorPage*))/block_size_; |
| 119 | |
| 120 | free_block_list_ = first_block; |
| 121 | |
| 122 | page->next = page_list_; |
| 123 | page_list_ = page; |
| 124 | } |
| 125 | |
| 126 | LinkerAllocatorPage* LinkerBlockAllocator::find_page(void* block) { |
| 127 | if (block == nullptr) { |
| 128 | abort(); |
| 129 | } |
| 130 | |
| 131 | LinkerAllocatorPage* page = page_list_; |
| 132 | const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page); |
| 133 | while (page != nullptr) { |
| 134 | if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + PAGE_SIZE)) { |
| 135 | return page; |
| 136 | } |
| 137 | |
| 138 | page = page->next; |
| 139 | } |
| 140 | |
| 141 | abort(); |
| 142 | } |