2014-05-06 01:49:04 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
2017-02-16 00:31:13 +01:00
|
|
|
* All rights reserved.
|
2014-05-06 01:49:04 +02: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.
|
2014-05-06 01:49:04 +02: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.
|
2014-05-06 01:49:04 +02:00
|
|
|
*/
|
|
|
|
|
2018-02-13 23:26:29 +01:00
|
|
|
#pragma once
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
2018-10-25 20:00:00 +02:00
|
|
|
|
|
|
|
#include <android-base/macros.h>
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2021-11-18 19:48:47 +01:00
|
|
|
static constexpr size_t kBlockSizeAlign = sizeof(void*);
|
|
|
|
static constexpr size_t kBlockSizeMin = sizeof(void*) * 2;
|
|
|
|
|
2015-03-10 23:43:50 +01:00
|
|
|
struct LinkerBlockAllocatorPage;
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
/*
|
2015-03-11 01:48:27 +01:00
|
|
|
* This class is a non-template version of the LinkerTypeAllocator
|
2014-05-06 01:49:04 +02:00
|
|
|
* It keeps code inside .cpp file by keeping the interface
|
|
|
|
* template-free.
|
|
|
|
*
|
2015-03-11 01:48:27 +01:00
|
|
|
* Please use LinkerTypeAllocator<type> where possible (everywhere).
|
2014-05-06 01:49:04 +02:00
|
|
|
*/
|
|
|
|
class LinkerBlockAllocator {
|
|
|
|
public:
|
2014-07-25 00:33:25 +02:00
|
|
|
explicit LinkerBlockAllocator(size_t block_size);
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
void* alloc();
|
|
|
|
void free(void* block);
|
|
|
|
void protect_all(int prot);
|
|
|
|
|
2019-01-30 05:23:16 +01:00
|
|
|
// Purge all pages if all previously allocated blocks have been freed.
|
|
|
|
void purge();
|
|
|
|
|
2014-05-06 01:49:04 +02:00
|
|
|
private:
|
|
|
|
void create_new_page();
|
2015-03-10 23:43:50 +01:00
|
|
|
LinkerBlockAllocatorPage* find_page(void* block);
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
size_t block_size_;
|
2015-03-10 23:43:50 +01:00
|
|
|
LinkerBlockAllocatorPage* page_list_;
|
2014-05-06 01:49:04 +02:00
|
|
|
void* free_block_list_;
|
2019-01-30 05:23:16 +01:00
|
|
|
size_t allocated_;
|
2014-05-06 01:49:04 +02:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A simple allocator for the dynamic linker. An allocator allocates instances
|
|
|
|
* of a single fixed-size type. Allocations are backed by page-sized private
|
|
|
|
* anonymous mmaps.
|
2015-03-11 01:48:27 +01:00
|
|
|
*
|
2019-01-24 22:47:13 +01:00
|
|
|
* The differences between this allocator and BionicAllocator are:
|
|
|
|
* 1. This allocator manages space more efficiently. BionicAllocator operates in
|
|
|
|
* power-of-two sized blocks up to 1k, when this implementation splits the
|
|
|
|
* page to aligned size of structure; For example for structures with size
|
|
|
|
* 513 this allocator will use 516 (520 for lp64) bytes of data where
|
|
|
|
* generalized implementation is going to use 1024 sized blocks.
|
2015-03-11 01:48:27 +01:00
|
|
|
*
|
2019-01-29 00:04:40 +01:00
|
|
|
* 2. This allocator does not munmap allocated memory, where BionicAllocator does.
|
2015-03-11 01:48:27 +01:00
|
|
|
*
|
2019-01-24 22:47:13 +01:00
|
|
|
* 3. This allocator provides mprotect services to the user, where BionicAllocator
|
|
|
|
* always treats its memory as READ|WRITE.
|
2014-05-06 01:49:04 +02:00
|
|
|
*/
|
|
|
|
template<typename T>
|
2015-03-10 23:43:50 +01:00
|
|
|
class LinkerTypeAllocator {
|
2014-05-06 01:49:04 +02:00
|
|
|
public:
|
2015-03-10 23:43:50 +01:00
|
|
|
LinkerTypeAllocator() : block_allocator_(sizeof(T)) {}
|
2014-05-06 01:49:04 +02:00
|
|
|
T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); }
|
|
|
|
void free(T* t) { block_allocator_.free(t); }
|
|
|
|
void protect_all(int prot) { block_allocator_.protect_all(prot); }
|
|
|
|
private:
|
|
|
|
LinkerBlockAllocator block_allocator_;
|
2015-03-10 23:43:50 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(LinkerTypeAllocator);
|
2014-05-06 01:49:04 +02:00
|
|
|
};
|