aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVilas Bhat <vilasbhat@google.com>2023-11-28 23:44:19 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2023-11-28 23:44:19 +0000
commitd9959c0a5f79fdfe0cb4da022737e704b66241a6 (patch)
tree6b9e41e78b97b03a7c45b31b0b434f6af8601b1b
parent051b570746bdf4b0f0553716a5ebefce944b831c (diff)
parentee7f9f7d91d8654c73f178ac65e38998828de9b4 (diff)
downloadlibmemunreachable-d9959c0a5f79fdfe0cb4da022737e704b66241a6.tar.gz
Allocator: Remove hardcoded value of page size am: fd3c027f01 am: 53dd09a9a7 am: ee7f9f7d91
Original change: https://android-review.googlesource.com/c/platform/system/memory/libmemunreachable/+/2820720 Change-Id: Ic13b561d88adcea12202c78966105869477cf5c7 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--Allocator.cpp20
1 files changed, 12 insertions, 8 deletions
diff --git a/Allocator.cpp b/Allocator.cpp
index 82118e5..58dce3f 100644
--- a/Allocator.cpp
+++ b/Allocator.cpp
@@ -50,9 +50,12 @@ constexpr size_t const_log2(size_t n, size_t p = 0) {
return (n <= 1) ? p : const_log2(n / 2, p + 1);
}
-static constexpr size_t kPageSize = 4096;
+constexpr unsigned int round_up_to_multiple(unsigned int x, unsigned int y) {
+ return y * ((x + y - 1) / y);
+}
+
+static const size_t kPageSize = getpagesize();
static constexpr size_t kChunkSize = 256 * 1024;
-static constexpr size_t kUsableChunkSize = kChunkSize - kPageSize;
static constexpr size_t kMaxBucketAllocationSize = kChunkSize / 4;
static constexpr size_t kMinBucketAllocationSize = 8;
static constexpr unsigned int kNumBuckets =
@@ -188,7 +191,7 @@ class Chunk {
unsigned int free_count_; // number of available allocations
// bitmap of free allocations.
- uint32_t free_bitmap_[kUsableChunkSize / kMinBucketAllocationSize / 32];
+ uint32_t free_bitmap_[kChunkSize / kMinBucketAllocationSize / 32];
std::max_align_t data_[0];
@@ -197,10 +200,9 @@ class Chunk {
return offset / allocation_size_;
}
void* n_to_ptr(unsigned int n) {
- return reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(data_) + n * allocation_size_);
+ return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(data_) + n * allocation_size_);
}
};
-static_assert(sizeof(Chunk) <= kPageSize, "header must fit in page");
// Override new operator on chunk to use mmap to allocate kChunkSize
void* Chunk::operator new(std::size_t count __attribute__((unused))) noexcept {
@@ -224,9 +226,11 @@ Chunk::Chunk(HeapImpl* heap, int bucket)
heap_(heap),
bucket_(bucket),
allocation_size_(bucket_to_size(bucket)),
- max_allocations_(kUsableChunkSize / allocation_size_),
- first_free_bitmap_(0),
- free_count_(max_allocations_) {
+ first_free_bitmap_(0) {
+ const size_t usable_chunk_size =
+ kChunkSize - round_up_to_multiple(sizeof(Chunk), sizeof(std::max_align_t));
+ max_allocations_ = usable_chunk_size / allocation_size_;
+ free_count_ = max_allocations_;
memset(free_bitmap_, 0xff, sizeof(free_bitmap_));
}