Merge changes Ie92399c9,Ic6f05388
* changes:
Change default block size alignment to be 4 for memory saving on 32-bit arch
Keep allocation of `tail_` outside of LinkedList
diff --git a/linker/linked_list.h b/linker/linked_list.h
index b8a8f0e..2741008 100644
--- a/linker/linked_list.h
+++ b/linker/linked_list.h
@@ -79,76 +79,89 @@
typedef LinkedListIterator<T> iterator;
typedef T* value_type;
- LinkedList() : head_(nullptr), tail_(nullptr) {}
+ // Allocating the head/tail fields separately from the LinkedList struct saves memory in the
+ // Zygote (e.g. because adding an soinfo to a namespace doesn't dirty the page containing the
+ // soinfo).
+ struct LinkedListHeader {
+ LinkedListEntry<T>* head;
+ LinkedListEntry<T>* tail;
+ };
+
+ // The allocator returns a LinkedListEntry<T>* but we want to treat it as a LinkedListHeader
+ // struct instead.
+ static_assert(sizeof(LinkedListHeader) == sizeof(LinkedListEntry<T>));
+ static_assert(alignof(LinkedListHeader) == alignof(LinkedListEntry<T>));
+
+ constexpr LinkedList() : header_(nullptr) {}
~LinkedList() {
clear();
+ if (header_ != nullptr) {
+ Allocator::free(reinterpret_cast<LinkedListEntry<T>*>(header_));
+ }
}
LinkedList(LinkedList&& that) noexcept {
- this->head_ = that.head_;
- this->tail_ = that.tail_;
- that.head_ = that.tail_ = nullptr;
+ this->header_ = that.header_;
+ that.header_ = nullptr;
+ }
+
+ bool empty() const {
+ return header_ == nullptr || header_->head == nullptr;
}
void push_front(T* const element) {
+ alloc_header();
LinkedListEntry<T>* new_entry = Allocator::alloc();
- new_entry->next = head_;
+ new_entry->next = header_->head;
new_entry->element = element;
- head_ = new_entry;
- if (tail_ == nullptr) {
- tail_ = new_entry;
+ header_->head = new_entry;
+ if (header_->tail == nullptr) {
+ header_->tail = new_entry;
}
}
void push_back(T* const element) {
+ alloc_header();
LinkedListEntry<T>* new_entry = Allocator::alloc();
new_entry->next = nullptr;
new_entry->element = element;
- if (tail_ == nullptr) {
- tail_ = head_ = new_entry;
+ if (header_->tail == nullptr) {
+ header_->tail = header_->head = new_entry;
} else {
- tail_->next = new_entry;
- tail_ = new_entry;
+ header_->tail->next = new_entry;
+ header_->tail = new_entry;
}
}
T* pop_front() {
- if (head_ == nullptr) {
- return nullptr;
- }
+ if (empty()) return nullptr;
- LinkedListEntry<T>* entry = head_;
+ LinkedListEntry<T>* entry = header_->head;
T* element = entry->element;
- head_ = entry->next;
+ header_->head = entry->next;
Allocator::free(entry);
- if (head_ == nullptr) {
- tail_ = nullptr;
+ if (header_->head == nullptr) {
+ header_->tail = nullptr;
}
return element;
}
T* front() const {
- if (head_ == nullptr) {
- return nullptr;
- }
-
- return head_->element;
+ return empty() ? nullptr : header_->head->element;
}
void clear() {
- while (head_ != nullptr) {
- LinkedListEntry<T>* p = head_;
- head_ = head_->next;
+ if (empty()) return;
+
+ while (header_->head != nullptr) {
+ LinkedListEntry<T>* p = header_->head;
+ header_->head = header_->head->next;
Allocator::free(p);
}
- tail_ = nullptr;
- }
-
- bool empty() {
- return (head_ == nullptr);
+ header_->tail = nullptr;
}
template<typename F>
@@ -161,7 +174,7 @@
template<typename F>
bool visit(F action) const {
- for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
+ for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
if (!action(e->element)) {
return false;
}
@@ -171,17 +184,18 @@
template<typename F>
void remove_if(F predicate) {
- for (LinkedListEntry<T>* e = head_, *p = nullptr; e != nullptr;) {
+ if (empty()) return;
+ for (LinkedListEntry<T>* e = header_->head, *p = nullptr; e != nullptr;) {
if (predicate(e->element)) {
LinkedListEntry<T>* next = e->next;
if (p == nullptr) {
- head_ = next;
+ header_->head = next;
} else {
p->next = next;
}
- if (tail_ == e) {
- tail_ = p;
+ if (header_->tail == e) {
+ header_->tail = p;
}
Allocator::free(e);
@@ -202,7 +216,7 @@
template<typename F>
T* find_if(F predicate) const {
- for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
+ for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
if (predicate(e->element)) {
return e->element;
}
@@ -212,7 +226,7 @@
}
iterator begin() const {
- return iterator(head_);
+ return iterator(head());
}
iterator end() const {
@@ -220,7 +234,7 @@
}
iterator find(T* value) const {
- for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
+ for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
if (e->element == value) {
return iterator(e);
}
@@ -231,7 +245,7 @@
size_t copy_to_array(T* array[], size_t array_length) const {
size_t sz = 0;
- for (LinkedListEntry<T>* e = head_; sz < array_length && e != nullptr; e = e->next) {
+ for (LinkedListEntry<T>* e = head(); sz < array_length && e != nullptr; e = e->next) {
array[sz++] = e->element;
}
@@ -239,7 +253,7 @@
}
bool contains(const T* el) const {
- for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
+ for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
if (e->element == el) {
return true;
}
@@ -260,7 +274,17 @@
}
private:
- LinkedListEntry<T>* head_;
- LinkedListEntry<T>* tail_;
+ void alloc_header() {
+ if (header_ == nullptr) {
+ header_ = reinterpret_cast<LinkedListHeader*>(Allocator::alloc());
+ header_->head = header_->tail = nullptr;
+ }
+ }
+
+ LinkedListEntry<T>* head() const {
+ return header_ != nullptr ? header_->head : nullptr;
+ }
+
+ LinkedListHeader* header_;
DISALLOW_COPY_AND_ASSIGN(LinkedList);
};
diff --git a/linker/linker_block_allocator.cpp b/linker/linker_block_allocator.cpp
index 5b68b1d..60e5e1c 100644
--- a/linker/linker_block_allocator.cpp
+++ b/linker/linker_block_allocator.cpp
@@ -31,6 +31,7 @@
#include <inttypes.h>
#include <string.h>
#include <sys/mman.h>
+#include <sys/param.h>
#include <sys/prctl.h>
#include <unistd.h>
@@ -39,11 +40,6 @@
static constexpr size_t kAllocateSize = PAGE_SIZE * 100;
static_assert(kAllocateSize % PAGE_SIZE == 0, "Invalid kAllocateSize.");
-// the multiplier should be power of 2
-static constexpr size_t round_up(size_t size, size_t multiplier) {
- return (size + (multiplier - 1)) & ~(multiplier-1);
-}
-
struct LinkerBlockAllocatorPage {
LinkerBlockAllocatorPage* next;
uint8_t bytes[kAllocateSize - 16] __attribute__((aligned(16)));
@@ -54,13 +50,14 @@
size_t num_free_blocks;
};
+static_assert(kBlockSizeAlign >= alignof(FreeBlockInfo));
+static_assert(kBlockSizeMin == sizeof(FreeBlockInfo));
+
LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size)
- : block_size_(
- round_up(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size, 16)),
- page_list_(nullptr),
- free_block_list_(nullptr),
- allocated_(0)
-{}
+ : block_size_(__BIONIC_ALIGN(MAX(block_size, kBlockSizeMin), kBlockSizeAlign)),
+ page_list_(nullptr),
+ free_block_list_(nullptr),
+ allocated_(0) {}
void* LinkerBlockAllocator::alloc() {
if (free_block_list_ == nullptr) {
diff --git a/linker/linker_block_allocator.h b/linker/linker_block_allocator.h
index 8ae4094..32656c7 100644
--- a/linker/linker_block_allocator.h
+++ b/linker/linker_block_allocator.h
@@ -33,6 +33,9 @@
#include <android-base/macros.h>
+static constexpr size_t kBlockSizeAlign = sizeof(void*);
+static constexpr size_t kBlockSizeMin = sizeof(void*) * 2;
+
struct LinkerBlockAllocatorPage;
/*
diff --git a/linker/linker_block_allocator_test.cpp b/linker/linker_block_allocator_test.cpp
index 6fb2b26..56fbee8 100644
--- a/linker/linker_block_allocator_test.cpp
+++ b/linker/linker_block_allocator_test.cpp
@@ -29,6 +29,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
+#include <sys/param.h>
#include <gtest/gtest.h>
@@ -44,12 +45,16 @@
};
/*
- * this one has size below allocator cap which is 2*sizeof(void*)
+ * this one has size below kBlockSizeAlign
*/
struct test_struct_small {
- char str[5];
+ char str[3];
};
+struct test_struct_max_align {
+ char str[16];
+} __attribute__((aligned(16)));
+
/*
* 1009 byte struct (1009 is prime)
*/
@@ -58,54 +63,49 @@
};
static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
-};
-TEST(linker_allocator, test_nominal) {
- LinkerTypeAllocator<test_struct_nominal> allocator;
+template <typename Element>
+void linker_allocator_test_helper() {
+ LinkerTypeAllocator<Element> allocator;
- test_struct_nominal* ptr1 = allocator.alloc();
+ Element* ptr1 = allocator.alloc();
ASSERT_TRUE(ptr1 != nullptr);
- ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16);
- test_struct_nominal* ptr2 = allocator.alloc();
- ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16);
+ ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % kBlockSizeAlign);
+ ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % alignof(Element));
+ Element* ptr2 = allocator.alloc();
+ ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % kBlockSizeAlign);
+ ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % alignof(Element));
ASSERT_TRUE(ptr2 != nullptr);
- // they should be next to each other.
- ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1)+16, reinterpret_cast<uint8_t*>(ptr2));
- ptr1->value = 42;
+ // they should be next to each other.
+ size_t dist = __BIONIC_ALIGN(MAX(sizeof(Element), kBlockSizeMin), kBlockSizeAlign);
+ ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1) + dist, reinterpret_cast<uint8_t*>(ptr2));
allocator.free(ptr1);
allocator.free(ptr2);
}
+}; // anonymous namespace
+
+TEST(linker_allocator, test_nominal) {
+ linker_allocator_test_helper<test_struct_nominal>();
+}
+
TEST(linker_allocator, test_small) {
- LinkerTypeAllocator<test_struct_small> allocator;
+ linker_allocator_test_helper<test_struct_small>();
+}
- char* ptr1 = reinterpret_cast<char*>(allocator.alloc());
- char* ptr2 = reinterpret_cast<char*>(allocator.alloc());
-
- ASSERT_TRUE(ptr1 != nullptr);
- ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16);
- ASSERT_TRUE(ptr2 != nullptr);
- ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16);
- ASSERT_EQ(ptr1+16, ptr2); // aligned to 16
+TEST(linker_allocator, test_max_align) {
+ linker_allocator_test_helper<test_struct_max_align>();
}
TEST(linker_allocator, test_larger) {
+ linker_allocator_test_helper<test_struct_larger>();
+
LinkerTypeAllocator<test_struct_larger> allocator;
- test_struct_larger* ptr1 = allocator.alloc();
- test_struct_larger* ptr2 = allocator.alloc();
-
- ASSERT_TRUE(ptr1 != nullptr);
- ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16);
- ASSERT_TRUE(ptr2 != nullptr);
- ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16);
-
- ASSERT_EQ(reinterpret_cast<uint8_t*>(ptr1) + 1024, reinterpret_cast<uint8_t*>(ptr2));
-
// lets allocate until we reach next page.
- size_t n = kPageSize/sizeof(test_struct_larger) + 1 - 2;
+ size_t n = kPageSize / sizeof(test_struct_larger) + 1;
for (size_t i=0; i<n; ++i) {
ASSERT_TRUE(allocator.alloc() != nullptr);
@@ -113,7 +113,6 @@
test_struct_larger* ptr_to_free = allocator.alloc();
ASSERT_TRUE(ptr_to_free != nullptr);
- allocator.free(ptr1);
}
static void protect_all() {