From 2eb461b8ea235104ec5c40bd4e6ecf7d337c835b Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Tue, 11 Jun 2024 11:38:55 -0700 Subject: [PATCH] Fix build for llvm 18 --- ConflictSet.cpp | 4 ---- Internal.h | 63 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/ConflictSet.cpp b/ConflictSet.cpp index e035c78..168561b 100644 --- a/ConflictSet.cpp +++ b/ConflictSet.cpp @@ -2944,10 +2944,6 @@ Iterator firstGeq(Node *n, std::string_view key) { } // namespace -namespace std { -void __throw_length_error(const char *) { __builtin_unreachable(); } -} // namespace std - #if SHOW_MEMORY int64_t nodeBytes = 0; diff --git a/Internal.h b/Internal.h index 8c5d1cd..23e3013 100644 --- a/Internal.h +++ b/Internal.h @@ -255,10 +255,65 @@ template struct ArenaAlloc { void deallocate(T *, size_t) noexcept {} }; -template using Vector = std::vector>; -template auto vector(Arena &arena) { - return Vector(ArenaAlloc(&arena)); -} +template struct Vector { + static_assert(std::is_trivially_destructible_v); + static_assert(std::is_trivially_copyable_v); + + explicit Vector(Arena *arena) + : arena(arena), t(nullptr), size_(0), capacity(0) {} + + void append(std::span slice) { + if (size_ + int(slice.size()) > capacity) { + grow(std::max(size_ + slice.size(), capacity * 2)); + } + if (slice.size() > 0) { + memcpy(const_cast *>(t) + size_, slice.data(), + slice.size() * sizeof(T)); + } + size_ += slice.size(); + } + + void push_back(const T &t) { append(std::span(&t, 1)); } + + T *begin() { return t; } + T *end() { return t + size_; } + T *data() { return t; } + T &back() { + assert(size_ > 0); + return t[size_ - 1]; + } + T &operator[](int i) { + assert(i >= 0 && i < size_); + return t[i]; + } + + void pop_back() { + assert(size_ > 0); + --size_; + } + + int size() const { return size_; } + + operator std::span() const { return std::span(t, size_); } + +private: + void grow(int newCapacity) { + capacity = newCapacity; + auto old = std::span(*this); + t = (T *)new (std::align_val_t(alignof(T)), *arena) + uint8_t[capacity * sizeof(T)]; + size_ = 0; + append(old); + } + + Arena *arena; + T *t; + int size_; + int capacity; +}; + +template auto vector(Arena &arena) { return Vector(&arena); } + template using Set = std::set>; template > auto set(Arena &arena) { return Set(ArenaAlloc(&arena));