blob: 85e6bd9621348540a1d9cb9ca89824197f987051 [file] [log] [blame]
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * 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.
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * 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.
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070027 */
28
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070030
31#include <stdlib.h>
32#include <limits.h>
Elliott Hughes5e62b342018-10-25 11:00:00 -070033
34#include <android-base/macros.h>
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070035
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070036struct LinkerBlockAllocatorPage;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070037
38/*
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070039 * This class is a non-template version of the LinkerTypeAllocator
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070040 * It keeps code inside .cpp file by keeping the interface
41 * template-free.
42 *
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070043 * Please use LinkerTypeAllocator<type> where possible (everywhere).
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070044 */
45class LinkerBlockAllocator {
46 public:
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -070047 explicit LinkerBlockAllocator(size_t block_size);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070048
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070049 void* alloc();
50 void free(void* block);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070051 void protect_all(int prot);
52
53 private:
54 void create_new_page();
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070055 LinkerBlockAllocatorPage* find_page(void* block);
Vic Yangfb78a4a2019-01-23 12:19:41 -080056 void free_all_pages();
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070057
58 size_t block_size_;
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070059 LinkerBlockAllocatorPage* page_list_;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070060 void* free_block_list_;
Vic Yangfb78a4a2019-01-23 12:19:41 -080061 size_t allocated_;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070062
63 DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator);
64};
65
66/*
67 * A simple allocator for the dynamic linker. An allocator allocates instances
68 * of a single fixed-size type. Allocations are backed by page-sized private
69 * anonymous mmaps.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070070 *
71 * The differences between this allocator and LinkerMemoryAllocator are:
72 * 1. This allocator manages space more efficiently. LinkerMemoryAllocator
73 * operates in power-of-two sized blocks up to 1k, when this implementation
74 * splits the page to aligned size of structure; For example for structures
75 * with size 513 this allocator will use 516 (520 for lp64) bytes of data
76 * where generalized implementation is going to use 1024 sized blocks.
77 *
Vic Yangfb78a4a2019-01-23 12:19:41 -080078 * 2. Unless all allocated memory is freed, this allocator does not munmap
79 * allocated memory, where LinkerMemoryAllocator does.
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070080 *
81 * 3. This allocator provides mprotect services to the user, where LinkerMemoryAllocator
82 * always treats it's memory as READ|WRITE.
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070083 */
84template<typename T>
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070085class LinkerTypeAllocator {
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070086 public:
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070087 LinkerTypeAllocator() : block_allocator_(sizeof(T)) {}
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070088 T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); }
89 void free(T* t) { block_allocator_.free(t); }
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070090 void protect_all(int prot) { block_allocator_.protect_all(prot); }
91 private:
92 LinkerBlockAllocator block_allocator_;
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070093 DISALLOW_COPY_AND_ASSIGN(LinkerTypeAllocator);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070094};