StringPool.h revision 210299
1169691Skan//===-- StringPool.h - Interned string pool ---------------------*- C++ -*-===//
2169691Skan//
3169691Skan//                     The LLVM Compiler Infrastructure
4169691Skan//
5169691Skan// This file is distributed under the University of Illinois Open Source
6169691Skan// License. See LICENSE.TXT for details.
7169691Skan//
8169691Skan//===----------------------------------------------------------------------===//
9169691Skan//
10169691Skan// This file declares an interned string pool, which helps reduce the cost of
11169691Skan// strings by using the same storage for identical strings.
12169691Skan//
13169691Skan// To intern a string:
14169691Skan//
15169691Skan//   StringPool Pool;
16169691Skan//   PooledStringPtr Str = Pool.intern("wakka wakka");
17169691Skan//
18169691Skan// To use the value of an interned string, use operator bool and operator*:
19169691Skan//
20169691Skan//   if (Str)
21169691Skan//     cerr << "the string is" << *Str << "\n";
22169691Skan//
23169691Skan// Pooled strings are immutable, but you can change a PooledStringPtr to point
24169691Skan// to another instance. So that interned strings can eventually be freed,
25169691Skan// strings in the string pool are reference-counted (automatically).
26169691Skan//
27169691Skan//===----------------------------------------------------------------------===//
28169691Skan
29169691Skan#ifndef LLVM_SUPPORT_STRINGPOOL_H
30169691Skan#define LLVM_SUPPORT_STRINGPOOL_H
31169691Skan
32169691Skan#include "llvm/ADT/StringMap.h"
33169691Skan#include <new>
34169691Skan#include <cassert>
35169691Skan
36169691Skannamespace llvm {
37169691Skan
38169691Skan  class PooledStringPtr;
39169691Skan
40169691Skan  /// StringPool - An interned string pool. Use the intern method to add a
41169691Skan  /// string. Strings are removed automatically as PooledStringPtrs are
42169691Skan  /// destroyed.
43169691Skan  class StringPool {
44169691Skan    /// PooledString - This is the value of an entry in the pool's interning
45169691Skan    /// table.
46169691Skan    struct PooledString {
47169691Skan      StringPool *Pool;  ///< So the string can remove itself.
48169691Skan      unsigned Refcount; ///< Number of referencing PooledStringPtrs.
49169691Skan
50169691Skan    public:
51169691Skan      PooledString() : Pool(0), Refcount(0) { }
52169691Skan    };
53169691Skan
54169691Skan    friend class PooledStringPtr;
55169691Skan
56169691Skan    typedef StringMap<PooledString> table_t;
57169691Skan    typedef StringMapEntry<PooledString> entry_t;
58169691Skan    table_t InternTable;
59169691Skan
60169691Skan  public:
61169691Skan    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(StringRef Str);
68
69    /// empty - Checks whether the pool is empty. Returns true if so.
70    ///
71    inline bool empty() const { return InternTable.empty(); }
72  };
73
74  /// PooledStringPtr - A pointer to an interned string. Use operator bool to
75  /// test whether the pointer is valid, and operator * to get the string if so.
76  /// This is a lightweight value class with storage requirements equivalent to
77  /// a single pointer, but it does have reference-counting overhead when
78  /// copied.
79  class PooledStringPtr {
80    typedef StringPool::entry_t entry_t;
81    entry_t *S;
82
83  public:
84    PooledStringPtr() : S(0) {}
85
86    explicit PooledStringPtr(entry_t *E) : S(E) {
87      if (S) ++S->getValue().Refcount;
88    }
89
90    PooledStringPtr(const PooledStringPtr &That) : S(That.S) {
91      if (S) ++S->getValue().Refcount;
92    }
93
94    PooledStringPtr &operator=(const PooledStringPtr &That) {
95      if (S != That.S) {
96        clear();
97        S = That.S;
98        if (S) ++S->getValue().Refcount;
99      }
100      return *this;
101    }
102
103    void clear() {
104      if (!S)
105        return;
106      if (--S->getValue().Refcount == 0) {
107        S->getValue().Pool->InternTable.remove(S);
108        S->Destroy();
109      }
110      S = 0;
111    }
112
113    ~PooledStringPtr() { clear(); }
114
115    inline const char *begin() const {
116      assert(*this && "Attempt to dereference empty PooledStringPtr!");
117      return S->getKeyData();
118    }
119
120    inline const char *end() const {
121      assert(*this && "Attempt to dereference empty PooledStringPtr!");
122      return S->getKeyData() + S->getKeyLength();
123    }
124
125    inline unsigned size() const {
126      assert(*this && "Attempt to dereference empty PooledStringPtr!");
127      return S->getKeyLength();
128    }
129
130    inline const char *operator*() const { return begin(); }
131    inline operator bool() const { return S != 0; }
132
133    inline bool operator==(const PooledStringPtr &That) { return S == That.S; }
134    inline bool operator!=(const PooledStringPtr &That) { return S != That.S; }
135  };
136
137} // End llvm namespace
138
139#endif
140