linker_block_allocator.cpp revision 600bc3cb9342fbb1dc16ea25f5b676ce072e3e1b
1/*
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
17#include "linker_block_allocator.h"
18#include <inttypes.h>
19#include <string.h>
20#include <sys/mman.h>
21#include <unistd.h>
22
23#include "private/bionic_prctl.h"
24
25struct LinkerBlockAllocatorPage {
26  LinkerBlockAllocatorPage* next;
27  uint8_t bytes[PAGE_SIZE-sizeof(LinkerBlockAllocatorPage*)];
28};
29
30struct FreeBlockInfo {
31  void* next_block;
32  size_t num_free_blocks;
33};
34
35LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size)
36  : block_size_(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size),
37    page_list_(nullptr),
38    free_block_list_(nullptr)
39{}
40
41void* LinkerBlockAllocator::alloc() {
42  if (free_block_list_ == nullptr) {
43    create_new_page();
44  }
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
57  memset(block_info, 0, block_size_);
58
59  return block_info;
60}
61
62void LinkerBlockAllocator::free(void* block) {
63  if (block == nullptr) {
64    return;
65  }
66
67  LinkerBlockAllocatorPage* 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
79  memset(block, 0, block_size_);
80
81  FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block);
82
83  block_info->next_block = free_block_list_;
84  block_info->num_free_blocks = 1;
85
86  free_block_list_ = block_info;
87}
88
89void LinkerBlockAllocator::protect_all(int prot) {
90  for (LinkerBlockAllocatorPage* page = page_list_; page != nullptr; page = page->next) {
91    if (mprotect(page, PAGE_SIZE, prot) == -1) {
92      abort();
93    }
94  }
95}
96
97void LinkerBlockAllocator::create_new_page() {
98  LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>(
99      mmap(nullptr, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
100
101  if (page == MAP_FAILED) {
102    abort(); // oom
103  }
104
105  prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, page, PAGE_SIZE, "linker_alloc");
106
107  memset(page, 0, PAGE_SIZE);
108
109  FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes);
110  first_block->next_block = free_block_list_;
111  first_block->num_free_blocks = (PAGE_SIZE - sizeof(LinkerBlockAllocatorPage*))/block_size_;
112
113  free_block_list_ = first_block;
114
115  page->next = page_list_;
116  page_list_ = page;
117}
118
119LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) {
120  if (block == nullptr) {
121    abort();
122  }
123
124  LinkerBlockAllocatorPage* page = page_list_;
125  while (page != nullptr) {
126    const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page);
127    if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + PAGE_SIZE)) {
128      return page;
129    }
130
131    page = page->next;
132  }
133
134  abort();
135}
136