Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up Result and make Result move-only #691

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions fuzzing/snmalloc-fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ struct Result
char* ptr;
size_t size;

Result(char filler, char* ptr, size_t size) : filler(filler), ptr(ptr), size(size) {}
Result(Result&& other) noexcept : filler(other.filler), ptr(other.ptr), size(other.size)
{
other.ptr = nullptr;
}
Result &operator=(Result&& other) noexcept
{
if (this != &other)
{
filler = other.filler;
ptr = other.ptr;
size = other.size;
other.ptr = nullptr;
}
return *this;
}

void check()
{
auto res = std::reduce(
Expand All @@ -126,6 +143,14 @@ struct Result
if (res)
abort();
}

~Result()
{
auto alloc = snmalloc::get_scoped_allocator();
if (ptr)
alloc->dealloc(ptr, size);
ptr = nullptr;
}
};

void snmalloc_random_walk(
Expand All @@ -142,7 +167,7 @@ void snmalloc_random_walk(
{
auto ptr =
static_cast<char*>(scoped->alloc<snmalloc::YesZero>(e.size_or_index));
results.push_back({0, ptr, e.size_or_index});
results.emplace_back(0, ptr, e.size_or_index);
break;
}

Expand All @@ -151,7 +176,7 @@ void snmalloc_random_walk(
auto ptr =
static_cast<char*>(scoped->alloc<snmalloc::NoZero>(e.size_or_index));
std::fill(ptr, ptr + e.size_or_index, e.filler);
results.push_back({e.filler, ptr, e.size_or_index});
results.emplace_back(e.filler, ptr, e.size_or_index);
break;
}

Expand All @@ -160,7 +185,6 @@ void snmalloc_random_walk(
if (results.empty())
break;
auto index = e.size_or_index % results.size();
scoped->dealloc(results[index].ptr);
results.erase(results.begin() + static_cast<ptrdiff_t>(index));
break;
}
Expand Down
Loading