blob: 4545c4ae26a5b535daa66e611f7f2c910c4edcd0 [file] [log] [blame]
Dmitriy Ivanovd597d262014-05-05 16:49:04 -07001/*
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 */
Elliott Hughes05fc1d72015-01-28 18:02:33 -080016
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070017#include "linker_block_allocator.h"
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070018#include <inttypes.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080019#include <string.h>
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070020#include <sys/mman.h>
21#include <unistd.h>
22
Dmitriy Ivanov51a22a12014-08-08 16:57:15 -070023#include "private/bionic_prctl.h"
24
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070025struct LinkerAllocatorPage {
26 LinkerAllocatorPage* next;
27 uint8_t bytes[PAGE_SIZE-sizeof(LinkerAllocatorPage*)];
28};
29
30struct FreeBlockInfo {
31 void* next_block;
32 size_t num_free_blocks;
33};
34
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -070035LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size)
36 : block_size_(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size),
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070037 page_list_(nullptr),
38 free_block_list_(nullptr)
39{}
40
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070041void* LinkerBlockAllocator::alloc() {
42 if (free_block_list_ == nullptr) {
43 create_new_page();
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070044 }
45
46 FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_);
47 if (block_info->num_free_blocks > 1) {
48 FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>(
49 reinterpret_cast<char*>(free_block_list_) + block_size_);
50 next_block_info->next_block = block_info->next_block;
51 next_block_info->num_free_blocks = block_info->num_free_blocks - 1;
52 free_block_list_ = next_block_info;
53 } else {
54 free_block_list_ = block_info->next_block;
55 }
56
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070057 memset(block_info, 0, block_size_);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070058
59 return block_info;
60}
61
62void LinkerBlockAllocator::free(void* block) {
63 if (block == nullptr) {
64 return;
65 }
66
67 LinkerAllocatorPage* page = find_page(block);
68
69 if (page == nullptr) {
70 abort();
71 }
72
73 ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes;
74
75 if (offset % block_size_ != 0) {
76 abort();
77 }
78
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070079 memset(block, 0, block_size_);
80
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070081 FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block);
82
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070083 block_info->next_block = free_block_list_;
84 block_info->num_free_blocks = 1;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070085
86 free_block_list_ = block_info;
87}
88
89void LinkerBlockAllocator::protect_all(int prot) {
90 for (LinkerAllocatorPage* page = page_list_; page != nullptr; page = page->next) {
91 if (mprotect(page, PAGE_SIZE, prot) == -1) {
92 abort();
93 }
94 }
95}
96
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070097void LinkerBlockAllocator::create_new_page() {
98 LinkerAllocatorPage* page = reinterpret_cast<LinkerAllocatorPage*>(mmap(nullptr, PAGE_SIZE,
99 PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
100 if (page == MAP_FAILED) {
101 abort(); // oom
102 }
Dmitriy Ivanov51a22a12014-08-08 16:57:15 -0700103
104 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, page, PAGE_SIZE, "linker_alloc");
105
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700106 memset(page, 0, PAGE_SIZE);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700107
108 FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes);
109 first_block->next_block = free_block_list_;
110 first_block->num_free_blocks = (PAGE_SIZE - sizeof(LinkerAllocatorPage*))/block_size_;
111
112 free_block_list_ = first_block;
113
114 page->next = page_list_;
115 page_list_ = page;
116}
117
118LinkerAllocatorPage* LinkerBlockAllocator::find_page(void* block) {
119 if (block == nullptr) {
120 abort();
121 }
122
123 LinkerAllocatorPage* page = page_list_;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700124 while (page != nullptr) {
Dmitriy Ivanov10794062014-05-14 12:52:57 -0700125 const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700126 if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + PAGE_SIZE)) {
127 return page;
128 }
129
130 page = page->next;
131 }
132
133 abort();
134}