| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "absl/base/attributes.h" |
| |
| #include "absl/base/internal/low_level_alloc.h" |
| #ifndef ABSL_LOW_LEVEL_ALLOC_MISSING |
|
|
| #include "absl/synchronization/internal/graphcycles.h" |
|
|
| #include <algorithm> |
| #include <array> |
| #include <cinttypes> |
| #include <limits> |
| #include "absl/base/internal/hide_ptr.h" |
| #include "absl/base/internal/raw_logging.h" |
| #include "absl/base/internal/spinlock.h" |
|
|
| |
|
|
| namespace absl { |
| ABSL_NAMESPACE_BEGIN |
| namespace synchronization_internal { |
|
|
| namespace { |
|
|
| |
| |
| ABSL_CONST_INIT static absl::base_internal::SpinLock arena_mu( |
| absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY); |
| ABSL_CONST_INIT static base_internal::LowLevelAlloc::Arena* arena; |
|
|
| static void InitArenaIfNecessary() { |
| arena_mu.Lock(); |
| if (arena == nullptr) { |
| arena = base_internal::LowLevelAlloc::NewArena(0); |
| } |
| arena_mu.Unlock(); |
| } |
|
|
| |
| |
| static const uint32_t kInline = 8; |
|
|
| |
| |
| |
| template <typename T> |
| class Vec { |
| public: |
| Vec() { Init(); } |
| ~Vec() { Discard(); } |
|
|
| void clear() { |
| Discard(); |
| Init(); |
| } |
|
|
| bool empty() const { return size_ == 0; } |
| uint32_t size() const { return size_; } |
| T* begin() { return ptr_; } |
| T* end() { return ptr_ + size_; } |
| const T& operator[](uint32_t i) const { return ptr_[i]; } |
| T& operator[](uint32_t i) { return ptr_[i]; } |
| const T& back() const { return ptr_[size_-1]; } |
| void pop_back() { size_--; } |
|
|
| void push_back(const T& v) { |
| if (size_ == capacity_) Grow(size_ + 1); |
| ptr_[size_] = v; |
| size_++; |
| } |
|
|
| void resize(uint32_t n) { |
| if (n > capacity_) Grow(n); |
| size_ = n; |
| } |
|
|
| void fill(const T& val) { |
| for (uint32_t i = 0; i < size(); i++) { |
| ptr_[i] = val; |
| } |
| } |
|
|
| |
| |
| void MoveFrom(Vec<T>* src) { |
| if (src->ptr_ == src->space_) { |
| |
| resize(src->size_); |
| std::copy_n(src->ptr_, src->size_, ptr_); |
| src->size_ = 0; |
| } else { |
| Discard(); |
| ptr_ = src->ptr_; |
| size_ = src->size_; |
| capacity_ = src->capacity_; |
| src->Init(); |
| } |
| } |
|
|
| private: |
| T* ptr_; |
| T space_[kInline]; |
| uint32_t size_; |
| uint32_t capacity_; |
|
|
| void Init() { |
| ptr_ = space_; |
| size_ = 0; |
| capacity_ = kInline; |
| } |
|
|
| void Discard() { |
| if (ptr_ != space_) base_internal::LowLevelAlloc::Free(ptr_); |
| } |
|
|
| void Grow(uint32_t n) { |
| while (capacity_ < n) { |
| capacity_ *= 2; |
| } |
| size_t request = static_cast<size_t>(capacity_) * sizeof(T); |
| T* copy = static_cast<T*>( |
| base_internal::LowLevelAlloc::AllocWithArena(request, arena)); |
| std::copy_n(ptr_, size_, copy); |
| Discard(); |
| ptr_ = copy; |
| } |
|
|
| Vec(const Vec&) = delete; |
| Vec& operator=(const Vec&) = delete; |
| }; |
|
|
| |
| class NodeSet { |
| public: |
| NodeSet() { Init(); } |
|
|
| void clear() { Init(); } |
| bool contains(int32_t v) const { return table_[FindIndex(v)] == v; } |
|
|
| bool insert(int32_t v) { |
| uint32_t i = FindIndex(v); |
| if (table_[i] == v) { |
| return false; |
| } |
| if (table_[i] == kEmpty) { |
| |
| |
| occupied_++; |
| } |
| table_[i] = v; |
| |
| if (occupied_ >= table_.size() - table_.size()/4) Grow(); |
| return true; |
| } |
|
|
| void erase(int32_t v) { |
| uint32_t i = FindIndex(v); |
| if (table_[i] == v) { |
| table_[i] = kDel; |
| } |
| } |
|
|
| |
| |
| |
| #define HASH_FOR_EACH(elem, eset) \ |
| for (int32_t elem, _cursor = 0; (eset).Next(&_cursor, &elem); ) |
| bool Next(int32_t* cursor, int32_t* elem) { |
| while (static_cast<uint32_t>(*cursor) < table_.size()) { |
| int32_t v = table_[static_cast<uint32_t>(*cursor)]; |
| (*cursor)++; |
| if (v >= 0) { |
| *elem = v; |
| return true; |
| } |
| } |
| return false; |
| } |
|
|
| private: |
| enum : int32_t { kEmpty = -1, kDel = -2 }; |
| Vec<int32_t> table_; |
| uint32_t occupied_; |
|
|
| static uint32_t Hash(int32_t a) { return static_cast<uint32_t>(a * 41); } |
|
|
| |
| uint32_t FindIndex(int32_t v) const { |
| |
| const uint32_t mask = table_.size() - 1; |
| uint32_t i = Hash(v) & mask; |
| uint32_t deleted_index = 0; |
| bool seen_deleted_element = false; |
| while (true) { |
| int32_t e = table_[i]; |
| if (v == e) { |
| return i; |
| } else if (e == kEmpty) { |
| |
| return seen_deleted_element ? deleted_index : i; |
| } else if (e == kDel && !seen_deleted_element) { |
| |
| deleted_index = i; |
| seen_deleted_element = true; |
| } |
| i = (i + 1) & mask; |
| } |
| } |
|
|
| void Init() { |
| table_.clear(); |
| table_.resize(kInline); |
| table_.fill(kEmpty); |
| occupied_ = 0; |
| } |
|
|
| void Grow() { |
| Vec<int32_t> copy; |
| copy.MoveFrom(&table_); |
| occupied_ = 0; |
| table_.resize(copy.size() * 2); |
| table_.fill(kEmpty); |
|
|
| for (const auto& e : copy) { |
| if (e >= 0) insert(e); |
| } |
| } |
|
|
| NodeSet(const NodeSet&) = delete; |
| NodeSet& operator=(const NodeSet&) = delete; |
| }; |
|
|
| |
| |
| |
|
|
| inline GraphId MakeId(int32_t index, uint32_t version) { |
| GraphId g; |
| g.handle = |
| (static_cast<uint64_t>(version) << 32) | static_cast<uint32_t>(index); |
| return g; |
| } |
|
|
| inline int32_t NodeIndex(GraphId id) { |
| return static_cast<int32_t>(id.handle); |
| } |
|
|
| inline uint32_t NodeVersion(GraphId id) { |
| return static_cast<uint32_t>(id.handle >> 32); |
| } |
|
|
| struct Node { |
| int32_t rank; |
| uint32_t version; |
| int32_t next_hash; |
| bool visited; |
| uintptr_t masked_ptr; |
| NodeSet in; |
| NodeSet out; |
| int priority; |
| int nstack; |
| void* stack[40]; |
| }; |
|
|
| |
| class PointerMap { |
| public: |
| explicit PointerMap(const Vec<Node*>* nodes) : nodes_(nodes) { |
| table_.fill(-1); |
| } |
|
|
| int32_t Find(void* ptr) { |
| auto masked = base_internal::HidePtr(ptr); |
| for (int32_t i = table_[Hash(ptr)]; i != -1;) { |
| Node* n = (*nodes_)[static_cast<uint32_t>(i)]; |
| if (n->masked_ptr == masked) return i; |
| i = n->next_hash; |
| } |
| return -1; |
| } |
|
|
| void Add(void* ptr, int32_t i) { |
| int32_t* head = &table_[Hash(ptr)]; |
| (*nodes_)[static_cast<uint32_t>(i)]->next_hash = *head; |
| *head = i; |
| } |
|
|
| int32_t Remove(void* ptr) { |
| |
| |
| auto masked = base_internal::HidePtr(ptr); |
| for (int32_t* slot = &table_[Hash(ptr)]; *slot != -1; ) { |
| int32_t index = *slot; |
| Node* n = (*nodes_)[static_cast<uint32_t>(index)]; |
| if (n->masked_ptr == masked) { |
| *slot = n->next_hash; |
| n->next_hash = -1; |
| return index; |
| } |
| slot = &n->next_hash; |
| } |
| return -1; |
| } |
|
|
| private: |
| |
| static constexpr uint32_t kHashTableSize = 8171; |
|
|
| const Vec<Node*>* nodes_; |
| std::array<int32_t, kHashTableSize> table_; |
|
|
| static uint32_t Hash(void* ptr) { |
| return reinterpret_cast<uintptr_t>(ptr) % kHashTableSize; |
| } |
| }; |
|
|
| } |
|
|
| struct GraphCycles::Rep { |
| Vec<Node*> nodes_; |
| Vec<int32_t> free_nodes_; |
| PointerMap ptrmap_; |
|
|
| |
| Vec<int32_t> deltaf_; |
| Vec<int32_t> deltab_; |
| Vec<int32_t> list_; |
| Vec<int32_t> merged_; |
| Vec<int32_t> stack_; |
|
|
| Rep() : ptrmap_(&nodes_) {} |
| }; |
|
|
| static Node* FindNode(GraphCycles::Rep* rep, GraphId id) { |
| Node* n = rep->nodes_[static_cast<uint32_t>(NodeIndex(id))]; |
| return (n->version == NodeVersion(id)) ? n : nullptr; |
| } |
|
|
| GraphCycles::GraphCycles() { |
| InitArenaIfNecessary(); |
| rep_ = new (base_internal::LowLevelAlloc::AllocWithArena(sizeof(Rep), arena)) |
| Rep; |
| } |
|
|
| GraphCycles::~GraphCycles() { |
| for (auto* node : rep_->nodes_) { |
| node->Node::~Node(); |
| base_internal::LowLevelAlloc::Free(node); |
| } |
| rep_->Rep::~Rep(); |
| base_internal::LowLevelAlloc::Free(rep_); |
| } |
|
|
| bool GraphCycles::CheckInvariants() const { |
| Rep* r = rep_; |
| NodeSet ranks; |
| for (uint32_t x = 0; x < r->nodes_.size(); x++) { |
| Node* nx = r->nodes_[x]; |
| void* ptr = base_internal::UnhidePtr<void>(nx->masked_ptr); |
| if (ptr != nullptr && static_cast<uint32_t>(r->ptrmap_.Find(ptr)) != x) { |
| ABSL_RAW_LOG(FATAL, "Did not find live node in hash table %" PRIu32 " %p", |
| x, ptr); |
| } |
| if (nx->visited) { |
| ABSL_RAW_LOG(FATAL, "Did not clear visited marker on node %" PRIu32, x); |
| } |
| if (!ranks.insert(nx->rank)) { |
| ABSL_RAW_LOG(FATAL, "Duplicate occurrence of rank %" PRId32, nx->rank); |
| } |
| HASH_FOR_EACH(y, nx->out) { |
| Node* ny = r->nodes_[static_cast<uint32_t>(y)]; |
| if (nx->rank >= ny->rank) { |
| ABSL_RAW_LOG(FATAL, |
| "Edge %" PRIu32 " ->%" PRId32 |
| " has bad rank assignment %" PRId32 "->%" PRId32, |
| x, y, nx->rank, ny->rank); |
| } |
| } |
| } |
| return true; |
| } |
|
|
| GraphId GraphCycles::GetId(void* ptr) { |
| int32_t i = rep_->ptrmap_.Find(ptr); |
| if (i != -1) { |
| return MakeId(i, rep_->nodes_[static_cast<uint32_t>(i)]->version); |
| } else if (rep_->free_nodes_.empty()) { |
| Node* n = |
| new (base_internal::LowLevelAlloc::AllocWithArena(sizeof(Node), arena)) |
| Node; |
| n->version = 1; |
| n->visited = false; |
| n->rank = static_cast<int32_t>(rep_->nodes_.size()); |
| n->masked_ptr = base_internal::HidePtr(ptr); |
| n->nstack = 0; |
| n->priority = 0; |
| rep_->nodes_.push_back(n); |
| rep_->ptrmap_.Add(ptr, n->rank); |
| return MakeId(n->rank, n->version); |
| } else { |
| |
| |
| int32_t r = rep_->free_nodes_.back(); |
| rep_->free_nodes_.pop_back(); |
| Node* n = rep_->nodes_[static_cast<uint32_t>(r)]; |
| n->masked_ptr = base_internal::HidePtr(ptr); |
| n->nstack = 0; |
| n->priority = 0; |
| rep_->ptrmap_.Add(ptr, r); |
| return MakeId(r, n->version); |
| } |
| } |
|
|
| void GraphCycles::RemoveNode(void* ptr) { |
| int32_t i = rep_->ptrmap_.Remove(ptr); |
| if (i == -1) { |
| return; |
| } |
| Node* x = rep_->nodes_[static_cast<uint32_t>(i)]; |
| HASH_FOR_EACH(y, x->out) { |
| rep_->nodes_[static_cast<uint32_t>(y)]->in.erase(i); |
| } |
| HASH_FOR_EACH(y, x->in) { |
| rep_->nodes_[static_cast<uint32_t>(y)]->out.erase(i); |
| } |
| x->in.clear(); |
| x->out.clear(); |
| x->masked_ptr = base_internal::HidePtr<void>(nullptr); |
| if (x->version == std::numeric_limits<uint32_t>::max()) { |
| |
| } else { |
| x->version++; |
| rep_->free_nodes_.push_back(i); |
| } |
| } |
|
|
| void* GraphCycles::Ptr(GraphId id) { |
| Node* n = FindNode(rep_, id); |
| return n == nullptr ? nullptr |
| : base_internal::UnhidePtr<void>(n->masked_ptr); |
| } |
|
|
| bool GraphCycles::HasNode(GraphId node) { |
| return FindNode(rep_, node) != nullptr; |
| } |
|
|
| bool GraphCycles::HasEdge(GraphId x, GraphId y) const { |
| Node* xn = FindNode(rep_, x); |
| return xn && FindNode(rep_, y) && xn->out.contains(NodeIndex(y)); |
| } |
|
|
| void GraphCycles::RemoveEdge(GraphId x, GraphId y) { |
| Node* xn = FindNode(rep_, x); |
| Node* yn = FindNode(rep_, y); |
| if (xn && yn) { |
| xn->out.erase(NodeIndex(y)); |
| yn->in.erase(NodeIndex(x)); |
| |
| |
| } |
| } |
|
|
| static bool ForwardDFS(GraphCycles::Rep* r, int32_t n, int32_t upper_bound); |
| static void BackwardDFS(GraphCycles::Rep* r, int32_t n, int32_t lower_bound); |
| static void Reorder(GraphCycles::Rep* r); |
| static void Sort(const Vec<Node*>&, Vec<int32_t>* delta); |
| static void MoveToList( |
| GraphCycles::Rep* r, Vec<int32_t>* src, Vec<int32_t>* dst); |
|
|
| bool GraphCycles::InsertEdge(GraphId idx, GraphId idy) { |
| Rep* r = rep_; |
| const int32_t x = NodeIndex(idx); |
| const int32_t y = NodeIndex(idy); |
| Node* nx = FindNode(r, idx); |
| Node* ny = FindNode(r, idy); |
| if (nx == nullptr || ny == nullptr) return true; |
|
|
| if (nx == ny) return false; |
| if (!nx->out.insert(y)) { |
| |
| return true; |
| } |
|
|
| ny->in.insert(x); |
|
|
| if (nx->rank <= ny->rank) { |
| |
| return true; |
| } |
|
|
| |
| |
| if (!ForwardDFS(r, y, nx->rank)) { |
| |
| nx->out.erase(y); |
| ny->in.erase(x); |
| |
| |
| for (const auto& d : r->deltaf_) { |
| r->nodes_[static_cast<uint32_t>(d)]->visited = false; |
| } |
| return false; |
| } |
| BackwardDFS(r, x, ny->rank); |
| Reorder(r); |
| return true; |
| } |
|
|
| static bool ForwardDFS(GraphCycles::Rep* r, int32_t n, int32_t upper_bound) { |
| |
| |
| r->deltaf_.clear(); |
| r->stack_.clear(); |
| r->stack_.push_back(n); |
| while (!r->stack_.empty()) { |
| n = r->stack_.back(); |
| r->stack_.pop_back(); |
| Node* nn = r->nodes_[static_cast<uint32_t>(n)]; |
| if (nn->visited) continue; |
|
|
| nn->visited = true; |
| r->deltaf_.push_back(n); |
|
|
| HASH_FOR_EACH(w, nn->out) { |
| Node* nw = r->nodes_[static_cast<uint32_t>(w)]; |
| if (nw->rank == upper_bound) { |
| return false; |
| } |
| if (!nw->visited && nw->rank < upper_bound) { |
| r->stack_.push_back(w); |
| } |
| } |
| } |
| return true; |
| } |
|
|
| static void BackwardDFS(GraphCycles::Rep* r, int32_t n, int32_t lower_bound) { |
| r->deltab_.clear(); |
| r->stack_.clear(); |
| r->stack_.push_back(n); |
| while (!r->stack_.empty()) { |
| n = r->stack_.back(); |
| r->stack_.pop_back(); |
| Node* nn = r->nodes_[static_cast<uint32_t>(n)]; |
| if (nn->visited) continue; |
|
|
| nn->visited = true; |
| r->deltab_.push_back(n); |
|
|
| HASH_FOR_EACH(w, nn->in) { |
| Node* nw = r->nodes_[static_cast<uint32_t>(w)]; |
| if (!nw->visited && lower_bound < nw->rank) { |
| r->stack_.push_back(w); |
| } |
| } |
| } |
| } |
|
|
| static void Reorder(GraphCycles::Rep* r) { |
| Sort(r->nodes_, &r->deltab_); |
| Sort(r->nodes_, &r->deltaf_); |
|
|
| |
| r->list_.clear(); |
| MoveToList(r, &r->deltab_, &r->list_); |
| MoveToList(r, &r->deltaf_, &r->list_); |
|
|
| |
| r->merged_.resize(r->deltab_.size() + r->deltaf_.size()); |
| std::merge(r->deltab_.begin(), r->deltab_.end(), |
| r->deltaf_.begin(), r->deltaf_.end(), |
| r->merged_.begin()); |
|
|
| |
| for (uint32_t i = 0; i < r->list_.size(); i++) { |
| r->nodes_[static_cast<uint32_t>(r->list_[i])]->rank = r->merged_[i]; |
| } |
| } |
|
|
| static void Sort(const Vec<Node*>& nodes, Vec<int32_t>* delta) { |
| struct ByRank { |
| const Vec<Node*>* nodes; |
| bool operator()(int32_t a, int32_t b) const { |
| return (*nodes)[static_cast<uint32_t>(a)]->rank < |
| (*nodes)[static_cast<uint32_t>(b)]->rank; |
| } |
| }; |
| ByRank cmp; |
| cmp.nodes = &nodes; |
| std::sort(delta->begin(), delta->end(), cmp); |
| } |
|
|
| static void MoveToList( |
| GraphCycles::Rep* r, Vec<int32_t>* src, Vec<int32_t>* dst) { |
| for (auto& v : *src) { |
| int32_t w = v; |
| |
| v = r->nodes_[static_cast<uint32_t>(w)]->rank; |
| |
| r->nodes_[static_cast<uint32_t>(w)]->visited = false; |
| dst->push_back(w); |
| } |
| } |
|
|
| int GraphCycles::FindPath(GraphId idx, GraphId idy, int max_path_len, |
| GraphId path[]) const { |
| Rep* r = rep_; |
| if (FindNode(r, idx) == nullptr || FindNode(r, idy) == nullptr) return 0; |
| const int32_t x = NodeIndex(idx); |
| const int32_t y = NodeIndex(idy); |
|
|
| |
| |
| |
| int path_len = 0; |
|
|
| NodeSet seen; |
| r->stack_.clear(); |
| r->stack_.push_back(x); |
| while (!r->stack_.empty()) { |
| int32_t n = r->stack_.back(); |
| r->stack_.pop_back(); |
| if (n < 0) { |
| |
| path_len--; |
| continue; |
| } |
|
|
| if (path_len < max_path_len) { |
| path[path_len] = |
| MakeId(n, rep_->nodes_[static_cast<uint32_t>(n)]->version); |
| } |
| path_len++; |
| r->stack_.push_back(-1); |
|
|
| if (n == y) { |
| return path_len; |
| } |
|
|
| HASH_FOR_EACH(w, r->nodes_[static_cast<uint32_t>(n)]->out) { |
| if (seen.insert(w)) { |
| r->stack_.push_back(w); |
| } |
| } |
| } |
|
|
| return 0; |
| } |
|
|
| bool GraphCycles::IsReachable(GraphId x, GraphId y) const { |
| return FindPath(x, y, 0, nullptr) > 0; |
| } |
|
|
| void GraphCycles::UpdateStackTrace(GraphId id, int priority, |
| int (*get_stack_trace)(void** stack, int)) { |
| Node* n = FindNode(rep_, id); |
| if (n == nullptr || n->priority >= priority) { |
| return; |
| } |
| n->nstack = (*get_stack_trace)(n->stack, ABSL_ARRAYSIZE(n->stack)); |
| n->priority = priority; |
| } |
|
|
| int GraphCycles::GetStackTrace(GraphId id, void*** ptr) { |
| Node* n = FindNode(rep_, id); |
| if (n == nullptr) { |
| *ptr = nullptr; |
| return 0; |
| } else { |
| *ptr = n->stack; |
| return n->nstack; |
| } |
| } |
|
|
| } |
| ABSL_NAMESPACE_END |
| } |
|
|
| #endif |
|
|