13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/iterator.h"
15#include "llvm/Support/Allocator.h"
16#include "llvm/Support/DJB.h"
17#include "llvm/Support/FormatProviders.h"
18#include "llvm/Support/RWMutex.h"
19#include "llvm/Support/Threading.h"
62 typedef llvm::StringMap<StringPoolValueType, Allocator>
StringPool;
67 return StringPoolEntryType::GetStringMapEntryFromKeyData(keyData);
71 if (ccstr !=
nullptr) {
75 return entry.getKey().size();
81 if (ccstr !=
nullptr) {
83 llvm::sys::SmartScopedReader<false> rlock(pool.
m_mutex);
102 if (string_ref.data()) {
103 const uint32_t string_hash = StringPool::hash(string_ref);
107 llvm::sys::SmartScopedReader<false> rlock(pool.
m_mutex);
108 auto it = pool.
m_string_map.find(string_ref, string_hash);
110 return it->getKeyData();
113 llvm::sys::SmartScopedWriter<false> wlock(pool.
m_mutex);
116 .insert(std::make_pair(string_ref,
nullptr), string_hash)
118 return entry.getKeyData();
125 const char *mangled_ccstr) {
126 const char *demangled_ccstr =
nullptr;
129 const uint32_t demangled_hash = StringPool::hash(demangled);
131 llvm::sys::SmartScopedWriter<false> wlock(pool.
m_mutex);
136 *map.try_emplace_with_hash(demangled, demangled_hash).first;
138 entry.second = mangled_ccstr;
141 demangled_ccstr = entry.getKeyData();
148 llvm::sys::SmartScopedWriter<false> wlock(pool.
m_mutex);
153 return demangled_ccstr;
158 if (cstr !=
nullptr) {
159 const size_t trimmed_len = strnlen(cstr, cstr_len);
168 llvm::sys::SmartScopedReader<false> rlock(pool.m_mutex);
169 const Allocator &alloc = pool.m_string_map.getAllocator();
171 stats.
bytes_used += alloc.getBytesAllocated();
178 mutable llvm::sys::SmartRWMutex<false>
m_mutex;
189 return m_string_pools[((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff];
202 static llvm::once_flag g_pool_initialization_flag;
203 static Pool *g_string_pool =
nullptr;
205 llvm::call_once(g_pool_initialization_flag,
206 []() { g_string_pool =
new Pool(); });
208 return *g_string_pool;
212 : m_string(
StringPool().GetConstCString(cstr)) {}
215 : m_string(
StringPool().GetConstCStringWithLength(cstr, cstr_len)) {}
218 : m_string(
StringPool().GetConstCStringWithStringRef(s)) {}
228 if (lhs_string_ref.data() && rhs_string_ref.data())
229 return lhs_string_ref < rhs_string_ref;
232 return lhs_string_ref.data() ==
nullptr;
248 const bool case_sensitive) {
261 return lhs_string_ref.equals_insensitive(rhs_string_ref);
265 const bool case_sensitive) {
267 const char *lhs_cstr = lhs.
m_string;
268 const char *rhs_cstr = rhs.
m_string;
269 if (lhs_cstr == rhs_cstr)
271 if (lhs_cstr && rhs_cstr) {
275 if (case_sensitive) {
276 return lhs_string_ref.compare(rhs_string_ref);
278 return lhs_string_ref.compare_insensitive(rhs_string_ref);
290 const char *cstr =
AsCString(fail_value);
300 const char *parens = cstr ?
"\"" :
"";
301 s->
Printf(
"%*p: ConstString, string = %s%s%s, length = %" PRIu64,
302 static_cast<int>(
sizeof(
void *) * 2),
303 static_cast<const void *
>(
this), parens, cstr, parens,
304 static_cast<uint64_t
>(cstr_len));
323 return (
bool)counterpart;
339void llvm::format_provider<ConstString>::format(
const ConstString &CS,
340 llvm::raw_ostream &
OS,
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)
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
const char * GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled, const char *mangled_ccstr)
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
StringPoolValueType GetMangledCounterpart(const char *ccstr)
static size_t GetConstCStringLength(const char *ccstr)
A uniqued constant string class.
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.
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
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.
A command line option parsing protocol class.
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
A class that represents a running process on the host machine.
Stream & operator<<(Stream &s, const Mangled &obj)
llvm::sys::SmartRWMutex< false > m_mutex