LLDB mainline
ConstString.cpp
Go to the documentation of this file.
1//===-- ConstString.cpp ---------------------------------------------------===//
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
10
11#include "lldb/Utility/Stream.h"
12
13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/iterator.h"
16#include "llvm/Support/Allocator.h"
17#include "llvm/Support/DJB.h"
18#include "llvm/Support/FormatProviders.h"
19#include "llvm/Support/Threading.h"
20
21#include <array>
22#include <mutex>
23#include <shared_mutex>
24#include <utility>
25
26#include <cinttypes>
27#include <cstdint>
28#include <cstring>
29
30using namespace lldb_private;
31
32#if !defined(__APPLE__)
33using PoolMutex = std::shared_mutex;
34#else
35#include <os/lock.h>
36
37namespace {
38/// On Apple platforms os_unfair_lock is significantly faster than
39/// pthread_rwlock for concurrent writes, and roughly on par for concurrent
40/// reads.
41///
42/// The class satisfies both Lockable and SharedLockable so it composes with
43/// std::lock_guard and std::shared_lock.
44class PoolMutex {
45public:
46 void lock() { os_unfair_lock_lock(&m_lock); }
47 void unlock() { os_unfair_lock_unlock(&m_lock); }
48 void lock_shared() { os_unfair_lock_lock(&m_lock); }
49 void unlock_shared() { os_unfair_lock_unlock(&m_lock); }
50
51private:
52 os_unfair_lock m_lock = OS_UNFAIR_LOCK_INIT;
53};
54} // namespace
55#endif
56
57class Pool {
58public:
59 /// The default BumpPtrAllocatorImpl slab size.
60 static const size_t AllocatorSlabSize = 4096;
61 static const size_t SizeThreshold = AllocatorSlabSize;
62 /// Every Pool has its own allocator which receives an equal share of
63 /// the ConstString allocations. This means that when allocating many
64 /// ConstStrings, every allocator sees only its small share of allocations and
65 /// assumes LLDB only allocated a small amount of memory so far. In reality
66 /// LLDB allocated a total memory that is N times as large as what the
67 /// allocator sees (where N is the number of string pools). This causes that
68 /// the BumpPtrAllocator continues a long time to allocate memory in small
69 /// chunks which only makes sense when allocating a small amount of memory
70 /// (which is true from the perspective of a single allocator). On some
71 /// systems doing all these small memory allocations causes LLDB to spend
72 /// a lot of time in malloc, so we need to force all these allocators to
73 /// behave like one allocator in terms of scaling their memory allocations
74 /// with increased demand. To do this we set the growth delay for each single
75 /// allocator to a rate so that our pool of allocators scales their memory
76 /// allocations similar to a single BumpPtrAllocatorImpl.
77 ///
78 /// Currently we have 256 string pools and the normal growth delay of the
79 /// BumpPtrAllocatorImpl is 128 (i.e., the memory allocation size increases
80 /// every 128 full chunks), so by changing the delay to 1 we get a
81 /// total growth delay in our allocator collection of 256/1 = 256. This is
82 /// still only half as fast as a normal allocator but we can't go any faster
83 /// without decreasing the number of string pools.
84 static const size_t AllocatorGrowthDelay = 1;
85 typedef llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, AllocatorSlabSize,
88 typedef const char *StringPoolValueType;
89 typedef llvm::StringMap<StringPoolValueType, Allocator> StringPool;
90 typedef llvm::StringMapEntry<StringPoolValueType> StringPoolEntryType;
91
92 static StringPoolEntryType &
93 GetStringMapEntryFromKeyData(const char *keyData) {
94 return StringPoolEntryType::GetStringMapEntryFromKeyData(keyData);
95 }
96
97 static size_t GetConstCStringLength(const char *ccstr) {
98 if (ccstr != nullptr) {
99 // Since the entry is read only, and we derive the entry entirely from
100 // the pointer, we don't need the lock.
102 return entry.getKeyLength();
103 }
104 return 0;
105 }
106
108 const char *const ccstr = str.data();
109 if (ccstr != nullptr) {
110 const PoolEntry &pool = selectPool(str);
111 std::shared_lock<PoolMutex> lock(pool.m_mutex);
112 return GetStringMapEntryFromKeyData(ccstr).getValue();
113 }
114 return nullptr;
115 }
116
117 const char *GetConstCString(const char *cstr) {
118 if (cstr != nullptr)
119 return GetConstCStringWithLength(cstr, strlen(cstr));
120 return nullptr;
121 }
122
123 const char *GetConstCStringWithLength(const char *cstr, size_t cstr_len) {
124 if (cstr != nullptr)
125 return GetConstCStringWithStringRef(llvm::StringRef(cstr, cstr_len));
126 return nullptr;
127 }
128
129 const char *GetConstCStringWithStringRef(llvm::StringRef string_ref) {
130 if (string_ref.data()) {
131 const uint32_t string_hash = StringPool::hash(string_ref);
132 PoolEntry &pool = selectPool(string_hash);
133
134 {
135 std::shared_lock<PoolMutex> lock(pool.m_mutex);
136 auto it = pool.m_string_map.find(string_ref, string_hash);
137 if (it != pool.m_string_map.end())
138 return it->getKeyData();
139 }
140
141 std::lock_guard<PoolMutex> lock(pool.m_mutex);
142 StringPoolEntryType &entry =
143 *pool.m_string_map
144 .insert(std::make_pair(string_ref, nullptr), string_hash)
145 .first;
146 return entry.getKeyData();
147 }
148 return nullptr;
149 }
150
151 const char *GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled,
152 llvm::StringRef mangled) {
153 const char *demangled_ccstr = nullptr;
154 const char *const mangled_ccstr = mangled.data();
155
156 {
157 const uint32_t demangled_hash = StringPool::hash(demangled);
158 PoolEntry &pool = selectPool(demangled_hash);
159 std::lock_guard<PoolMutex> lock(pool.m_mutex);
160
161 // Make or update string pool entry with the mangled counterpart
162 StringPool &map = pool.m_string_map;
163 StringPoolEntryType &entry =
164 *map.try_emplace_with_hash(demangled, demangled_hash).first;
165
166 entry.second = mangled_ccstr;
167
168 // Extract the const version of the demangled_cstr
169 demangled_ccstr = entry.getKeyData();
170 }
171
172 {
173 // Now assign the demangled const string as the counterpart of the
174 // mangled const string...
175 PoolEntry &pool = selectPool(mangled);
176 std::lock_guard<PoolMutex> lock(pool.m_mutex);
177 GetStringMapEntryFromKeyData(mangled_ccstr).setValue(demangled_ccstr);
178 }
179
180 // Return the constant demangled C string
181 return demangled_ccstr;
182 }
183
184 const char *GetConstTrimmedCStringWithLength(const char *cstr,
185 size_t cstr_len) {
186 if (cstr != nullptr) {
187 const size_t trimmed_len = strnlen(cstr, cstr_len);
188 return GetConstCStringWithLength(cstr, trimmed_len);
189 }
190 return nullptr;
191 }
192
195 for (const auto &pool : m_string_pools) {
196 std::shared_lock<PoolMutex> lock(pool.m_mutex);
197 const Allocator &alloc = pool.m_string_map.getAllocator();
198 stats.bytes_total += alloc.getTotalMemory();
199 stats.bytes_used += alloc.getBytesAllocated();
200 }
201 return stats;
202 }
203
204protected:
209
210 std::array<PoolEntry, 256> m_string_pools;
211
212 PoolEntry &selectPool(const llvm::StringRef &s) {
213 return selectPool(StringPool::hash(s));
214 }
215
216 PoolEntry &selectPool(uint32_t h) {
217 return m_string_pools[((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff];
218 }
219};
220
221// Frameworks and dylibs aren't supposed to have global C++ initializers so we
222// hide the string pool in a static function so that it will get initialized on
223// the first call to this static function.
224//
225// Note, for now we make the string pool a pointer to the pool, because we
226// can't guarantee that some objects won't get destroyed after the global
227// destructor chain is run, and trying to make sure no destructors touch
228// ConstStrings is difficult. So we leak the pool instead.
229static Pool &StringPool() {
230 static llvm::once_flag g_pool_initialization_flag;
231 static Pool *g_string_pool = nullptr;
232
233 llvm::call_once(g_pool_initialization_flag,
234 []() { g_string_pool = new Pool(); });
235
236 return *g_string_pool;
237}
238
240 : m_string(StringPool().GetConstCString(cstr)) {}
241
242ConstString::ConstString(const char *cstr, size_t cstr_len)
243 : m_string(StringPool().GetConstCStringWithLength(cstr, cstr_len)) {}
244
245ConstString::ConstString(llvm::StringRef s)
246 : m_string(StringPool().GetConstCStringWithStringRef(s)) {}
247
249 if (m_string == rhs.m_string)
250 return false;
251
252 llvm::StringRef lhs_string_ref(GetStringRef());
253 llvm::StringRef rhs_string_ref(rhs.GetStringRef());
254
255 // If both have valid C strings, then return the comparison
256 if (lhs_string_ref.data() && rhs_string_ref.data())
257 return lhs_string_ref < rhs_string_ref;
258
259 // Else one of them was nullptr, so if LHS is nullptr then it is less than
260 return lhs_string_ref.data() == nullptr;
261}
262
264 const char *cstr = str.GetCString();
265 if (cstr != nullptr)
266 s << cstr;
267
268 return s;
269}
270
274
276 const bool case_sensitive) {
277 if (lhs.m_string == rhs.m_string)
278 return true;
279
280 // Since the pointers weren't equal, and identical ConstStrings always have
281 // identical pointers, the result must be false for case sensitive equality
282 // test.
283 if (case_sensitive)
284 return false;
285
286 // perform case insensitive equality test
287 llvm::StringRef lhs_string_ref(lhs.GetStringRef());
288 llvm::StringRef rhs_string_ref(rhs.GetStringRef());
289 return lhs_string_ref.equals_insensitive(rhs_string_ref);
290}
291
293 const bool case_sensitive) {
294 // If the iterators are the same, this is the same string
295 const char *lhs_cstr = lhs.m_string;
296 const char *rhs_cstr = rhs.m_string;
297 if (lhs_cstr == rhs_cstr)
298 return 0;
299 if (lhs_cstr && rhs_cstr) {
300 llvm::StringRef lhs_string_ref(lhs.GetStringRef());
301 llvm::StringRef rhs_string_ref(rhs.GetStringRef());
302
303 if (case_sensitive) {
304 return lhs_string_ref.compare(rhs_string_ref);
305 } else {
306 return lhs_string_ref.compare_insensitive(rhs_string_ref);
307 }
308 }
309
310 if (lhs_cstr)
311 return +1; // LHS isn't nullptr but RHS is
312 else
313 return -1; // LHS is nullptr but RHS isn't
314}
315
316void ConstString::Dump(Stream *s, const char *fail_value) const {
317 if (s != nullptr) {
318 const char *cstr = AsCString(fail_value);
319 if (cstr != nullptr)
320 s->PutCString(cstr);
321 }
322}
323
325 const char *cstr = GetCString();
326 size_t cstr_len = GetLength();
327 // Only print the parens if we have a non-nullptr string
328 const char *parens = cstr ? "\"" : "";
329 s->Printf("%*p: ConstString, string = %s%s%s, length = %" PRIu64,
330 static_cast<int>(sizeof(void *) * 2),
331 static_cast<const void *>(this), parens, cstr, parens,
332 static_cast<uint64_t>(cstr_len));
333}
334
335void ConstString::SetCString(const char *cstr) {
337}
338
342
343void ConstString::SetStringWithMangledCounterpart(llvm::StringRef demangled,
344 ConstString mangled) {
346 demangled, mangled.GetStringRef());
347}
348
351 return (bool)counterpart;
352}
353
354void ConstString::SetCStringWithLength(const char *cstr, size_t cstr_len) {
356}
357
359 size_t cstr_len) {
361}
362
366
367void llvm::format_provider<ConstString>::format(const ConstString &CS,
368 llvm::raw_ostream &OS,
369 llvm::StringRef Options) {
370 format_provider<StringRef>::format(CS.GetStringRef(), OS, Options);
371}
std::shared_mutex PoolMutex
static Pool & StringPool()
PoolEntry & selectPool(const llvm::StringRef &s)
static const size_t SizeThreshold
static StringPoolEntryType & GetStringMapEntryFromKeyData(const char *keyData)
const char * GetConstCStringWithStringRef(llvm::StringRef string_ref)
static const size_t AllocatorSlabSize
The default BumpPtrAllocatorImpl slab size.
llvm::StringMap< StringPoolValueType, Allocator > StringPool
const char * GetConstCStringWithLength(const char *cstr, size_t cstr_len)
StringPoolValueType GetMangledCounterpart(llvm::StringRef str)
const char * GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled, llvm::StringRef mangled)
const char * GetConstTrimmedCStringWithLength(const char *cstr, size_t cstr_len)
ConstString::MemoryStats GetMemoryStats() const
llvm::BumpPtrAllocatorImpl< llvm::MallocAllocator, AllocatorSlabSize, SizeThreshold, AllocatorGrowthDelay > Allocator
std::array< PoolEntry, 256 > m_string_pools
llvm::StringMapEntry< StringPoolValueType > StringPoolEntryType
const char * GetConstCString(const char *cstr)
PoolEntry & selectPool(uint32_t h)
static const size_t AllocatorGrowthDelay
Every Pool has its own allocator which receives an equal share of the ConstString allocations.
const char * StringPoolValueType
static size_t GetConstCStringLength(const char *ccstr)
A uniqued constant string class.
Definition ConstString.h:40
bool GetMangledCounterpart(ConstString &counterpart) const
Retrieve the mangled or demangled counterpart for a mangled or demangled ConstString.
static MemoryStats GetMemoryStats()
void SetCStringWithLength(const char *cstr, size_t cstr_len)
Set the C string value with length.
void SetCString(const char *cstr)
Set the C string value.
static int Compare(ConstString lhs, ConstString rhs, const bool case_sensitive=true)
Compare two string objects.
ConstString()=default
Default constructor.
void Dump(Stream *s, const char *value_if_empty=nullptr) const
Dump the object description to a stream.
static bool Equals(ConstString lhs, ConstString rhs, const bool case_sensitive=true)
Equal to operator.
void DumpDebug(Stream *s) const
Dump the object debug description to a stream.
void SetTrimmedCStringWithLength(const char *cstr, size_t fixed_cstr_len)
Set the C string value with the minimum length between fixed_cstr_len and the actual length of the C ...
size_t GetLength() const
Get the length in bytes of string value.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
void SetString(llvm::StringRef s)
bool operator<(ConstString rhs) const
const char * GetCString() const
Get the string value as a C string.
void SetStringWithMangledCounterpart(llvm::StringRef demangled, ConstString mangled)
Set the C string value and its mangled counterpart.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
A command line option parsing protocol class.
Definition Options.h:58
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
A class that represents a running process on the host machine.
Stream & operator<<(Stream &s, const Mangled &obj)
StringPool m_string_map