Skip to content

Commit

Permalink
Guard TestSysAlloc map with mutex. (#76)
Browse files Browse the repository at this point in the history
* Guard TestSysAlloc map with mutex.

* Fix mutex cycle.
  • Loading branch information
ClaytonKnittel authored Nov 17, 2024
1 parent 3cf9029 commit 1dcb387
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/ckmalloc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ cc_library(
"@abseil-cpp//absl/status:statusor",
"@abseil-cpp//absl/strings",
"@abseil-cpp//absl/strings:str_format",
"@abseil-cpp//absl/synchronization",
],
)

Expand Down
28 changes: 20 additions & 8 deletions src/ckmalloc/testlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>

#include "absl/status/statusor.h"
#include "absl/synchronization/mutex.h"

#include "src/ckmalloc/common.h"
#include "src/ckmalloc/slab.h"
Expand Down Expand Up @@ -122,13 +123,16 @@ TestHeap* RandomHeapFromFactory(bench::HeapFactory& heap_factory) {

TestSysAlloc::TestSysAlloc(bench::HeapFactory* heap_factory)
: heap_factory_(heap_factory) {
heap_factory_->WithInstances<void>([this](const auto& instances) {
for (const auto& heap : instances) {
// Assume all already-created heaps are metadata heaps.
heap_map_.emplace(heap->Start(),
std::make_pair(HeapType::kMetadataHeap, heap.get()));
}
});
absl::MutexLock lock(&mutex_);
heap_factory_->WithInstances<void>(
[this](const auto& instances) CK_EXCLUSIVE_LOCKS_REQUIRED(mutex_) {
for (const auto& heap : instances) {
// Assume all already-created heaps are metadata heaps.
heap_map_.emplace(
heap->Start(),
std::make_pair(HeapType::kMetadataHeap, heap.get()));
}
});
}

/* static */
Expand Down Expand Up @@ -160,12 +164,16 @@ void* TestSysAlloc::Mmap(void* start_hint, size_t size, HeapType type) {

bench::Heap* heap = result.value();
void* heap_start = heap->Start();
heap_map_.emplace(heap_start, std::make_pair(type, heap));
{
absl::MutexLock lock(&mutex_);
heap_map_.emplace(heap_start, std::make_pair(type, heap));
}

return heap_start;
}

void TestSysAlloc::Munmap(void* ptr, size_t size) {
absl::MutexLock lock(&mutex_);
auto it = heap_map_.find(ptr);
CK_ASSERT_TRUE(it != heap_map_.end());
bench::Heap* heap = it->second.second;
Expand All @@ -188,20 +196,24 @@ void TestSysAlloc::Sbrk(void* heap_start, size_t increment, void* current_end) {
}

bench::Heap* TestSysAlloc::HeapFromStart(void* heap_start) {
absl::MutexLock lock(&mutex_);
auto it = heap_map_.find(heap_start);
CK_ASSERT_TRUE(it != heap_map_.end());
return it->second.second;
}

size_t TestSysAlloc::Size() const {
absl::MutexLock lock(&mutex_);
return heap_map_.size();
}

TestSysAlloc::const_iterator TestSysAlloc::begin() const {
absl::MutexLock lock(&mutex_);
return heap_map_.begin();
}

TestSysAlloc::const_iterator TestSysAlloc::end() const {
absl::MutexLock lock(&mutex_);
return heap_map_.end();
}

Expand Down
23 changes: 14 additions & 9 deletions src/ckmalloc/testlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/synchronization/mutex.h"

#include "src/ckmalloc/block.h"
#include "src/ckmalloc/common.h"
Expand Down Expand Up @@ -248,27 +249,31 @@ class TestSysAlloc : public SysAlloc {

static void Reset();

void* Mmap(void* start_hint, size_t size, HeapType type) override;
void* Mmap(void* start_hint, size_t size, HeapType type) override
CK_LOCKS_EXCLUDED(mutex_);

void Munmap(void* ptr, size_t size) override;
void Munmap(void* ptr, size_t size) override CK_LOCKS_EXCLUDED(mutex_);

void Sbrk(void* heap_start, size_t increment, void* current_end) override;
void Sbrk(void* heap_start, size_t increment, void* current_end) override
CK_LOCKS_EXCLUDED(mutex_);

bench::Heap* HeapFromStart(void* heap_start);
bench::Heap* HeapFromStart(void* heap_start) CK_LOCKS_EXCLUDED(mutex_);

size_t Size() const;
size_t Size() const CK_LOCKS_EXCLUDED(mutex_);

const_iterator begin() const;
const_iterator end() const;
const_iterator begin() const CK_LOCKS_EXCLUDED(mutex_);
const_iterator end() const CK_LOCKS_EXCLUDED(mutex_);

auto Find(void* heap_start) const {
auto Find(void* heap_start) const CK_LOCKS_EXCLUDED(mutex_) {
absl::MutexLock lock(&mutex_);
return heap_map_.find(heap_start);
}

private:
bench::HeapFactory* heap_factory_;

MapT heap_map_;
mutable absl::Mutex mutex_;
MapT heap_map_ CK_GUARDED_BY(mutex_);
};

class CkMallocTest {
Expand Down

0 comments on commit 1dcb387

Please sign in to comment.