LLVM API Documentation
00001 //===-- StringPool.h - Interned string pool ---------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares an interned string pool, which helps reduce the cost of 00011 // strings by using the same storage for identical strings. 00012 // 00013 // To intern a string: 00014 // 00015 // StringPool Pool; 00016 // PooledStringPtr Str = Pool.intern("wakka wakka"); 00017 // 00018 // To use the value of an interned string, use operator bool and operator*: 00019 // 00020 // if (Str) 00021 // cerr << "the string is" << *Str << "\n"; 00022 // 00023 // Pooled strings are immutable, but you can change a PooledStringPtr to point 00024 // to another instance. So that interned strings can eventually be freed, 00025 // strings in the string pool are reference-counted (automatically). 00026 // 00027 //===----------------------------------------------------------------------===// 00028 00029 #ifndef LLVM_SUPPORT_STRINGPOOL_H 00030 #define LLVM_SUPPORT_STRINGPOOL_H 00031 00032 #include "llvm/Support/Compiler.h" 00033 #include "llvm/ADT/StringMap.h" 00034 #include <cassert> 00035 #include <new> 00036 00037 namespace llvm { 00038 00039 class PooledStringPtr; 00040 00041 /// StringPool - An interned string pool. Use the intern method to add a 00042 /// string. Strings are removed automatically as PooledStringPtrs are 00043 /// destroyed. 00044 class StringPool { 00045 /// PooledString - This is the value of an entry in the pool's interning 00046 /// table. 00047 struct PooledString { 00048 StringPool *Pool; ///< So the string can remove itself. 00049 unsigned Refcount; ///< Number of referencing PooledStringPtrs. 00050 00051 public: 00052 PooledString() : Pool(nullptr), Refcount(0) { } 00053 }; 00054 00055 friend class PooledStringPtr; 00056 00057 typedef StringMap<PooledString> table_t; 00058 typedef StringMapEntry<PooledString> entry_t; 00059 table_t InternTable; 00060 00061 public: 00062 StringPool(); 00063 ~StringPool(); 00064 00065 /// intern - Adds a string to the pool and returns a reference-counted 00066 /// pointer to it. No additional memory is allocated if the string already 00067 /// exists in the pool. 00068 PooledStringPtr intern(StringRef Str); 00069 00070 /// empty - Checks whether the pool is empty. Returns true if so. 00071 /// 00072 inline bool empty() const { return InternTable.empty(); } 00073 }; 00074 00075 /// PooledStringPtr - A pointer to an interned string. Use operator bool to 00076 /// test whether the pointer is valid, and operator * to get the string if so. 00077 /// This is a lightweight value class with storage requirements equivalent to 00078 /// a single pointer, but it does have reference-counting overhead when 00079 /// copied. 00080 class PooledStringPtr { 00081 typedef StringPool::entry_t entry_t; 00082 entry_t *S; 00083 00084 public: 00085 PooledStringPtr() : S(nullptr) {} 00086 00087 explicit PooledStringPtr(entry_t *E) : S(E) { 00088 if (S) ++S->getValue().Refcount; 00089 } 00090 00091 PooledStringPtr(const PooledStringPtr &That) : S(That.S) { 00092 if (S) ++S->getValue().Refcount; 00093 } 00094 00095 PooledStringPtr &operator=(const PooledStringPtr &That) { 00096 if (S != That.S) { 00097 clear(); 00098 S = That.S; 00099 if (S) ++S->getValue().Refcount; 00100 } 00101 return *this; 00102 } 00103 00104 void clear() { 00105 if (!S) 00106 return; 00107 if (--S->getValue().Refcount == 0) { 00108 S->getValue().Pool->InternTable.remove(S); 00109 S->Destroy(); 00110 } 00111 S = nullptr; 00112 } 00113 00114 ~PooledStringPtr() { clear(); } 00115 00116 inline const char *begin() const { 00117 assert(*this && "Attempt to dereference empty PooledStringPtr!"); 00118 return S->getKeyData(); 00119 } 00120 00121 inline const char *end() const { 00122 assert(*this && "Attempt to dereference empty PooledStringPtr!"); 00123 return S->getKeyData() + S->getKeyLength(); 00124 } 00125 00126 inline unsigned size() const { 00127 assert(*this && "Attempt to dereference empty PooledStringPtr!"); 00128 return S->getKeyLength(); 00129 } 00130 00131 inline const char *operator*() const { return begin(); } 00132 inline LLVM_EXPLICIT operator bool() const { return S != nullptr; } 00133 00134 inline bool operator==(const PooledStringPtr &That) const { return S == That.S; } 00135 inline bool operator!=(const PooledStringPtr &That) const { return S != That.S; } 00136 }; 00137 00138 } // End llvm namespace 00139 00140 #endif