| 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 |  | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 17 | #ifndef __LINKER_BLOCK_ALLOCATOR_H | 
|  | 18 | #define __LINKER_BLOCK_ALLOCATOR_H | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 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 | /* | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 27 | * This class is a non-template version of the LinkerTypeAllocator | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 28 | * It keeps code inside .cpp file by keeping the interface | 
|  | 29 | * template-free. | 
|  | 30 | * | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 31 | * Please use LinkerTypeAllocator<type> where possible (everywhere). | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 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 | /* | 
|  | 53 | * A simple allocator for the dynamic linker. An allocator allocates instances | 
|  | 54 | * of a single fixed-size type. Allocations are backed by page-sized private | 
|  | 55 | * anonymous mmaps. | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 56 | * | 
|  | 57 | * The differences between this allocator and LinkerMemoryAllocator are: | 
|  | 58 | * 1. This allocator manages space more efficiently. LinkerMemoryAllocator | 
|  | 59 | *    operates in power-of-two sized blocks up to 1k, when this implementation | 
|  | 60 | *    splits the page to aligned size of structure; For example for structures | 
|  | 61 | *    with size 513 this allocator will use 516 (520 for lp64) bytes of data | 
|  | 62 | *    where generalized implementation is going to use 1024 sized blocks. | 
|  | 63 | * | 
|  | 64 | * 2. This allocator does not munmap allocated memory, where LinkerMemoryAllocator does. | 
|  | 65 | * | 
|  | 66 | * 3. This allocator provides mprotect services to the user, where LinkerMemoryAllocator | 
|  | 67 | *    always treats it's memory as READ|WRITE. | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 68 | */ | 
|  | 69 | template<typename T> | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 70 | class LinkerTypeAllocator { | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 71 | public: | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 72 | LinkerTypeAllocator() : block_allocator_(sizeof(T)) {} | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 73 | T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); } | 
|  | 74 | void free(T* t) { block_allocator_.free(t); } | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 75 | void protect_all(int prot) { block_allocator_.protect_all(prot); } | 
|  | 76 | private: | 
|  | 77 | LinkerBlockAllocator block_allocator_; | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 78 | DISALLOW_COPY_AND_ASSIGN(LinkerTypeAllocator); | 
| Dmitriy Ivanov | d597d26 | 2014-05-05 16:49:04 -0700 | [diff] [blame] | 79 | }; | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 80 |  | 
| Dmitriy Ivanov | 19656ce | 2015-03-10 17:48:27 -0700 | [diff] [blame] | 81 | #endif // __LINKER_BLOCK_ALLOCATOR_H |