blob: f5d3745b49790b74f3c6332dfdad43a6864ac95e [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 */
16#include "linker_allocator.h"
17#include <inttypes.h>
18#include <sys/mman.h>
19#include <unistd.h>
20
21struct LinkerAllocatorPage {
22 LinkerAllocatorPage* next;
23 uint8_t bytes[PAGE_SIZE-sizeof(LinkerAllocatorPage*)];
24};
25
26struct FreeBlockInfo {
27 void* next_block;
28 size_t num_free_blocks;
29};
30
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -070031LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size)
32 : block_size_(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size),
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070033 page_list_(nullptr),
34 free_block_list_(nullptr)
35{}
36
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070037void* LinkerBlockAllocator::alloc() {
38 if (free_block_list_ == nullptr) {
39 create_new_page();
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070040 }
41
42 FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_);
43 if (block_info->num_free_blocks > 1) {
44 FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>(
45 reinterpret_cast<char*>(free_block_list_) + block_size_);
46 next_block_info->next_block = block_info->next_block;
47 next_block_info->num_free_blocks = block_info->num_free_blocks - 1;
48 free_block_list_ = next_block_info;
49 } else {
50 free_block_list_ = block_info->next_block;
51 }
52
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070053 memset(block_info, 0, block_size_);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070054
55 return block_info;
56}
57
58void LinkerBlockAllocator::free(void* block) {
59 if (block == nullptr) {
60 return;
61 }
62
63 LinkerAllocatorPage* page = find_page(block);
64
65 if (page == nullptr) {
66 abort();
67 }
68
69 ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes;
70
71 if (offset % block_size_ != 0) {
72 abort();
73 }
74
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070075 memset(block, 0, block_size_);
76
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070077 FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block);
78
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070079 block_info->next_block = free_block_list_;
80 block_info->num_free_blocks = 1;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070081
82 free_block_list_ = block_info;
83}
84
85void LinkerBlockAllocator::protect_all(int prot) {
86 for (LinkerAllocatorPage* page = page_list_; page != nullptr; page = page->next) {
87 if (mprotect(page, PAGE_SIZE, prot) == -1) {
88 abort();
89 }
90 }
91}
92
Dmitriy Ivanovd597d262014-05-05 16:49:04 -070093void LinkerBlockAllocator::create_new_page() {
94 LinkerAllocatorPage* page = reinterpret_cast<LinkerAllocatorPage*>(mmap(nullptr, PAGE_SIZE,
95 PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
96 if (page == MAP_FAILED) {
97 abort(); // oom
98 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070099 memset(page, 0, PAGE_SIZE);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700100
101 FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes);
102 first_block->next_block = free_block_list_;
103 first_block->num_free_blocks = (PAGE_SIZE - sizeof(LinkerAllocatorPage*))/block_size_;
104
105 free_block_list_ = first_block;
106
107 page->next = page_list_;
108 page_list_ = page;
109}
110
111LinkerAllocatorPage* LinkerBlockAllocator::find_page(void* block) {
112 if (block == nullptr) {
113 abort();
114 }
115
116 LinkerAllocatorPage* page = page_list_;
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700117 while (page != nullptr) {
Dmitriy Ivanov10794062014-05-14 12:52:57 -0700118 const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page);
Dmitriy Ivanovd597d262014-05-05 16:49:04 -0700119 if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + PAGE_SIZE)) {
120 return page;
121 }
122
123 page = page->next;
124 }
125
126 abort();
127}