1 //===-- StringPool.h - Interned string pool -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Gordon Henriksen and is distributed under the
6 // University of Illinois Open Source 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/ADT/StringMap.h"
38 class PooledStringPtr;
40 /// StringPool - An interned string pool. Use the intern method to add a
41 /// string. Strings are removed automatically as PooledStringPtrs are
44 /// PooledString - This is the value of an entry in the pool's interning
47 StringPool *Pool; ///< So the string can remove itself.
48 unsigned Refcount; ///< Number of referencing PooledStringPtrs.
51 PooledString() : Pool(0), Refcount(0) { }
54 friend class PooledStringPtr;
56 typedef StringMap<PooledString> table_t;
57 typedef StringMapEntry<PooledString> entry_t;
64 /// intern - Adds a string to the pool and returns a reference-counted
65 /// pointer to it. No additional memory is allocated if the string already
66 /// exists in the pool.
67 PooledStringPtr intern(const char *Begin, const char *End);
69 /// intern - Adds a null-terminated string to the pool and returns a
70 /// reference-counted pointer to it. No additional memory is allocated if
71 /// the string already exists in the pool.
72 inline PooledStringPtr intern(const char *Str);
74 /// empty - Checks whether the pool is empty. Returns true if so.
76 inline bool empty() const { return InternTable.empty(); }
79 /// PooledStringPtr - A pointer to an interned string. Use operator bool to
80 /// test whether the pointer is valid, and operator * to get the string if so.
81 /// This is a lightweight value class with storage requirements equivalent to
82 /// a single pointer, but it does have reference-counting overhead when
84 class PooledStringPtr {
85 typedef StringPool::entry_t entry_t;
89 PooledStringPtr() : S(0) {}
91 explicit PooledStringPtr(entry_t *E) : S(E) {
92 if (S) ++S->getValue().Refcount;
95 PooledStringPtr(const PooledStringPtr &That) : S(That.S) {
96 if (S) ++S->getValue().Refcount;
99 PooledStringPtr &operator=(const PooledStringPtr &That) {
103 if (S) ++S->getValue().Refcount;
111 if (--S->getValue().Refcount == 0) {
112 S->getValue().Pool->InternTable.remove(S);
118 ~PooledStringPtr() { clear(); }
120 inline const char *begin() const {
121 assert(*this && "Attempt to dereference empty PooledStringPtr!");
122 return S->getKeyData();
125 inline const char *end() const {
126 assert(*this && "Attempt to dereference empty PooledStringPtr!");
127 return S->getKeyData() + S->getKeyLength();
130 inline unsigned size() const {
131 assert(*this && "Attempt to dereference empty PooledStringPtr!");
132 return S->getKeyLength();
135 inline const char *operator*() const { return begin(); }
136 inline operator bool() const { return S != 0; }
138 inline bool operator==(const PooledStringPtr &That) { return S == That.S; }
139 inline bool operator!=(const PooledStringPtr &That) { return S != That.S; }
142 PooledStringPtr StringPool::intern(const char *Str) {
143 return intern(Str, Str + strlen(Str));
146 } // End llvm namespace