am 452ddd02: am e8107b66: Merge "Rename LinkerAllocator and LinkerAllocatorPage"

* commit '452ddd0245d9fb790d55206fc9535d02665c28fa':
  Rename LinkerAllocator and LinkerAllocatorPage
This commit is contained in:
Dmitriy Ivanov 2015-03-11 23:02:18 +00:00 committed by Android Git Automerger
commit 3e7d8d200f
4 changed files with 24 additions and 22 deletions

View File

@ -92,8 +92,8 @@ static const char* get_base_name(const char* name) {
static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf); static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
static LinkerAllocator<soinfo> g_soinfo_allocator; static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator; static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
static soinfo* solist; static soinfo* solist;
static soinfo* sonext; static soinfo* sonext;

View File

@ -22,9 +22,9 @@
#include "private/bionic_prctl.h" #include "private/bionic_prctl.h"
struct LinkerAllocatorPage { struct LinkerBlockAllocatorPage {
LinkerAllocatorPage* next; LinkerBlockAllocatorPage* next;
uint8_t bytes[PAGE_SIZE-sizeof(LinkerAllocatorPage*)]; uint8_t bytes[PAGE_SIZE-sizeof(LinkerBlockAllocatorPage*)];
}; };
struct FreeBlockInfo { struct FreeBlockInfo {
@ -64,7 +64,7 @@ void LinkerBlockAllocator::free(void* block) {
return; return;
} }
LinkerAllocatorPage* page = find_page(block); LinkerBlockAllocatorPage* page = find_page(block);
if (page == nullptr) { if (page == nullptr) {
abort(); abort();
@ -87,7 +87,7 @@ void LinkerBlockAllocator::free(void* block) {
} }
void LinkerBlockAllocator::protect_all(int prot) { void LinkerBlockAllocator::protect_all(int prot) {
for (LinkerAllocatorPage* page = page_list_; page != nullptr; page = page->next) { for (LinkerBlockAllocatorPage* page = page_list_; page != nullptr; page = page->next) {
if (mprotect(page, PAGE_SIZE, prot) == -1) { if (mprotect(page, PAGE_SIZE, prot) == -1) {
abort(); abort();
} }
@ -95,8 +95,9 @@ void LinkerBlockAllocator::protect_all(int prot) {
} }
void LinkerBlockAllocator::create_new_page() { void LinkerBlockAllocator::create_new_page() {
LinkerAllocatorPage* page = reinterpret_cast<LinkerAllocatorPage*>(mmap(nullptr, PAGE_SIZE, LinkerBlockAllocatorPage* page = reinterpret_cast<LinkerBlockAllocatorPage*>(
PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0)); mmap(nullptr, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
if (page == MAP_FAILED) { if (page == MAP_FAILED) {
abort(); // oom abort(); // oom
} }
@ -107,7 +108,7 @@ void LinkerBlockAllocator::create_new_page() {
FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes); FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes);
first_block->next_block = free_block_list_; first_block->next_block = free_block_list_;
first_block->num_free_blocks = (PAGE_SIZE - sizeof(LinkerAllocatorPage*))/block_size_; first_block->num_free_blocks = (PAGE_SIZE - sizeof(LinkerBlockAllocatorPage*))/block_size_;
free_block_list_ = first_block; free_block_list_ = first_block;
@ -115,12 +116,12 @@ void LinkerBlockAllocator::create_new_page() {
page_list_ = page; page_list_ = page;
} }
LinkerAllocatorPage* LinkerBlockAllocator::find_page(void* block) { LinkerBlockAllocatorPage* LinkerBlockAllocator::find_page(void* block) {
if (block == nullptr) { if (block == nullptr) {
abort(); abort();
} }
LinkerAllocatorPage* page = page_list_; LinkerBlockAllocatorPage* page = page_list_;
while (page != nullptr) { while (page != nullptr) {
const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page); const uint8_t* page_ptr = reinterpret_cast<const uint8_t*>(page);
if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + PAGE_SIZE)) { if (block >= (page_ptr + sizeof(page->next)) && block < (page_ptr + PAGE_SIZE)) {

View File

@ -21,7 +21,7 @@
#include <limits.h> #include <limits.h>
#include "private/bionic_macros.h" #include "private/bionic_macros.h"
struct LinkerAllocatorPage; struct LinkerBlockAllocatorPage;
/* /*
* This class is a non-template version of the LinkerAllocator * This class is a non-template version of the LinkerAllocator
@ -40,10 +40,10 @@ class LinkerBlockAllocator {
private: private:
void create_new_page(); void create_new_page();
LinkerAllocatorPage* find_page(void* block); LinkerBlockAllocatorPage* find_page(void* block);
size_t block_size_; size_t block_size_;
LinkerAllocatorPage* page_list_; LinkerBlockAllocatorPage* page_list_;
void* free_block_list_; void* free_block_list_;
DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator); DISALLOW_COPY_AND_ASSIGN(LinkerBlockAllocator);
@ -57,14 +57,15 @@ class LinkerBlockAllocator {
* anonymous mmaps. * anonymous mmaps.
*/ */
template<typename T> template<typename T>
class LinkerAllocator { class LinkerTypeAllocator {
public: public:
LinkerAllocator() : block_allocator_(sizeof(T)) {} LinkerTypeAllocator() : block_allocator_(sizeof(T)) {}
T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); } T* alloc() { return reinterpret_cast<T*>(block_allocator_.alloc()); }
void free(T* t) { block_allocator_.free(t); } void free(T* t) { block_allocator_.free(t); }
void protect_all(int prot) { block_allocator_.protect_all(prot); } void protect_all(int prot) { block_allocator_.protect_all(prot); }
private: private:
LinkerBlockAllocator block_allocator_; LinkerBlockAllocator block_allocator_;
DISALLOW_COPY_AND_ASSIGN(LinkerAllocator); DISALLOW_COPY_AND_ASSIGN(LinkerTypeAllocator);
}; };
#endif // __LINKER_ALLOCATOR_H #endif // __LINKER_ALLOCATOR_H

View File

@ -49,7 +49,7 @@ static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
}; };
TEST(linker_allocator, test_nominal) { TEST(linker_allocator, test_nominal) {
LinkerAllocator<test_struct_nominal> allocator; LinkerTypeAllocator<test_struct_nominal> allocator;
test_struct_nominal* ptr1 = allocator.alloc(); test_struct_nominal* ptr1 = allocator.alloc();
ASSERT_TRUE(ptr1 != nullptr); ASSERT_TRUE(ptr1 != nullptr);
@ -65,7 +65,7 @@ TEST(linker_allocator, test_nominal) {
} }
TEST(linker_allocator, test_small) { TEST(linker_allocator, test_small) {
LinkerAllocator<test_struct_small> allocator; LinkerTypeAllocator<test_struct_small> allocator;
char* ptr1 = reinterpret_cast<char*>(allocator.alloc()); char* ptr1 = reinterpret_cast<char*>(allocator.alloc());
char* ptr2 = reinterpret_cast<char*>(allocator.alloc()); char* ptr2 = reinterpret_cast<char*>(allocator.alloc());
@ -76,7 +76,7 @@ TEST(linker_allocator, test_small) {
} }
TEST(linker_allocator, test_larger) { TEST(linker_allocator, test_larger) {
LinkerAllocator<test_struct_larger> allocator; LinkerTypeAllocator<test_struct_larger> allocator;
test_struct_larger* ptr1 = allocator.alloc(); test_struct_larger* ptr1 = allocator.alloc();
test_struct_larger* ptr2 = allocator.alloc(); test_struct_larger* ptr2 = allocator.alloc();
@ -99,7 +99,7 @@ TEST(linker_allocator, test_larger) {
} }
static void protect_all() { static void protect_all() {
LinkerAllocator<test_struct_larger> allocator; LinkerTypeAllocator<test_struct_larger> allocator;
// number of allocs to reach the end of first page // number of allocs to reach the end of first page
size_t n = kPageSize/sizeof(test_struct_larger) - 1; size_t n = kPageSize/sizeof(test_struct_larger) - 1;