Change default block size alignment to be 4 for memory saving on 32-bit arch
For a 32-bit userspace, `struct LinkedListEntry` takes 8 bytes for storing the two pointers, a default block allocator size alignment of 16-bytes would waste 50% of memory. By changing the alignment to size of a pointer, it saves >1MB memory postboot on wembley device. Bug: http://b/206889551 Test: bionic-unit-tests Change-Id: Ie92399c9bb3971f631396ee09bbbfd7eb17dc1a7
This commit is contained in:
parent
fbee3177af
commit
08cf949855
|
@ -31,6 +31,7 @@
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
#include <sys/param.h>
|
||||||
#include <sys/prctl.h>
|
#include <sys/prctl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
@ -39,11 +40,6 @@
|
||||||
static constexpr size_t kAllocateSize = PAGE_SIZE * 100;
|
static constexpr size_t kAllocateSize = PAGE_SIZE * 100;
|
||||||
static_assert(kAllocateSize % PAGE_SIZE == 0, "Invalid kAllocateSize.");
|
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 {
|
struct LinkerBlockAllocatorPage {
|
||||||
LinkerBlockAllocatorPage* next;
|
LinkerBlockAllocatorPage* next;
|
||||||
uint8_t bytes[kAllocateSize - 16] __attribute__((aligned(16)));
|
uint8_t bytes[kAllocateSize - 16] __attribute__((aligned(16)));
|
||||||
|
@ -54,13 +50,14 @@ struct FreeBlockInfo {
|
||||||
size_t num_free_blocks;
|
size_t num_free_blocks;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static_assert(kBlockSizeAlign >= alignof(FreeBlockInfo));
|
||||||
|
static_assert(kBlockSizeMin == sizeof(FreeBlockInfo));
|
||||||
|
|
||||||
LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size)
|
LinkerBlockAllocator::LinkerBlockAllocator(size_t block_size)
|
||||||
: block_size_(
|
: block_size_(__BIONIC_ALIGN(MAX(block_size, kBlockSizeMin), kBlockSizeAlign)),
|
||||||
round_up(block_size < sizeof(FreeBlockInfo) ? sizeof(FreeBlockInfo) : block_size, 16)),
|
|
||||||
page_list_(nullptr),
|
page_list_(nullptr),
|
||||||
free_block_list_(nullptr),
|
free_block_list_(nullptr),
|
||||||
allocated_(0)
|
allocated_(0) {}
|
||||||
{}
|
|
||||||
|
|
||||||
void* LinkerBlockAllocator::alloc() {
|
void* LinkerBlockAllocator::alloc() {
|
||||||
if (free_block_list_ == nullptr) {
|
if (free_block_list_ == nullptr) {
|
||||||
|
|
|
@ -33,6 +33,9 @@
|
||||||
|
|
||||||
#include <android-base/macros.h>
|
#include <android-base/macros.h>
|
||||||
|
|
||||||
|
static constexpr size_t kBlockSizeAlign = sizeof(void*);
|
||||||
|
static constexpr size_t kBlockSizeMin = sizeof(void*) * 2;
|
||||||
|
|
||||||
struct LinkerBlockAllocatorPage;
|
struct LinkerBlockAllocatorPage;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
@ -44,12 +45,16 @@ struct test_struct_nominal {
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* this one has size below allocator cap which is 2*sizeof(void*)
|
* this one has size below kBlockSizeAlign
|
||||||
*/
|
*/
|
||||||
struct test_struct_small {
|
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)
|
* 1009 byte struct (1009 is prime)
|
||||||
*/
|
*/
|
||||||
|
@ -58,54 +63,49 @@ struct test_struct_larger {
|
||||||
};
|
};
|
||||||
|
|
||||||
static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
|
static size_t kPageSize = sysconf(_SC_PAGE_SIZE);
|
||||||
};
|
|
||||||
|
|
||||||
TEST(linker_allocator, test_nominal) {
|
template <typename Element>
|
||||||
LinkerTypeAllocator<test_struct_nominal> allocator;
|
void linker_allocator_test_helper() {
|
||||||
|
LinkerTypeAllocator<Element> allocator;
|
||||||
|
|
||||||
test_struct_nominal* ptr1 = allocator.alloc();
|
Element* ptr1 = allocator.alloc();
|
||||||
ASSERT_TRUE(ptr1 != nullptr);
|
ASSERT_TRUE(ptr1 != nullptr);
|
||||||
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % 16);
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % kBlockSizeAlign);
|
||||||
test_struct_nominal* ptr2 = allocator.alloc();
|
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr1) % alignof(Element));
|
||||||
ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(ptr2) % 16);
|
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);
|
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(ptr1);
|
||||||
allocator.free(ptr2);
|
allocator.free(ptr2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}; // anonymous namespace
|
||||||
|
|
||||||
|
TEST(linker_allocator, test_nominal) {
|
||||||
|
linker_allocator_test_helper<test_struct_nominal>();
|
||||||
|
}
|
||||||
|
|
||||||
TEST(linker_allocator, test_small) {
|
TEST(linker_allocator, test_small) {
|
||||||
LinkerTypeAllocator<test_struct_small> allocator;
|
linker_allocator_test_helper<test_struct_small>();
|
||||||
|
}
|
||||||
|
|
||||||
char* ptr1 = reinterpret_cast<char*>(allocator.alloc());
|
TEST(linker_allocator, test_max_align) {
|
||||||
char* ptr2 = reinterpret_cast<char*>(allocator.alloc());
|
linker_allocator_test_helper<test_struct_max_align>();
|
||||||
|
|
||||||
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_larger) {
|
TEST(linker_allocator, test_larger) {
|
||||||
|
linker_allocator_test_helper<test_struct_larger>();
|
||||||
|
|
||||||
LinkerTypeAllocator<test_struct_larger> allocator;
|
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.
|
// 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) {
|
for (size_t i=0; i<n; ++i) {
|
||||||
ASSERT_TRUE(allocator.alloc() != nullptr);
|
ASSERT_TRUE(allocator.alloc() != nullptr);
|
||||||
|
@ -113,7 +113,6 @@ TEST(linker_allocator, test_larger) {
|
||||||
|
|
||||||
test_struct_larger* ptr_to_free = allocator.alloc();
|
test_struct_larger* ptr_to_free = allocator.alloc();
|
||||||
ASSERT_TRUE(ptr_to_free != nullptr);
|
ASSERT_TRUE(ptr_to_free != nullptr);
|
||||||
allocator.free(ptr1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void protect_all() {
|
static void protect_all() {
|
||||||
|
|
Loading…
Reference in New Issue