llvm.org GIT mirror llvm / 985cb62
Adding a StringPool data structure, which GC will use. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44705 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 12 years ago
2 changed file(s) with 170 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- StringPool.h - Interned string pool -------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Gordon Henriksen and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares an interned string pool, which helps reduce the cost of
10 // strings by using the same storage for identical strings.
11 //
12 // To intern a string:
13 //
14 // StringPool Pool;
15 // PooledStringPtr Str = Pool.intern("wakka wakka");
16 //
17 // To use the value of an interned string, use operator bool and operator*:
18 //
19 // if (Str)
20 // cerr << "the string is" << *Str << "\n";
21 //
22 // Pooled strings are immutable, but you can change a PooledStringPtr to point
23 // to another instance. So that interned strings can eventually be freed,
24 // strings in the string pool are reference-counted (automatically).
25 //
26 //===----------------------------------------------------------------------===//
27
28 #ifndef LLVM_SUPPORT_STRINGPOOL_H
29 #define LLVM_SUPPORT_STRINGPOOL_H
30
31 #include
32 #include
33 #include
34
35 namespace llvm {
36
37 class PooledStringPtr;
38
39 /// StringPool - An interned string pool. Use the intern method to add a
40 /// string. Strings are removed automatically as PooledStringPtrs are
41 /// destroyed.
42 class StringPool {
43 struct PooledString {
44 StringPool *Pool; ///< So the string can remove itself.
45 unsigned Refcount; ///< Number of referencing PooledStringPtrs.
46
47 public:
48 PooledString() : Pool(0), Refcount(0) { }
49 };
50
51 friend class PooledStringPtr;
52
53 typedef StringMap table_t;
54 typedef StringMapEntry entry_t;
55 table_t InternTable;
56
57 public:
58 StringPool();
59 ~StringPool();
60
61 PooledStringPtr intern(const char *Begin, const char *End);
62 inline PooledStringPtr intern(const char *Str);
63 };
64
65 /// PooledStringPtr - A pointer to an interned string. Use operator bool to
66 /// test whether the pointer is valid, and operator * to get the string if so.
67 /// This is a lightweight value class with storage requirements equivalent to
68 /// a single pointer, but it does have reference-counting overhead when
69 /// copied.
70 class PooledStringPtr {
71 typedef StringPool::entry_t entry_t;
72 entry_t *S;
73
74 public:
75 PooledStringPtr() : S(0) {}
76
77 explicit PooledStringPtr(entry_t *E) : S(E) {
78 if (S) ++S->getValue().Refcount;
79 }
80
81 PooledStringPtr(const PooledStringPtr &That) : S(That.S) {
82 if (S) ++S->getValue().Refcount;
83 }
84
85 PooledStringPtr &operator=(const PooledStringPtr &That) {
86 if (S != That.S) {
87 clear();
88 S = That.S;
89 if (S) ++S->getValue().Refcount;
90 }
91 return *this;
92 }
93
94 void clear() {
95 if (!S)
96 return;
97 if (--S->getValue().Refcount == 0) {
98 S->getValue().Pool->InternTable.remove(S);
99 delete S;
100 }
101 S = 0;
102 }
103
104 ~PooledStringPtr() { clear(); }
105
106 inline const char *begin() const {
107 assert(*this && "Attempt to dereference empty PooledStringPtr!");
108 return S->getKeyData();
109 }
110
111 inline const char *end() const {
112 assert(*this && "Attempt to dereference empty PooledStringPtr!");
113 return S->getKeyData() + S->getKeyLength();
114 }
115
116 inline unsigned size() const {
117 assert(*this && "Attempt to dereference empty PooledStringPtr!");
118 return S->getKeyLength();
119 }
120
121 inline const char *operator*() const { return begin(); }
122 inline operator bool() const { return S != 0; }
123
124 inline bool operator==(const PooledStringPtr &That) { return S == That.S; }
125 inline bool operator!=(const PooledStringPtr &That) { return S != That.S; }
126 };
127
128 PooledStringPtr StringPool::intern(const char *Str) {
129 return intern(Str, Str + strlen(Str));
130 }
131
132 } // End llvm namespace
133
134 #endif
0 //===-- StringPool.cpp - Interned string pool -----------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Gordon Henriksen and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the StringPool class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Support/StringPool.h"
14 #include "llvm/Support/Streams.h"
15
16 using namespace llvm;
17
18 StringPool::StringPool() {}
19
20 StringPool::~StringPool() {
21 assert(InternTable.empty() && "PooledStringPtr leaked!");
22 }
23
24 PooledStringPtr StringPool::intern(const char *Begin, const char *End) {
25 table_t::iterator I = InternTable.find(Begin, End);
26 if (I != InternTable.end())
27 return PooledStringPtr(&*I);
28
29 entry_t *S = entry_t::Create(Begin, End);
30 S->getValue().Pool = this;
31 InternTable.insert(S);
32
33 return PooledStringPtr(S);
34 }