98db8e2bf37c9fd673a05c2ad70f7557ef497ca9
[oota-llvm.git] / include / llvm / Support / StringPool.h
1 //===-- StringPool.h - Interned string pool -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares an interned string pool, which helps reduce the cost of
11 // strings by using the same storage for identical strings.
12 //
13 // To intern a string:
14 //
15 //   StringPool Pool;
16 //   PooledStringPtr Str = Pool.intern("wakka wakka");
17 //
18 // To use the value of an interned string, use operator bool and operator*:
19 //
20 //   if (Str)
21 //     cerr << "the string is" << *Str << "\n";
22 //
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).
26 //
27 //===----------------------------------------------------------------------===//
28
29 #ifndef LLVM_SUPPORT_STRINGPOOL_H
30 #define LLVM_SUPPORT_STRINGPOOL_H
31
32 #include "llvm/ADT/StringMap.h"
33 #include <new>
34 #include <cassert>
35
36 namespace llvm {
37
38   class PooledStringPtr;
39
40   /// StringPool - An interned string pool. Use the intern method to add a
41   /// string. Strings are removed automatically as PooledStringPtrs are
42   /// destroyed.
43   class StringPool {
44     /// PooledString - This is the value of an entry in the pool's interning
45     /// table.
46     struct PooledString {
47       StringPool *Pool;  ///< So the string can remove itself.
48       unsigned Refcount; ///< Number of referencing PooledStringPtrs.
49
50     public:
51       PooledString() : Pool(0), Refcount(0) { }
52     };
53
54     friend class PooledStringPtr;
55
56     typedef StringMap<PooledString> table_t;
57     typedef StringMapEntry<PooledString> entry_t;
58     table_t InternTable;
59
60   public:
61     StringPool();
62     ~StringPool();
63
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);
68
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);
73
74     /// empty - Checks whether the pool is empty. Returns true if so.
75     ///
76     inline bool empty() const { return InternTable.empty(); }
77   };
78
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
83   /// copied.
84   class PooledStringPtr {
85     typedef StringPool::entry_t entry_t;
86     entry_t *S;
87
88   public:
89     PooledStringPtr() : S(0) {}
90
91     explicit PooledStringPtr(entry_t *E) : S(E) {
92       if (S) ++S->getValue().Refcount;
93     }
94
95     PooledStringPtr(const PooledStringPtr &That) : S(That.S) {
96       if (S) ++S->getValue().Refcount;
97     }
98
99     PooledStringPtr &operator=(const PooledStringPtr &That) {
100       if (S != That.S) {
101         clear();
102         S = That.S;
103         if (S) ++S->getValue().Refcount;
104       }
105       return *this;
106     }
107
108     void clear() {
109       if (!S)
110         return;
111       if (--S->getValue().Refcount == 0) {
112         S->getValue().Pool->InternTable.remove(S);
113         S->Destroy();
114       }
115       S = 0;
116     }
117
118     ~PooledStringPtr() { clear(); }
119
120     inline const char *begin() const {
121       assert(*this && "Attempt to dereference empty PooledStringPtr!");
122       return S->getKeyData();
123     }
124
125     inline const char *end() const {
126       assert(*this && "Attempt to dereference empty PooledStringPtr!");
127       return S->getKeyData() + S->getKeyLength();
128     }
129
130     inline unsigned size() const {
131       assert(*this && "Attempt to dereference empty PooledStringPtr!");
132       return S->getKeyLength();
133     }
134
135     inline const char *operator*() const { return begin(); }
136     inline operator bool() const { return S != 0; }
137
138     inline bool operator==(const PooledStringPtr &That) { return S == That.S; }
139     inline bool operator!=(const PooledStringPtr &That) { return S != That.S; }
140   };
141
142   PooledStringPtr StringPool::intern(const char *Str) {
143     return intern(Str, Str + strlen(Str));
144   }
145
146 } // End llvm namespace
147
148 #endif