]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - linker/linker_allocator.cpp
Merge "With libstdc++ in libc, __futex_wait and __futex_wake can be hidden."
[android-sdk/platform-bionic.git] / linker / linker_allocator.cpp
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>
21 struct LinkerAllocatorPage {
22   LinkerAllocatorPage* next;
23   uint8_t bytes[PAGE_SIZE-sizeof(LinkerAllocatorPage*)];
24 };
26 struct FreeBlockInfo {
27   void* next_block;
28   size_t num_free_blocks;
29 };
31 LinkerBlockAllocator::LinkerBlockAllocator()
32   : block_size_(0),
33     page_list_(nullptr),
34     free_block_list_(nullptr)
35 {}
37 void LinkerBlockAllocator::init(size_t block_size) {
38   block_size_ = block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size;
39 }
42 void* LinkerBlockAllocator::alloc() {
43   if (free_block_list_ == nullptr) {
44     create_new_page();
45   }
47   FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(free_block_list_);
48   if (block_info->num_free_blocks > 1) {
49     FreeBlockInfo* next_block_info = reinterpret_cast<FreeBlockInfo*>(
50       reinterpret_cast<char*>(free_block_list_) + block_size_);
51     next_block_info->next_block = block_info->next_block;
52     next_block_info->num_free_blocks = block_info->num_free_blocks - 1;
53     free_block_list_ = next_block_info;
54   } else {
55     free_block_list_ = block_info->next_block;
56   }
58   memset(block_info, 0, block_size_);
60   return block_info;
61 }
63 void LinkerBlockAllocator::free(void* block) {
64   if (block == nullptr) {
65     return;
66   }
68   LinkerAllocatorPage* page = find_page(block);
70   if (page == nullptr) {
71     abort();
72   }
74   ssize_t offset = reinterpret_cast<uint8_t*>(block) - page->bytes;
76   if (offset % block_size_ != 0) {
77     abort();
78   }
80   memset(block, 0, block_size_);
82   FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block);
84   block_info->next_block = free_block_list_;
85   block_info->num_free_blocks = 1;
87   free_block_list_ = block_info;
88 }
90 void LinkerBlockAllocator::protect_all(int prot) {
91   for (LinkerAllocatorPage* page = page_list_; page != nullptr; page = page->next) {
92     if (mprotect(page, PAGE_SIZE, prot) == -1) {
93       abort();
94     }
95   }
96 }
98 void LinkerBlockAllocator::create_new_page() {
99   LinkerAllocatorPage* page = reinterpret_cast<LinkerAllocatorPage*>(mmap(nullptr, PAGE_SIZE,
100       PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
101   if (page == MAP_FAILED) {
102     abort(); // oom
103   }
104   memset(page, 0, PAGE_SIZE);
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_;
110   free_block_list_ = first_block;
112   page->next = page_list_;
113   page_list_ = page;
116 LinkerAllocatorPage* LinkerBlockAllocator::find_page(void* block) {
117   if (block == nullptr) {
118     abort();
119   }
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     }
128     page = page->next;
129   }
131   abort();