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 | |
| 17 | #ifndef __LINKER_ALLOCATOR_H |
| 18 | #define __LINKER_ALLOCATOR_H |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <limits.h> |
| 22 | #include "private/bionic_macros.h" |
| 23 | |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 24 | struct LinkerBlockAllocatorPage; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * This class is a non-template version of the LinkerAllocator |
| 28 | * It keeps code inside .cpp file by keeping the interface |
| 29 | * template-free. |
| 30 | * |
| 31 | * Please use LinkerAllocator<type> where possible (everywhere). |
| 32 | */ |
| 33 | class LinkerBlockAllocator { |
| 34 | public: |
Dmitriy Ivanov | 4151ea7 | 2014-07-24 15:33:25 -0700 | [diff] [blame] | 35 | explicit LinkerBlockAllocator(size_t block_size); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 36 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 37 | void* alloc(); |
| 38 | void free(void* block); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 39 | void protect_all(int prot); |
| 40 | |
| 41 | private: |
| 42 | void create_new_page(); |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 43 | LinkerBlockAllocatorPage* find_page(void* block); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 44 | |
| 45 | size_t block_size_; |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 46 | LinkerBlockAllocatorPage* page_list_; |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 47 | void* free_block_list_; |
| 48 | |
| 49 | DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator); |
| 50 | }; |
| 51 | |
| 52 | /* |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 53 | * We can't use malloc(3) in the dynamic linker. |
| 54 | * |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 55 | * A simple allocator for the dynamic linker. An allocator allocates instances |
| 56 | * of a single fixed-size type. Allocations are backed by page-sized private |
| 57 | * anonymous mmaps. |
| 58 | */ |
| 59 | template<typename T> |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 60 | class LinkerTypeAllocator { |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 61 | public: |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 62 | LinkerTypeAllocator() : block_allocator_(sizeof(T)) {} |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 63 | T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); } |
| 64 | void free(T* t) { block_allocator_.free(t); } |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 65 | void protect_all(int prot) { block_allocator_.protect_all(prot); } |
| 66 | private: |
| 67 | LinkerBlockAllocator block_allocator_; |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 68 | DISALLOW_COPY_AND_ASSIGN(LinkerTypeAllocator); |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 69 | }; |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 70 | |
Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 71 | #endif // __LINKER_ALLOCATOR_H |