LLVM  11.0.0git
StringPool.h
Go to the documentation of this file.
1 //===- StringPool.h - Intern'd string pool ----------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares an interned string pool with separately malloc and
10 // reference counted entries. This can reduce the cost of strings by using the
11 // 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 
34 namespace llvm {
35 
36 class PooledStringPtr;
37 
38 /// StringPool - An interned string pool. Use the intern method to add a
39 /// string. Strings are removed automatically as PooledStringPtrs are
40 /// destroyed.
41 class StringPool {
42  /// PooledString - This is the value of an entry in the pool's interning
43  /// table.
44  struct PooledString {
45  StringPool *pool = nullptr; ///< So the string can remove itself.
46  unsigned refcount = 0; ///< Number of referencing PooledStringPtrs.
47 
48  public:
49  PooledString() = default;
50  };
51 
52  friend class PooledStringPtr;
54  StringMap<PooledString> internTable;
55 
56 public:
57  StringPool();
58  ~StringPool();
59 
60  /// intern - Adds a string to the pool and returns a reference-counted
61  /// pointer to it. No additional memory is allocated if the string already
62  /// exists in the pool.
64 
65  /// empty - Checks whether the pool is empty. Returns true if so.
66  bool empty() const { return internTable.empty(); }
67 };
68 
69 /// PooledStringPtr - A pointer to an interned string. Use operator bool to
70 /// test whether the pointer is valid, and operator * to get the string if so.
71 /// This is a lightweight value class with storage requirements equivalent to
72 /// a single pointer, but it does have reference-counting overhead when
73 /// copied.
75  using Entry = StringPool::Entry;
76  Entry *entry = nullptr;
77 
78 public:
79  PooledStringPtr() = default;
80 
81  explicit PooledStringPtr(Entry *e) : entry(e) {
82  if (entry)
83  ++entry->getValue().refcount;
84  }
85 
86  PooledStringPtr(const PooledStringPtr &that) : entry(that.entry) {
87  if (entry)
88  ++entry->getValue().refcount;
89  }
90 
92  if (entry != that.entry) {
93  clear();
94  entry = that.entry;
95  if (entry)
96  ++entry->getValue().refcount;
97  }
98  return *this;
99  }
100 
101  void clear() {
102  if (!entry)
103  return;
104  if (--entry->getValue().refcount == 0) {
105  entry->getValue().pool->internTable.remove(entry);
107  entry->Destroy(allocator);
108  }
109  entry = nullptr;
110  }
111 
113 
114  const char *begin() const {
115  assert(*this && "Attempt to dereference empty PooledStringPtr!");
116  return entry->getKeyData();
117  }
118 
119  const char *end() const {
120  assert(*this && "Attempt to dereference empty PooledStringPtr!");
121  return entry->getKeyData() + entry->getKeyLength();
122  }
123 
124  unsigned size() const {
125  assert(*this && "Attempt to dereference empty PooledStringPtr!");
126  return entry->getKeyLength();
127  }
128 
129  const char *operator*() const { return begin(); }
130  explicit operator bool() const { return entry != nullptr; }
131 
132  bool operator==(const PooledStringPtr &that) const {
133  return entry == that.entry;
134  }
135  bool operator!=(const PooledStringPtr &that) const {
136  return entry != that.entry;
137  }
138 };
139 
140 } // end namespace llvm
141 
142 #endif // LLVM_SUPPORT_STRINGPOOL_H
friend class PooledStringPtr
Definition: StringPool.h:52
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
This class represents lattice values for constants.
Definition: AllocatorList.h:23
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
PooledStringPtr(const PooledStringPtr &that)
Definition: StringPool.h:86
PooledStringPtr & operator=(const PooledStringPtr &that)
Definition: StringPool.h:91
StringPool - An interned string pool.
Definition: StringPool.h:41
const char * end() const
Definition: StringPool.h:119
bool operator!=(const PooledStringPtr &that) const
Definition: StringPool.h:135
const char * operator*() const
Definition: StringPool.h:129
bool operator==(const PooledStringPtr &that) const
Definition: StringPool.h:132
PooledStringPtr(Entry *e)
Definition: StringPool.h:81
constexpr double e
Definition: MathExtras.h:58
PooledStringPtr intern(StringRef string)
intern - Adds a string to the pool and returns a reference-counted pointer to it. ...
Definition: StringPool.cpp:22
bool empty() const
empty - Checks whether the pool is empty. Returns true if so.
Definition: StringPool.h:66
void Destroy(AllocatorTy &allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
unsigned size() const
Definition: StringPool.h:124
static void clear(coro::Shape &Shape)
Definition: Coroutines.cpp:226
const char * begin() const
Definition: StringPool.h:114
size_t getKeyLength() const
const char * getKeyData() const
getKeyData - Return the start of the string data that is the key for this value.
bool empty() const
Definition: StringMap.h:90
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
PooledStringPtr - A pointer to an interned string.
Definition: StringPool.h:74
const ValueTy & getValue() const
print Instructions which execute on loop entry
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57