1 //===-- StringPool.h - Interned string pool ---------------------*- 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 // This file declares an interned string pool, which helps reduce the cost of
11 // strings by using the same storage for identical strings.
13 // To intern a string:
16 // PooledStringPtr Str = Pool.intern("wakka wakka");
18 // To use the value of an interned string, use operator bool and operator*:
21 // cerr << "the string is" << *Str << "\n";
23 // Pooled strings are immutable, but you can change a PooledStringPtr to point
24 // to another instance. So that interned strings can eventually be freed,
25 // strings in the string pool are reference-counted (automatically).
27 //===----------------------------------------------------------------------===//
29 #ifndef LLVM_SUPPORT_STRINGPOOL_H
30 #define LLVM_SUPPORT_STRINGPOOL_H
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/ADT/StringMap.h"
39 class PooledStringPtr;
41 /// StringPool - An interned string pool. Use the intern method to add a
42 /// string. Strings are removed automatically as PooledStringPtrs are
45 /// PooledString - This is the value of an entry in the pool's interning
48 StringPool *Pool; ///< So the string can remove itself.
49 unsigned Refcount; ///< Number of referencing PooledStringPtrs.
52 PooledString() : Pool(nullptr), Refcount(0) { }
55 friend class PooledStringPtr;
57 typedef StringMap<PooledString> table_t;
58 typedef StringMapEntry<PooledString> entry_t;
65 /// intern - Adds a string to the pool and returns a reference-counted
66 /// pointer to it. No additional memory is allocated if the string already
67 /// exists in the pool.
68 PooledStringPtr intern(StringRef Str);
70 /// empty - Checks whether the pool is empty. Returns true if so.
72 inline bool empty() const { return InternTable.empty(); }
75 /// PooledStringPtr - A pointer to an interned string. Use operator bool to
76 /// test whether the pointer is valid, and operator * to get the string if so.
77 /// This is a lightweight value class with storage requirements equivalent to
78 /// a single pointer, but it does have reference-counting overhead when
80 class PooledStringPtr {
81 typedef StringPool::entry_t entry_t;
85 PooledStringPtr() : S(nullptr) {}
87 explicit PooledStringPtr(entry_t *E) : S(E) {
88 if (S) ++S->getValue().Refcount;
91 PooledStringPtr(const PooledStringPtr &That) : S(That.S) {
92 if (S) ++S->getValue().Refcount;
95 PooledStringPtr &operator=(const PooledStringPtr &That) {
99 if (S) ++S->getValue().Refcount;
107 if (--S->getValue().Refcount == 0) {
108 S->getValue().Pool->InternTable.remove(S);
114 ~PooledStringPtr() { clear(); }
116 inline const char *begin() const {
117 assert(*this && "Attempt to dereference empty PooledStringPtr!");
118 return S->getKeyData();
121 inline const char *end() const {
122 assert(*this && "Attempt to dereference empty PooledStringPtr!");
123 return S->getKeyData() + S->getKeyLength();
126 inline unsigned size() const {
127 assert(*this && "Attempt to dereference empty PooledStringPtr!");
128 return S->getKeyLength();
131 inline const char *operator*() const { return begin(); }
132 inline LLVM_EXPLICIT operator bool() const { return S != nullptr; }
134 inline bool operator==(const PooledStringPtr &That) const { return S == That.S; }
135 inline bool operator!=(const PooledStringPtr &That) const { return S != That.S; }
138 } // End llvm namespace