2015-03-11 01:48:27 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
2017-02-16 00:31:13 +01:00
|
|
|
* All rights reserved.
|
2015-03-11 01:48:27 +01:00
|
|
|
*
|
2017-02-16 00:31:13 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2015-03-11 01:48:27 +01:00
|
|
|
*
|
2017-02-16 00:31:13 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2015-03-11 01:48:27 +01:00
|
|
|
*/
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
#include "private/bionic_allocator.h"
|
2015-03-11 01:48:27 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2019-01-24 02:46:24 +01:00
|
|
|
#include <string.h>
|
2015-03-11 01:48:27 +01:00
|
|
|
#include <sys/mman.h>
|
2019-01-25 00:22:50 +01:00
|
|
|
#include <sys/param.h>
|
2018-08-22 19:36:23 +02:00
|
|
|
#include <sys/prctl.h>
|
2015-03-11 01:48:27 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-01-24 02:46:24 +01:00
|
|
|
#include <new>
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
#include <async_safe/log.h>
|
2019-02-13 21:39:07 +01:00
|
|
|
#include <async_safe/CHECK.h>
|
2017-04-25 02:48:32 +02:00
|
|
|
|
2019-12-13 00:26:14 +01:00
|
|
|
#include "platform/bionic/page.h"
|
2019-12-20 01:35:51 +01:00
|
|
|
#include "platform/bionic/macros.h"
|
2019-01-24 02:46:24 +01:00
|
|
|
|
2015-03-11 01:48:27 +01:00
|
|
|
//
|
2019-01-24 22:47:13 +01:00
|
|
|
// BionicAllocator is a general purpose allocator designed to provide the same
|
2019-04-02 01:16:05 +02:00
|
|
|
// functionality as the malloc/free/realloc/memalign libc functions.
|
2015-03-11 01:48:27 +01:00
|
|
|
//
|
|
|
|
// On alloc:
|
2019-04-02 01:16:05 +02:00
|
|
|
// If size is > 1k allocator proxies malloc call directly to mmap.
|
|
|
|
// If size <= 1k allocator uses BionicSmallObjectAllocator for the size
|
2015-03-11 01:48:27 +01:00
|
|
|
// rounded up to the nearest power of two.
|
|
|
|
//
|
|
|
|
// On free:
|
|
|
|
//
|
|
|
|
// For a pointer allocated using proxy-to-mmap allocator unmaps
|
|
|
|
// the memory.
|
|
|
|
//
|
2019-04-02 01:16:05 +02:00
|
|
|
// For a pointer allocated using BionicSmallObjectAllocator it adds
|
2018-12-03 08:46:26 +01:00
|
|
|
// the block to free_blocks_list in the corresponding page. If the number of
|
2019-04-02 01:16:05 +02:00
|
|
|
// free pages reaches 2, BionicSmallObjectAllocator munmaps one of the pages
|
|
|
|
// keeping the other one in reserve.
|
2018-12-03 08:46:26 +01:00
|
|
|
|
|
|
|
// Memory management for large objects is fairly straightforward, but for small
|
|
|
|
// objects it is more complicated. If you are changing this code, one simple
|
|
|
|
// way to evaluate the memory usage change is by running 'dd' and examine the
|
|
|
|
// memory usage by 'showmap $(pidof dd)'. 'dd' is nice in that:
|
|
|
|
// 1. It links in quite a few libraries, so you get some linker memory use.
|
|
|
|
// 2. When run with no arguments, it sits waiting for input, so it is easy to
|
|
|
|
// examine its memory usage with showmap.
|
|
|
|
// 3. Since it does nothing while waiting for input, the memory usage is
|
|
|
|
// determinisitic.
|
2015-03-11 01:48:27 +01:00
|
|
|
|
|
|
|
static const char kSignature[4] = {'L', 'M', 'A', 1};
|
|
|
|
|
|
|
|
static const size_t kSmallObjectMaxSize = 1 << kSmallObjectMaxSizeLog2;
|
|
|
|
|
|
|
|
// This type is used for large allocations (with size >1k)
|
|
|
|
static const uint32_t kLargeObject = 111;
|
|
|
|
|
2018-12-05 08:59:57 +01:00
|
|
|
// Allocated pointers must be at least 16-byte aligned. Round up the size of
|
|
|
|
// page_info to multiple of 16.
|
|
|
|
static constexpr size_t kPageInfoSize = __BIONIC_ALIGN(sizeof(page_info), 16);
|
|
|
|
|
2015-03-11 01:48:27 +01:00
|
|
|
static inline uint16_t log2(size_t number) {
|
|
|
|
uint16_t result = 0;
|
|
|
|
number--;
|
|
|
|
|
|
|
|
while (number != 0) {
|
|
|
|
result++;
|
|
|
|
number >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
BionicSmallObjectAllocator::BionicSmallObjectAllocator(uint32_t type,
|
2018-12-03 08:46:26 +01:00
|
|
|
size_t block_size)
|
|
|
|
: type_(type),
|
|
|
|
block_size_(block_size),
|
2018-12-05 08:59:57 +01:00
|
|
|
blocks_per_page_((PAGE_SIZE - sizeof(small_object_page_info)) /
|
|
|
|
block_size),
|
2018-12-03 08:46:26 +01:00
|
|
|
free_pages_cnt_(0),
|
|
|
|
page_list_(nullptr) {}
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
void* BionicSmallObjectAllocator::alloc() {
|
2016-07-13 19:24:06 +02:00
|
|
|
CHECK(block_size_ != 0);
|
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
if (page_list_ == nullptr) {
|
2015-03-11 01:48:27 +01:00
|
|
|
alloc_page();
|
|
|
|
}
|
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
// Fully allocated pages are de-managed and removed from the page list, so
|
|
|
|
// every page from the page list must be useable. Let's just take the first
|
|
|
|
// one.
|
|
|
|
small_object_page_info* page = page_list_;
|
|
|
|
CHECK(page->free_block_list != nullptr);
|
|
|
|
|
|
|
|
small_object_block_record* const block_record = page->free_block_list;
|
2015-03-11 01:48:27 +01:00
|
|
|
if (block_record->free_blocks_cnt > 1) {
|
2018-12-03 08:46:26 +01:00
|
|
|
small_object_block_record* next_free =
|
|
|
|
reinterpret_cast<small_object_block_record*>(
|
|
|
|
reinterpret_cast<uint8_t*>(block_record) + block_size_);
|
2015-03-11 01:48:27 +01:00
|
|
|
next_free->next = block_record->next;
|
|
|
|
next_free->free_blocks_cnt = block_record->free_blocks_cnt - 1;
|
2018-12-03 08:46:26 +01:00
|
|
|
page->free_block_list = next_free;
|
2015-03-11 01:48:27 +01:00
|
|
|
} else {
|
2018-12-03 08:46:26 +01:00
|
|
|
page->free_block_list = block_record->next;
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 08:59:57 +01:00
|
|
|
if (page->free_blocks_cnt == blocks_per_page_) {
|
2015-03-11 01:48:27 +01:00
|
|
|
free_pages_cnt_--;
|
|
|
|
}
|
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
page->free_blocks_cnt--;
|
2015-03-11 01:48:27 +01:00
|
|
|
|
|
|
|
memset(block_record, 0, block_size_);
|
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
if (page->free_blocks_cnt == 0) {
|
|
|
|
// De-manage fully allocated pages. These pages will be managed again if
|
|
|
|
// a block is freed.
|
|
|
|
remove_from_page_list(page);
|
|
|
|
}
|
|
|
|
|
2015-03-11 01:48:27 +01:00
|
|
|
return block_record;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
void BionicSmallObjectAllocator::free_page(small_object_page_info* page) {
|
2018-12-05 08:59:57 +01:00
|
|
|
CHECK(page->free_blocks_cnt == blocks_per_page_);
|
2018-12-03 08:46:26 +01:00
|
|
|
if (page->prev_page) {
|
|
|
|
page->prev_page->next_page = page->next_page;
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
2018-12-03 08:46:26 +01:00
|
|
|
if (page->next_page) {
|
|
|
|
page->next_page->prev_page = page->prev_page;
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
2018-12-03 08:46:26 +01:00
|
|
|
if (page_list_ == page) {
|
|
|
|
page_list_ = page->next_page;
|
|
|
|
}
|
|
|
|
munmap(page, PAGE_SIZE);
|
2015-03-11 01:48:27 +01:00
|
|
|
free_pages_cnt_--;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
void BionicSmallObjectAllocator::free(void* ptr) {
|
2018-12-03 08:46:26 +01:00
|
|
|
small_object_page_info* const page =
|
|
|
|
reinterpret_cast<small_object_page_info*>(
|
|
|
|
PAGE_START(reinterpret_cast<uintptr_t>(ptr)));
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2018-12-05 08:59:57 +01:00
|
|
|
if (reinterpret_cast<uintptr_t>(ptr) % block_size_ != 0) {
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_fatal("invalid pointer: %p (block_size=%zd)", ptr, block_size_);
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(ptr, 0, block_size_);
|
2018-12-03 08:46:26 +01:00
|
|
|
small_object_block_record* const block_record =
|
|
|
|
reinterpret_cast<small_object_block_record*>(ptr);
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
block_record->next = page->free_block_list;
|
2015-03-11 01:48:27 +01:00
|
|
|
block_record->free_blocks_cnt = 1;
|
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
page->free_block_list = block_record;
|
|
|
|
page->free_blocks_cnt++;
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2018-12-05 08:59:57 +01:00
|
|
|
if (page->free_blocks_cnt == blocks_per_page_) {
|
2018-12-03 08:46:26 +01:00
|
|
|
if (++free_pages_cnt_ > 1) {
|
2015-03-11 01:48:27 +01:00
|
|
|
// if we already have a free page - unmap this one.
|
2018-12-03 08:46:26 +01:00
|
|
|
free_page(page);
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
2018-12-03 08:46:26 +01:00
|
|
|
} else if (page->free_blocks_cnt == 1) {
|
|
|
|
// We just freed from a full page. Add this page back to the list.
|
|
|
|
add_to_page_list(page);
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
void BionicSmallObjectAllocator::alloc_page() {
|
2018-12-03 08:46:26 +01:00
|
|
|
void* const map_ptr = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2015-03-11 01:48:27 +01:00
|
|
|
if (map_ptr == MAP_FAILED) {
|
2017-09-16 01:09:22 +02:00
|
|
|
async_safe_fatal("mmap failed: %s", strerror(errno));
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, PAGE_SIZE,
|
2019-01-24 22:47:13 +01:00
|
|
|
"bionic_alloc_small_objects");
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
small_object_page_info* const page =
|
|
|
|
reinterpret_cast<small_object_page_info*>(map_ptr);
|
|
|
|
memcpy(page->info.signature, kSignature, sizeof(kSignature));
|
|
|
|
page->info.type = type_;
|
|
|
|
page->info.allocator_addr = this;
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2018-12-05 08:59:57 +01:00
|
|
|
page->free_blocks_cnt = blocks_per_page_;
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2018-12-05 08:59:57 +01:00
|
|
|
// Align the first block to block_size_.
|
|
|
|
const uintptr_t first_block_addr =
|
|
|
|
__BIONIC_ALIGN(reinterpret_cast<uintptr_t>(page + 1), block_size_);
|
2018-12-03 08:46:26 +01:00
|
|
|
small_object_block_record* const first_block =
|
2018-12-05 08:59:57 +01:00
|
|
|
reinterpret_cast<small_object_block_record*>(first_block_addr);
|
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
first_block->next = nullptr;
|
2018-12-05 08:59:57 +01:00
|
|
|
first_block->free_blocks_cnt = blocks_per_page_;
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2018-12-03 08:46:26 +01:00
|
|
|
page->free_block_list = first_block;
|
|
|
|
|
|
|
|
add_to_page_list(page);
|
2018-11-27 22:34:44 +01:00
|
|
|
|
|
|
|
free_pages_cnt_++;
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
void BionicSmallObjectAllocator::add_to_page_list(small_object_page_info* page) {
|
2018-12-03 08:46:26 +01:00
|
|
|
page->next_page = page_list_;
|
|
|
|
page->prev_page = nullptr;
|
|
|
|
if (page_list_) {
|
|
|
|
page_list_->prev_page = page;
|
|
|
|
}
|
|
|
|
page_list_ = page;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
void BionicSmallObjectAllocator::remove_from_page_list(
|
2018-12-03 08:46:26 +01:00
|
|
|
small_object_page_info* page) {
|
|
|
|
if (page->prev_page) {
|
|
|
|
page->prev_page->next_page = page->next_page;
|
|
|
|
}
|
|
|
|
if (page->next_page) {
|
|
|
|
page->next_page->prev_page = page->prev_page;
|
|
|
|
}
|
|
|
|
if (page_list_ == page) {
|
|
|
|
page_list_ = page->next_page;
|
|
|
|
}
|
|
|
|
page->prev_page = nullptr;
|
|
|
|
page->next_page = nullptr;
|
|
|
|
}
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
void BionicAllocator::initialize_allocators() {
|
2016-07-29 22:25:33 +02:00
|
|
|
if (allocators_ != nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
BionicSmallObjectAllocator* allocators =
|
|
|
|
reinterpret_cast<BionicSmallObjectAllocator*>(allocators_buf_);
|
2016-07-29 22:25:33 +02:00
|
|
|
|
2015-03-11 01:48:27 +01:00
|
|
|
for (size_t i = 0; i < kSmallObjectAllocatorsCount; ++i) {
|
|
|
|
uint32_t type = i + kSmallObjectMinSizeLog2;
|
2019-01-24 22:47:13 +01:00
|
|
|
new (allocators + i) BionicSmallObjectAllocator(type, 1 << type);
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
2016-07-29 22:25:33 +02:00
|
|
|
|
|
|
|
allocators_ = allocators;
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 00:22:50 +01:00
|
|
|
void* BionicAllocator::alloc_mmap(size_t align, size_t size) {
|
|
|
|
size_t header_size = __BIONIC_ALIGN(kPageInfoSize, align);
|
|
|
|
size_t allocated_size;
|
|
|
|
if (__builtin_add_overflow(header_size, size, &allocated_size) ||
|
|
|
|
PAGE_END(allocated_size) < allocated_size) {
|
|
|
|
async_safe_fatal("overflow trying to alloc %zu bytes", size);
|
|
|
|
}
|
|
|
|
allocated_size = PAGE_END(allocated_size);
|
2017-09-16 01:09:22 +02:00
|
|
|
void* map_ptr = mmap(nullptr, allocated_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
|
|
|
|
-1, 0);
|
2015-03-11 01:48:27 +01:00
|
|
|
|
|
|
|
if (map_ptr == MAP_FAILED) {
|
2017-09-16 01:09:22 +02:00
|
|
|
async_safe_fatal("mmap failed: %s", strerror(errno));
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, map_ptr, allocated_size, "bionic_alloc_lob");
|
2015-03-11 01:48:27 +01:00
|
|
|
|
2019-01-25 00:22:50 +01:00
|
|
|
void* result = static_cast<char*>(map_ptr) + header_size;
|
|
|
|
page_info* info = get_page_info_unchecked(result);
|
2015-03-11 01:48:27 +01:00
|
|
|
memcpy(info->signature, kSignature, sizeof(kSignature));
|
|
|
|
info->type = kLargeObject;
|
|
|
|
info->allocated_size = allocated_size;
|
|
|
|
|
2019-01-25 00:22:50 +01:00
|
|
|
return result;
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-25 00:22:50 +01:00
|
|
|
inline void* BionicAllocator::alloc_impl(size_t align, size_t size) {
|
2015-03-11 01:48:27 +01:00
|
|
|
if (size > kSmallObjectMaxSize) {
|
2019-01-25 00:22:50 +01:00
|
|
|
return alloc_mmap(align, size);
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t log2_size = log2(size);
|
|
|
|
|
|
|
|
if (log2_size < kSmallObjectMinSizeLog2) {
|
|
|
|
log2_size = kSmallObjectMinSizeLog2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_small_object_allocator(log2_size)->alloc();
|
|
|
|
}
|
|
|
|
|
2019-01-25 00:22:50 +01:00
|
|
|
void* BionicAllocator::alloc(size_t size) {
|
|
|
|
// treat alloc(0) as alloc(1)
|
|
|
|
if (size == 0) {
|
|
|
|
size = 1;
|
|
|
|
}
|
|
|
|
return alloc_impl(16, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* BionicAllocator::memalign(size_t align, size_t size) {
|
|
|
|
// The Bionic allocator only supports alignment up to one page, which is good
|
|
|
|
// enough for ELF TLS.
|
|
|
|
align = MIN(align, PAGE_SIZE);
|
|
|
|
align = MAX(align, 16);
|
|
|
|
if (!powerof2(align)) {
|
|
|
|
align = BIONIC_ROUND_UP_POWER_OF_2(align);
|
|
|
|
}
|
|
|
|
size = MAX(size, align);
|
|
|
|
return alloc_impl(align, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline page_info* BionicAllocator::get_page_info_unchecked(void* ptr) {
|
|
|
|
uintptr_t header_page = PAGE_START(reinterpret_cast<size_t>(ptr) - kPageInfoSize);
|
|
|
|
return reinterpret_cast<page_info*>(header_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline page_info* BionicAllocator::get_page_info(void* ptr) {
|
|
|
|
page_info* info = get_page_info_unchecked(ptr);
|
2015-03-11 01:48:27 +01:00
|
|
|
if (memcmp(info->signature, kSignature, sizeof(kSignature)) != 0) {
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
void* BionicAllocator::realloc(void* ptr, size_t size) {
|
2015-03-11 01:48:27 +01:00
|
|
|
if (ptr == nullptr) {
|
|
|
|
return alloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
free(ptr);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
page_info* info = get_page_info(ptr);
|
|
|
|
|
|
|
|
size_t old_size = 0;
|
|
|
|
|
|
|
|
if (info->type == kLargeObject) {
|
2019-01-25 00:22:50 +01:00
|
|
|
old_size = info->allocated_size - (static_cast<char*>(ptr) - reinterpret_cast<char*>(info));
|
2015-03-11 01:48:27 +01:00
|
|
|
} else {
|
2019-01-24 22:47:13 +01:00
|
|
|
BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
|
2015-03-11 01:48:27 +01:00
|
|
|
if (allocator != info->allocator_addr) {
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_fatal("invalid pointer %p (page signature mismatch)", ptr);
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
old_size = allocator->get_block_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_size < size) {
|
|
|
|
void *result = alloc(size);
|
|
|
|
memcpy(result, ptr, old_size);
|
|
|
|
free(ptr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
void BionicAllocator::free(void* ptr) {
|
2015-03-11 01:48:27 +01:00
|
|
|
if (ptr == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
page_info* info = get_page_info(ptr);
|
|
|
|
|
|
|
|
if (info->type == kLargeObject) {
|
|
|
|
munmap(info, info->allocated_size);
|
|
|
|
} else {
|
2019-01-24 22:47:13 +01:00
|
|
|
BionicSmallObjectAllocator* allocator = get_small_object_allocator(info->type);
|
2015-03-11 01:48:27 +01:00
|
|
|
if (allocator != info->allocator_addr) {
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_fatal("invalid pointer %p (invalid allocator address for the page)", ptr);
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
allocator->free(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:13 +01:00
|
|
|
BionicSmallObjectAllocator* BionicAllocator::get_small_object_allocator(uint32_t type) {
|
2015-03-11 01:48:27 +01:00
|
|
|
if (type < kSmallObjectMinSizeLog2 || type > kSmallObjectMaxSizeLog2) {
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_fatal("invalid type: %u", type);
|
2015-03-11 01:48:27 +01:00
|
|
|
}
|
|
|
|
|
2016-07-29 22:25:33 +02:00
|
|
|
initialize_allocators();
|
2015-03-11 01:48:27 +01:00
|
|
|
return &allocators_[type - kSmallObjectMinSizeLog2];
|
|
|
|
}
|