aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2024-02-03 00:02:59 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2024-02-03 00:02:59 +0000
commit930e5053f87caf6ee3e444576a86a5bbdb7b56b1 (patch)
tree831f95b0a7211905708cc293b55420366f3651b6
parentd21ba5e0b2032d70bb2ed5a922bd0d69bcf25651 (diff)
parentf1d04bff5624f238c22d13e88c81295c5603969b (diff)
downloadlibmemunreachable-simpleperf-release.tar.gz
Snap for 11400057 from f1d04bff5624f238c22d13e88c81295c5603969b to simpleperf-releasesimpleperf-release
Change-Id: I4eda4956404e10657917306d9a9ddbc7f857c2de
-rw-r--r--Allocator.cpp46
-rw-r--r--HeapWalker.h4
-rw-r--r--ScopedSignalHandler.h2
-rw-r--r--tests/MemUnreachable_test.cpp2
4 files changed, 17 insertions, 37 deletions
diff --git a/Allocator.cpp b/Allocator.cpp
index 1eb7e98..58dce3f 100644
--- a/Allocator.cpp
+++ b/Allocator.cpp
@@ -50,18 +50,16 @@ constexpr size_t const_log2(size_t n, size_t p = 0) {
return (n <= 1) ? p : const_log2(n / 2, p + 1);
}
-constexpr unsigned int div_round_up(unsigned int x, unsigned int y) {
- return (x + y - 1) / y;
+constexpr unsigned int round_up_to_multiple(unsigned int x, unsigned int y) {
+ return y * ((x + y - 1) / y);
}
-static constexpr size_t kPageSize = 4096;
+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 =
const_log2(kMaxBucketAllocationSize) - const_log2(kMinBucketAllocationSize) + 1;
-static constexpr unsigned int kUsablePagesPerChunk = kUsableChunkSize / kPageSize;
std::atomic<int> heap_count;
@@ -170,7 +168,6 @@ class Chunk {
void* Alloc();
void Free(void* ptr);
- void Purge();
bool Empty();
static Chunk* ptr_to_chunk(void* ptr) {
@@ -192,23 +189,20 @@ class Chunk {
unsigned int max_allocations_; // maximum number of allocations in the chunk
unsigned int first_free_bitmap_; // index into bitmap for first non-full entry
unsigned int free_count_; // number of available allocations
- unsigned int frees_since_purge_; // number of calls to Free since last Purge
-
- // bitmap of pages that have been dirtied
- uint32_t dirty_pages_[div_round_up(kUsablePagesPerChunk, 32)];
// bitmap of free allocations.
- uint32_t free_bitmap_[kUsableChunkSize / kMinBucketAllocationSize / 32];
+ uint32_t free_bitmap_[kChunkSize / kMinBucketAllocationSize / 32];
- char data_[0];
+ std::max_align_t data_[0];
unsigned int ptr_to_n(void* ptr) {
ptrdiff_t offset = reinterpret_cast<uintptr_t>(ptr) - reinterpret_cast<uintptr_t>(data_);
return offset / allocation_size_;
}
- void* n_to_ptr(unsigned int n) { return data_ + n * allocation_size_; }
+ void* n_to_ptr(unsigned int n) {
+ 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 {
@@ -232,11 +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_),
- frees_since_purge_(0) {
- memset(dirty_pages_, 0, sizeof(dirty_pages_));
+ 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_));
}
@@ -256,10 +250,6 @@ void* Chunk::Alloc() {
unsigned int n = i * 32 + bit;
assert(n < max_allocations_);
- unsigned int page = n * allocation_size_ / kPageSize;
- assert(page / 32 < arraysize(dirty_pages_));
- dirty_pages_[page / 32] |= 1U << (page % 32);
-
free_count_--;
if (free_count_ == 0) {
heap_->MoveToFullList(this, bucket_);
@@ -290,16 +280,6 @@ void Chunk::Free(void* ptr) {
} else {
// TODO(ccross): move down free list if necessary
}
-
- if (frees_since_purge_++ * allocation_size_ > 16 * kPageSize) {
- Purge();
- }
-}
-
-void Chunk::Purge() {
- frees_since_purge_ = 0;
-
- // unsigned int allocsPerPage = kPageSize / allocation_size_;
}
// Override new operator on HeapImpl to use mmap to allocate a page
diff --git a/HeapWalker.h b/HeapWalker.h
index f00bcca..42e4f4b 100644
--- a/HeapWalker.h
+++ b/HeapWalker.h
@@ -64,11 +64,11 @@ class HeapWalker {
valid_mappings_range_.begin = ~valid_allocations_range_.end;
sigsegv_handler_.install(
- SIGSEGV, [=](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
+ SIGSEGV, [this](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
this->HandleSegFault(handler, signal, siginfo, uctx);
});
sigbus_handler_.install(
- SIGBUS, [=](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
+ SIGBUS, [this](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
this->HandleSegFault(handler, signal, siginfo, uctx);
});
}
diff --git a/ScopedSignalHandler.h b/ScopedSignalHandler.h
index ef4473f..8cae4b6 100644
--- a/ScopedSignalHandler.h
+++ b/ScopedSignalHandler.h
@@ -50,7 +50,7 @@ class ScopedSignalHandler {
}
(*handler_map_)[signal] =
- SignalFn([=](int signal, siginfo_t* si, void* uctx) { f(*this, signal, si, uctx); });
+ SignalFn([=, this](int signal, siginfo_t* si, void* uctx) { f(*this, signal, si, uctx); });
struct sigaction act {};
act.sa_sigaction = [](int signal, siginfo_t* si, void* uctx) {
diff --git a/tests/MemUnreachable_test.cpp b/tests/MemUnreachable_test.cpp
index 8bc176b..4e2116b 100644
--- a/tests/MemUnreachable_test.cpp
+++ b/tests/MemUnreachable_test.cpp
@@ -49,7 +49,7 @@ class HiddenPointer {
// Trick the compiler into thinking a value on the stack is still referenced.
static void Ref(void** ptr) {
- void** volatile storage;
+ void** volatile storage [[maybe_unused]];
storage = ptr;
}