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
45 StringPool *Pool; ///< So the string can remove itself.
46 unsigned Refcount; ///< Number of referencing PooledStringPtrs.
49 PooledString() : Pool(0), Refcount(0) { }
52 friend class PooledStringPtr;
54 typedef StringMap<PooledString> table_t;
55 typedef StringMapEntry<PooledString> entry_t;
62 PooledStringPtr intern(const char *Begin, const char *End);
63 inline PooledStringPtr intern(const char *Str);
66 /// PooledStringPtr - A pointer to an interned string. Use operator bool to
67 /// test whether the pointer is valid, and operator * to get the string if so.
68 /// This is a lightweight value class with storage requirements equivalent to
69 /// a single pointer, but it does have reference-counting overhead when
71 class PooledStringPtr {
72 typedef StringPool::entry_t entry_t;
76 PooledStringPtr() : S(0) {}
78 explicit PooledStringPtr(entry_t *E) : S(E) {
79 if (S) ++S->getValue().Refcount;
82 PooledStringPtr(const PooledStringPtr &That) : S(That.S) {
83 if (S) ++S->getValue().Refcount;
86 PooledStringPtr &operator=(const PooledStringPtr &That) {
90 if (S) ++S->getValue().Refcount;
98 if (--S->getValue().Refcount == 0) {
99 S->getValue().Pool->InternTable.remove(S);
105 ~PooledStringPtr() { clear(); }
107 inline const char *begin() const {
108 assert(*this && "Attempt to dereference empty PooledStringPtr!");
109 return S->getKeyData();
112 inline const char *end() const {
113 assert(*this && "Attempt to dereference empty PooledStringPtr!");
114 return S->getKeyData() + S->getKeyLength();
117 inline unsigned size() const {
118 assert(*this && "Attempt to dereference empty PooledStringPtr!");
119 return S->getKeyLength();
122 inline const char *operator*() const { return begin(); }
123 inline operator bool() const { return S != 0; }
125 inline bool operator==(const PooledStringPtr &That) { return S == That.S; }
126 inline bool operator!=(const PooledStringPtr &That) { return S != That.S; }
129 PooledStringPtr StringPool::intern(const char *Str) {
130 return intern(Str, Str + strlen(Str));
133 } // End llvm namespace