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