2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __LINKER_BLOCK_ALLOCATOR_H
18 #define __LINKER_BLOCK_ALLOCATOR_H
22 #include "private/bionic_macros.h"
24 struct LinkerBlockAllocatorPage;
27 * This class is a non-template version of the LinkerTypeAllocator
28 * It keeps code inside .cpp file by keeping the interface
31 * Please use LinkerTypeAllocator<type> where possible (everywhere).
33 class LinkerBlockAllocator {
35 explicit LinkerBlockAllocator(size_t block_size);
38 void free(void* block);
39 void protect_all(int prot);
42 void create_new_page();
43 LinkerBlockAllocatorPage* find_page(void* block);
46 LinkerBlockAllocatorPage* page_list_;
47 void* free_block_list_;
49 DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator);
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
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.
64 * 2. This allocator does not munmap allocated memory, where LinkerMemoryAllocator does.
66 * 3. This allocator provides mprotect services to the user, where LinkerMemoryAllocator
67 * always treats it's memory as READ|WRITE.
70 class LinkerTypeAllocator {
72 LinkerTypeAllocator() : block_allocator_(sizeof(T)) {}
73 T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); }
74 void free(T* t) { block_allocator_.free(t); }
75 void protect_all(int prot) { block_allocator_.protect_all(prot); }
77 LinkerBlockAllocator block_allocator_;
78 DISALLOW_COPY_AND_ASSIGN(LinkerTypeAllocator);
81 #endif // __LINKER_BLOCK_ALLOCATOR_H