diff options
Diffstat (limited to 'include/llvm/ExecutionEngine/Orc/SymbolStringPool.h')
-rw-r--r-- | include/llvm/ExecutionEngine/Orc/SymbolStringPool.h | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/include/llvm/ExecutionEngine/Orc/SymbolStringPool.h b/include/llvm/ExecutionEngine/Orc/SymbolStringPool.h index b01fbd44bacd..4c45cfd199dd 100644 --- a/include/llvm/ExecutionEngine/Orc/SymbolStringPool.h +++ b/include/llvm/ExecutionEngine/Orc/SymbolStringPool.h @@ -23,29 +23,36 @@ namespace orc { class SymbolStringPtr; -/// @brief String pool for symbol names used by the JIT. +/// String pool for symbol names used by the JIT. class SymbolStringPool { friend class SymbolStringPtr; public: - /// @brief Create a symbol string pointer from the given string. + /// Destroy a SymbolStringPool. + ~SymbolStringPool(); + + /// Create a symbol string pointer from the given string. SymbolStringPtr intern(StringRef S); - /// @brief Remove from the pool any entries that are no longer referenced. + /// Remove from the pool any entries that are no longer referenced. void clearDeadEntries(); - /// @brief Returns true if the pool is empty. + /// Returns true if the pool is empty. bool empty() const; private: - using RefCountType = std::atomic<uint64_t>; + using RefCountType = std::atomic<size_t>; using PoolMap = StringMap<RefCountType>; using PoolMapEntry = StringMapEntry<RefCountType>; mutable std::mutex PoolMutex; PoolMap Pool; }; -/// @brief Pointer to a pooled string representing a symbol name. +/// Pointer to a pooled string representing a symbol name. class SymbolStringPtr { friend class SymbolStringPool; + friend bool operator==(const SymbolStringPtr &LHS, + const SymbolStringPtr &RHS); + friend bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS); + public: SymbolStringPtr() = default; SymbolStringPtr(const SymbolStringPtr &Other) @@ -80,17 +87,7 @@ public: --S->getValue(); } - bool operator==(const SymbolStringPtr &Other) const { - return S == Other.S; - } - - bool operator!=(const SymbolStringPtr &Other) const { - return !(*this == Other); - } - - bool operator<(const SymbolStringPtr &Other) const { - return S->getValue() < Other.S->getValue(); - } + StringRef operator*() const { return S->first(); } private: @@ -103,25 +100,39 @@ private: SymbolStringPool::PoolMapEntry *S = nullptr; }; +inline bool operator==(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) { + return LHS.S == RHS.S; +} + +inline bool operator!=(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) { + return !(LHS == RHS); +} + +inline bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) { + return LHS.S < RHS.S; +} + +inline SymbolStringPool::~SymbolStringPool() { +#ifndef NDEBUG + clearDeadEntries(); + assert(Pool.empty() && "Dangling references at pool destruction time"); +#endif // NDEBUG +} + inline SymbolStringPtr SymbolStringPool::intern(StringRef S) { std::lock_guard<std::mutex> Lock(PoolMutex); - auto I = Pool.find(S); - if (I != Pool.end()) - return SymbolStringPtr(&*I); - + PoolMap::iterator I; bool Added; std::tie(I, Added) = Pool.try_emplace(S, 0); - assert(Added && "Insert should always succeed here"); return SymbolStringPtr(&*I); } inline void SymbolStringPool::clearDeadEntries() { std::lock_guard<std::mutex> Lock(PoolMutex); for (auto I = Pool.begin(), E = Pool.end(); I != E;) { - auto Tmp = std::next(I); - if (I->second == 0) - Pool.erase(I); - I = Tmp; + auto Tmp = I++; + if (Tmp->second == 0) + Pool.erase(Tmp); } } |