2 * Copyright (C) 2014 The Android Open Source Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
29 #ifndef __LINKER_BLOCK_ALLOCATOR_H
30 #define __LINKER_BLOCK_ALLOCATOR_H
34 #include "private/bionic_macros.h"
36 struct LinkerBlockAllocatorPage;
39 * This class is a non-template version of the LinkerTypeAllocator
40 * It keeps code inside .cpp file by keeping the interface
43 * Please use LinkerTypeAllocator<type> where possible (everywhere).
45 class LinkerBlockAllocator {
47 explicit LinkerBlockAllocator(size_t block_size);
50 void free(void* block);
51 void protect_all(int prot);
54 void create_new_page();
55 LinkerBlockAllocatorPage* find_page(void* block);
58 LinkerBlockAllocatorPage* page_list_;
59 void* free_block_list_;
61 DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator);
65 * A simple allocator for the dynamic linker. An allocator allocates instances
66 * of a single fixed-size type. Allocations are backed by page-sized private
69 * The differences between this allocator and LinkerMemoryAllocator are:
70 * 1. This allocator manages space more efficiently. LinkerMemoryAllocator
71 * operates in power-of-two sized blocks up to 1k, when this implementation
72 * splits the page to aligned size of structure; For example for structures
73 * with size 513 this allocator will use 516 (520 for lp64) bytes of data
74 * where generalized implementation is going to use 1024 sized blocks.
76 * 2. This allocator does not munmap allocated memory, where LinkerMemoryAllocator does.
78 * 3. This allocator provides mprotect services to the user, where LinkerMemoryAllocator
79 * always treats it's memory as READ|WRITE.
82 class LinkerTypeAllocator {
84 LinkerTypeAllocator() : block_allocator_(sizeof(T)) {}
85 T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); }
86 void free(T* t) { block_allocator_.free(t); }
87 void protect_all(int prot) { block_allocator_.protect_all(prot); }
89 LinkerBlockAllocator block_allocator_;
90 DISALLOW_COPY_AND_ASSIGN(LinkerTypeAllocator);
93 #endif // __LINKER_BLOCK_ALLOCATOR_H