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
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) {