blob: 7deddea4a51a4e685d939153a0733209101e89b9 [file] [log] [blame]
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -07001/*
2 * Copyright (C) 2015 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 */
16
17#include "linker_allocator.h"
Dimitry Ivanovf8572112016-07-13 10:24:06 -070018#include "linker_debug.h"
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070019#include "linker.h"
20
21#include <algorithm>
22#include <vector>
23
24#include <stdlib.h>
25#include <sys/mman.h>
26#include <unistd.h>
27
28#include "private/bionic_prctl.h"
29
30//
31// LinkerMemeoryAllocator is general purpose allocator
32// designed to provide the same functionality as the malloc/free/realloc
33// libc functions.
34//
35// On alloc:
36// If size is >= 1k allocator proxies malloc call directly to mmap
37// If size < 1k allocator uses SmallObjectAllocator for the size
38// rounded up to the nearest power of two.
39//
40// On free:
41//
42// For a pointer allocated using proxy-to-mmap allocator unmaps
43// the memory.
44//
45// For a pointer allocated using SmallObjectAllocator it adds
46// the block to free_blocks_list_. If the number of free pages reaches 2,
47// SmallObjectAllocator munmaps one of the pages keeping the other one
48// in reserve.
49
50static const char kSignature[4] = {'L', 'M', 'A', 1};
51
52static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2;
53
54// This type is used for large allocations (with size >1k)
55static const uint32_t kLargeObject = 111;
56
57bool operator<(const small_object_page_record& one, const small_object_page_record& two) {
58 return one.page_addr < two.page_addr;
59}
60
61static inline uint16_t log2(size_t number) {
62 uint16_t result = 0;
63 number--;
64
65 while (number != 0) {
66 result++;
67 number >>= 1;
68 }
69
70 return result;
71}
72
73LinkerSmallObjectAllocator::LinkerSmallObjectAllocator()
Dimitry Ivanovd9d6a842016-01-26 17:53:17 -080074 : type_(0), block_size_(0), free_pages_cnt_(0), free_blocks_list_(nullptr) {}
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070075
76void* LinkerSmallObjectAllocator::alloc() {
Dimitry Ivanovf8572112016-07-13 10:24:06 -070077 CHECK(block_size_ != 0);
78
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -070079 if (free_blocks_list_ == nullptr) {
80 alloc_page();
81 }
82
83 small_object_block_record* block_record = free_blocks_list_;
84 if (block_record->free_blocks_cnt > 1) {
85 small_object_block_record* next_free = reinterpret_cast<small_object_block_record*>(
86 reinterpret_cast<uint8_t*>(block_record) + block_size_);
87 next_free->next = block_record->next;
88 next_free->free_blocks_cnt = block_record->free_blocks_cnt - 1;
89 free_blocks_list_ = next_free;
90 } else {
91 free_blocks_list_ = block_record->next;
92 }
93
94 // bookkeeping...
95 auto page_record = find_page_record(block_record);
96
97 if (page_record->allocated_blocks_cnt == 0) {
98 free_pages_cnt_--;
99 }
100
101 page_record->free_blocks_cnt--;
102 page_record->allocated_blocks_cnt++;
103
104 memset(block_record, 0, block_size_);
105
106 return block_record;
107}
108
109void LinkerSmallObjectAllocator::free_page(linker_vector_t::iterator page_record) {
110 void* page_start = reinterpret_cast<void*>(page_record->page_addr);
111 void* page_end = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(page_start) + PAGE_SIZE);
112
113 while (free_blocks_list_ != nullptr &&
114 free_blocks_list_ > page_start &&
115 free_blocks_list_ < page_end) {
116 free_blocks_list_ = free_blocks_list_->next;
117 }
118
119 small_object_block_record* current = free_blocks_list_;
120
121 while (current != nullptr) {
122 while (current->next > page_start && current->next < page_end) {
123 current->next = current->next->next;
124 }
125
126 current = current->next;
127 }
128
129 munmap(page_start, PAGE_SIZE);
130 page_records_.erase(page_record);
131 free_pages_cnt_--;
132}
133
134void LinkerSmallObjectAllocator::free(void* ptr) {
135 auto page_record = find_page_record(ptr);
136
137 ssize_t offset = reinterpret_cast<uintptr_t>(ptr) - sizeof(page_info);
138
139 if (offset % block_size_ != 0) {
140 __libc_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_);
141 }
142
143 memset(ptr, 0, block_size_);
144 small_object_block_record* block_record = reinterpret_cast<small_object_block_record*>(ptr);
145
146 block_record->next = free_blocks_list_;
147 block_record->free_blocks_cnt = 1;
148
149 free_blocks_list_ = block_record;
150
151 page_record->free_blocks_cnt++;
152 page_record->allocated_blocks_cnt--;
153
154 if (page_record->allocated_blocks_cnt == 0) {
155 if (free_pages_cnt_++ > 1) {
156 // if we already have a free page - unmap this one.
157 free_page(page_record);
158 }
159 }
160}
161
Dimitry Ivanovd9d6a842016-01-26 17:53:17 -0800162void LinkerSmallObjectAllocator::init(uint32_t type, size_t block_size) {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700163 type_ = type;
164 block_size_ = block_size;
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700165}
166
167linker_vector_t::iterator LinkerSmallObjectAllocator::find_page_record(void* ptr) {
168 void* addr = reinterpret_cast<void*>(PAGE_START(reinterpret_cast<uintptr_t>(ptr)));
169 small_object_page_record boundary;
170 boundary.page_addr = addr;
171 linker_vector_t::iterator it = std::lower_bound(
172 page_records_.begin(), page_records_.end(), boundary);
173
174 if (it == page_records_.end() || it->page_addr != addr) {
175 // not found...
176 __libc_fatal("page record for %p was not found (block_size=%zd)", ptr, block_size_);
177 }
178
179 return it;
180}
181
182void LinkerSmallObjectAllocator::create_page_record(void* page_addr, size_t free_blocks_cnt) {
183 small_object_page_record record;
184 record.page_addr = page_addr;
185 record.free_blocks_cnt = free_blocks_cnt;
186 record.allocated_blocks_cnt = 0;
187
188 linker_vector_t::iterator it = std::lower_bound(
189 page_records_.begin(), page_records_.end(), record);
190 page_records_.insert(it, record);
191}
192
193void LinkerSmallObjectAllocator::alloc_page() {
Dimitry Ivanov3edc5c42016-01-21 10:55:40 -0800194 static_assert(sizeof(page_info) % 16 == 0,
195 "sizeof(page_info) is not multiple of 16");
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700196 void* map_ptr = mmap(nullptr, PAGE_SIZE,
197 PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
198 if (map_ptr == MAP_FAILED) {
199 __libc_fatal("mmap failed");
200 }
201
Dimitry Ivanovd9d6a842016-01-26 17:53:17 -0800202 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE, "linker_alloc_small_objects");
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700203
204 memset(map_ptr, 0, PAGE_SIZE);
205
206 page_info* info = reinterpret_cast<page_info*>(map_ptr);
207 memcpy(info->signature, kSignature, sizeof(kSignature));
208 info->type = type_;
209 info->allocator_addr = this;
210
211 size_t free_blocks_cnt = (PAGE_SIZE - sizeof(page_info))/block_size_;
212
213 create_page_record(map_ptr, free_blocks_cnt);
214
215 small_object_block_record* first_block = reinterpret_cast<small_object_block_record*>(info + 1);
216
217 first_block->next = free_blocks_list_;
218 first_block->free_blocks_cnt = free_blocks_cnt;
219
220 free_blocks_list_ = first_block;
221}
222
223
224LinkerMemoryAllocator::LinkerMemoryAllocator() {
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700225 for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) {
226 uint32_t type = i + kSmallObjectMinSizeLog2;
Dimitry Ivanovd9d6a842016-01-26 17:53:17 -0800227 allocators_[i].init(type, 1 << type);
Dmitriy Ivanov19656ce2015-03-10 17:48:27 -0700228 }
229}
230
231void* LinkerMemoryAllocator::alloc_mmap(size_t size) {
232 size_t allocated_size = PAGE_END(size + sizeof(page_info));
233 void* map_ptr = mmap(nullptr, allocated_size,
234 PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
235
236 if (map_ptr == MAP_FAILED) {
237 __libc_fatal("mmap failed");
238 }
239
240 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "linker_alloc_lob");
241
242 memset(map_ptr, 0, allocated_size);
243
244 page_info* info = reinterpret_cast<page_info*>(map_ptr);
245 memcpy(info->signature, kSignature, sizeof(kSignature));
246 info->type = kLargeObject;
247 info->allocated_size = allocated_size;
248
249 return info + 1;
250}
251
252void* LinkerMemoryAllocator::alloc(size_t size) {
253 // treat alloc(0) as alloc(1)
254 if (size == 0) {
255 size = 1;
256 }
257
258 if (size > kSmallObjectMaxSize) {
259 return alloc_mmap(size);
260 }
261
262 uint16_t log2_size = log2(size);
263
264 if (log2_size < kSmallObjectMinSizeLog2) {
265 log2_size = kSmallObjectMinSizeLog2;
266 }
267
268 return get_small_object_allocator(log2_size)->alloc();
269}
270
271page_info* LinkerMemoryAllocator::get_page_info(void* ptr) {
272 page_info* info = reinterpret_cast<page_info*>(PAGE_START(reinterpret_cast<size_t>(ptr)));
273 if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) {
274 __libc_fatal("invalid pointer %p (page signature mismatch)", ptr);
275 }
276
277 return info;
278}
279
280void* LinkerMemoryAllocator::realloc(void* ptr, size_t size) {
281 if (ptr == nullptr) {
282 return alloc(size);
283 }
284
285 if (size == 0) {
286 free(ptr);
287 return nullptr;
288 }
289
290 page_info* info = get_page_info(ptr);
291
292 size_t old_size = 0;
293
294 if (info->type == kLargeObject) {
295 old_size = info->allocated_size - sizeof(page_info);
296 } else {
297 LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
298 if (allocator != info->allocator_addr) {
299 __libc_fatal("invalid pointer %p (page signature mismatch)", ptr);
300 }
301
302 old_size = allocator->get_block_size();
303 }
304
305 if (old_size < size) {
306 void *result = alloc(size);
307 memcpy(result, ptr, old_size);
308 free(ptr);
309 return result;
310 }
311
312 return ptr;
313}
314
315void LinkerMemoryAllocator::free(void* ptr) {
316 if (ptr == nullptr) {
317 return;
318 }
319
320 page_info* info = get_page_info(ptr);
321
322 if (info->type == kLargeObject) {
323 munmap(info, info->allocated_size);
324 } else {
325 LinkerSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
326 if (allocator != info->allocator_addr) {
327 __libc_fatal("invalid pointer %p (invalid allocator address for the page)", ptr);
328 }
329
330 allocator->free(ptr);
331 }
332}
333
334LinkerSmallObjectAllocator* LinkerMemoryAllocator::get_small_object_allocator(uint32_t type) {
335 if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) {
336 __libc_fatal("invalid type: %u", type);
337 }
338
339 return &allocators_[type - kSmallObjectMinSizeLog2];
340}