Tidy up std::{set,vector} + arena

This commit is contained in:
2024-01-22 13:22:52 -08:00
parent bcc9195cdb
commit 5456475db6

View File

@@ -162,6 +162,15 @@ template <class T> struct ArenaAlloc {
private: private:
}; };
template <class T> using Vector = std::vector<T, ArenaAlloc<T>>;
template <class T> auto vector(Arena &arena) {
return Vector<T>(ArenaAlloc<T>(&arena));
}
template <class T> using Set = std::set<T, std::less<T>, ArenaAlloc<T>>;
template <class T> auto set(Arena &arena) {
return Set<T>(ArenaAlloc<T>(&arena));
}
template <class T, class U> template <class T, class U>
bool operator==(const ArenaAlloc<T> &lhs, const ArenaAlloc<U> &rhs) { bool operator==(const ArenaAlloc<T> &lhs, const ArenaAlloc<U> &rhs) {
return lhs.arena == rhs.arena; return lhs.arena == rhs.arena;
@@ -843,8 +852,7 @@ bool checkCorrectness(Node *node, ReferenceImpl &refImpl) {
bool success = true; bool success = true;
// Check bst invariant // Check bst invariant
Arena arena; Arena arena;
std::vector<std::string_view, ArenaAlloc<std::string_view>> keys{ auto keys = vector<std::string_view>(arena);
ArenaAlloc<std::string_view>(&arena)};
for (auto iter = extrema(node, false); iter != nullptr; for (auto iter = extrema(node, false); iter != nullptr;
iter = next(iter, true)) { iter = next(iter, true)) {
keys.push_back(std::string_view((char *)(iter + 1), iter->len)); keys.push_back(std::string_view((char *)(iter + 1), iter->len));
@@ -998,8 +1006,7 @@ struct __attribute__((__visibility__("hidden"))) ConflictSet::Impl {
runInterleaved(stepwiseInserts); runInterleaved(stepwiseInserts);
std::vector<Node *, ArenaAlloc<Node *>> workList{ auto workList = vector<Node *>(arena);
ArenaAlloc<Node *>(&arena)};
workList.reserve(count); workList.reserve(count);
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
Node *node = *stepwiseInserts[i].current; Node *node = *stepwiseInserts[i].current;
@@ -1048,7 +1055,7 @@ struct __attribute__((__visibility__("hidden"))) ConflictSet::Impl {
~Impl() { ~Impl() {
Arena arena; Arena arena;
std::vector<Node *, ArenaAlloc<Node *>> toFree{ArenaAlloc<Node *>(&arena)}; auto toFree = vector<Node *>(arena);
if (root != nullptr) { if (root != nullptr) {
toFree.push_back(root); toFree.push_back(root);
} }
@@ -1240,9 +1247,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
int numWrites = gArbitrary.bounded(10); int numWrites = gArbitrary.bounded(10);
int64_t v = ++writeVersion; int64_t v = ++writeVersion;
auto *writes = new (arena) ConflictSet::WriteRange[numWrites]; auto *writes = new (arena) ConflictSet::WriteRange[numWrites];
std::set<std::string_view, std::less<std::string_view>, auto keys = set<std::string_view>(arena);
ArenaAlloc<std::string_view>>
keys{ArenaAlloc<int>(&arena)};
while (int(keys.size()) < numWrites) { while (int(keys.size()) < numWrites) {
if (!gArbitrary.hasEntropy()) { if (!gArbitrary.hasEntropy()) {
// Tell the fuzzer it's not interesting // Tell the fuzzer it's not interesting
@@ -1271,9 +1276,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
int numReads = gArbitrary.bounded(10); int numReads = gArbitrary.bounded(10);
int64_t v = writeVersion - gArbitrary.bounded(10); int64_t v = writeVersion - gArbitrary.bounded(10);
auto *reads = new (arena) ConflictSet::ReadRange[numReads]; auto *reads = new (arena) ConflictSet::ReadRange[numReads];
std::set<std::string_view, std::less<std::string_view>, auto keys = set<std::string_view>(arena);
ArenaAlloc<std::string_view>>
keys{ArenaAlloc<int>(&arena)};
while (int(keys.size()) < numReads) { while (int(keys.size()) < numReads) {
if (!gArbitrary.hasEntropy()) { if (!gArbitrary.hasEntropy()) {
// Tell the fuzzer it's not interesting // Tell the fuzzer it's not interesting