1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
13  *    distribution.
14  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <stdlib.h>
32 #include <limits.h>
33 
34 #include <android-base/macros.h>
35 
36 struct LinkerBlockAllocatorPage;
37 
38 /*
39  * This class is a non-template version of the LinkerTypeAllocator
40  * It keeps code inside .cpp file by keeping the interface
41  * template-free.
42  *
43  * Please use LinkerTypeAllocator<type> where possible (everywhere).
44  */
45 class LinkerBlockAllocator {
46  public:
47   explicit LinkerBlockAllocator(size_t block_size);
48 
49   void* alloc();
50   void free(void* block);
51   void protect_all(int prot);
52 
53   // Purge all pages if all previously allocated blocks have been freed.
54   void purge();
55 
56  private:
57   void create_new_page();
58   LinkerBlockAllocatorPage* find_page(void* block);
59 
60   size_t block_size_;
61   LinkerBlockAllocatorPage* page_list_;
62   void* free_block_list_;
63   size_t allocated_;
64 
65   DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator);
66 };
67 
68 /*
69  * A simple allocator for the dynamic linker. An allocator allocates instances
70  * of a single fixed-size type. Allocations are backed by page-sized private
71  * anonymous mmaps.
72  *
73  * The differences between this allocator and BionicAllocator are:
74  * 1. This allocator manages space more efficiently. BionicAllocator operates in
75  *    power-of-two sized blocks up to 1k, when this implementation splits the
76  *    page to aligned size of structure; For example for structures with size
77  *    513 this allocator will use 516 (520 for lp64) bytes of data where
78  *    generalized implementation is going to use 1024 sized blocks.
79  *
80  * 2. This allocator does not munmap allocated memory, where BionicAllocator does.
81  *
82  * 3. This allocator provides mprotect services to the user, where BionicAllocator
83  *    always treats its memory as READ|WRITE.
84  */
85 template<typename T>
86 class LinkerTypeAllocator {
87  public:
LinkerTypeAllocator()88   LinkerTypeAllocator() : block_allocator_(sizeof(T)) {}
alloc()89   T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); }
free(T * t)90   void free(T* t) { block_allocator_.free(t); }
protect_all(int prot)91   void protect_all(int prot) { block_allocator_.protect_all(prot); }
92  private:
93   LinkerBlockAllocator block_allocator_;
94   DISALLOW_COPY_AND_ASSIGN(LinkerTypeAllocator);
95 };
96