Skip to content

Commit

Permalink
clean up Result and make Result move-only
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingerZhu committed Nov 19, 2024
1 parent e343232 commit ae69b4f
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions fuzzing/snmalloc-fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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 @@ -123,6 +140,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 @@ -139,7 +164,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 @@ -148,7 +173,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 @@ -157,7 +182,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

0 comments on commit ae69b4f

Please sign in to comment.