1 //===- SymbolStringPool.h - Multi-threaded pool for JIT symbols -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Contains a multi-threaded string pool suitable for use with ORC.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
15 #define LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H
17 #include "llvm/ADT/StringMap.h"
24 class SymbolStringPtr;
26 /// String pool for symbol names used by the JIT.
27 class SymbolStringPool {
28 friend class SymbolStringPtr;
30 /// Destroy a SymbolStringPool.
33 /// Create a symbol string pointer from the given string.
34 SymbolStringPtr intern(StringRef S);
36 /// Remove from the pool any entries that are no longer referenced.
37 void clearDeadEntries();
39 /// Returns true if the pool is empty.
42 using RefCountType = std::atomic<size_t>;
43 using PoolMap = StringMap<RefCountType>;
44 using PoolMapEntry = StringMapEntry<RefCountType>;
45 mutable std::mutex PoolMutex;
49 /// Pointer to a pooled string representing a symbol name.
50 class SymbolStringPtr {
51 friend class SymbolStringPool;
52 friend bool operator==(const SymbolStringPtr &LHS,
53 const SymbolStringPtr &RHS);
54 friend bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS);
57 SymbolStringPtr() = default;
58 SymbolStringPtr(const SymbolStringPtr &Other)
64 SymbolStringPtr& operator=(const SymbolStringPtr &Other) {
73 SymbolStringPtr(SymbolStringPtr &&Other) : S(nullptr) {
74 std::swap(S, Other.S);
77 SymbolStringPtr& operator=(SymbolStringPtr &&Other) {
81 std::swap(S, Other.S);
90 StringRef operator*() const { return S->first(); }
94 SymbolStringPtr(SymbolStringPool::PoolMapEntry *S)
100 SymbolStringPool::PoolMapEntry *S = nullptr;
103 inline bool operator==(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
104 return LHS.S == RHS.S;
107 inline bool operator!=(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
108 return !(LHS == RHS);
111 inline bool operator<(const SymbolStringPtr &LHS, const SymbolStringPtr &RHS) {
112 return LHS.S < RHS.S;
115 inline SymbolStringPool::~SymbolStringPool() {
118 assert(Pool.empty() && "Dangling references at pool destruction time");
122 inline SymbolStringPtr SymbolStringPool::intern(StringRef S) {
123 std::lock_guard<std::mutex> Lock(PoolMutex);
126 std::tie(I, Added) = Pool.try_emplace(S, 0);
127 return SymbolStringPtr(&*I);
130 inline void SymbolStringPool::clearDeadEntries() {
131 std::lock_guard<std::mutex> Lock(PoolMutex);
132 for (auto I = Pool.begin(), E = Pool.end(); I != E;) {
134 if (Tmp->second == 0)
139 inline bool SymbolStringPool::empty() const {
140 std::lock_guard<std::mutex> Lock(PoolMutex);
144 } // end namespace orc
145 } // end namespace llvm
147 #endif // LLVM_EXECUTIONENGINE_ORC_SYMBOLSTRINGPOOL_H