15ffd83dbSDimitry Andric //===-- ConstString.cpp ---------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Utility/ConstString.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
140b57cec5SDimitry Andric #include "llvm/ADT/iterator.h"
150b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
160b57cec5SDimitry Andric #include "llvm/Support/DJB.h"
170b57cec5SDimitry Andric #include "llvm/Support/FormatProviders.h"
180b57cec5SDimitry Andric #include "llvm/Support/RWMutex.h"
190b57cec5SDimitry Andric #include "llvm/Support/Threading.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric #include <array>
220b57cec5SDimitry Andric #include <utility>
230b57cec5SDimitry Andric 
24*5f7ddb14SDimitry Andric #include <cinttypes>
25*5f7ddb14SDimitry Andric #include <cstdint>
26*5f7ddb14SDimitry Andric #include <cstring>
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace lldb_private;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric class Pool {
310b57cec5SDimitry Andric public:
325ffd83dbSDimitry Andric   /// The default BumpPtrAllocatorImpl slab size.
335ffd83dbSDimitry Andric   static const size_t AllocatorSlabSize = 4096;
345ffd83dbSDimitry Andric   static const size_t SizeThreshold = AllocatorSlabSize;
355ffd83dbSDimitry Andric   /// Every Pool has its own allocator which receives an equal share of
365ffd83dbSDimitry Andric   /// the ConstString allocations. This means that when allocating many
375ffd83dbSDimitry Andric   /// ConstStrings, every allocator sees only its small share of allocations and
385ffd83dbSDimitry Andric   /// assumes LLDB only allocated a small amount of memory so far. In reality
395ffd83dbSDimitry Andric   /// LLDB allocated a total memory that is N times as large as what the
405ffd83dbSDimitry Andric   /// allocator sees (where N is the number of string pools). This causes that
415ffd83dbSDimitry Andric   /// the BumpPtrAllocator continues a long time to allocate memory in small
425ffd83dbSDimitry Andric   /// chunks which only makes sense when allocating a small amount of memory
435ffd83dbSDimitry Andric   /// (which is true from the perspective of a single allocator). On some
445ffd83dbSDimitry Andric   /// systems doing all these small memory allocations causes LLDB to spend
455ffd83dbSDimitry Andric   /// a lot of time in malloc, so we need to force all these allocators to
465ffd83dbSDimitry Andric   /// behave like one allocator in terms of scaling their memory allocations
475ffd83dbSDimitry Andric   /// with increased demand. To do this we set the growth delay for each single
485ffd83dbSDimitry Andric   /// allocator to a rate so that our pool of allocators scales their memory
495ffd83dbSDimitry Andric   /// allocations similar to a single BumpPtrAllocatorImpl.
505ffd83dbSDimitry Andric   ///
515ffd83dbSDimitry Andric   /// Currently we have 256 string pools and the normal growth delay of the
525ffd83dbSDimitry Andric   /// BumpPtrAllocatorImpl is 128 (i.e., the memory allocation size increases
535ffd83dbSDimitry Andric   /// every 128 full chunks), so by changing the delay to 1 we get a
545ffd83dbSDimitry Andric   /// total growth delay in our allocator collection of 256/1 = 256. This is
555ffd83dbSDimitry Andric   /// still only half as fast as a normal allocator but we can't go any faster
565ffd83dbSDimitry Andric   /// without decreasing the number of string pools.
575ffd83dbSDimitry Andric   static const size_t AllocatorGrowthDelay = 1;
585ffd83dbSDimitry Andric   typedef llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, AllocatorSlabSize,
595ffd83dbSDimitry Andric                                      SizeThreshold, AllocatorGrowthDelay>
605ffd83dbSDimitry Andric       Allocator;
610b57cec5SDimitry Andric   typedef const char *StringPoolValueType;
625ffd83dbSDimitry Andric   typedef llvm::StringMap<StringPoolValueType, Allocator> StringPool;
630b57cec5SDimitry Andric   typedef llvm::StringMapEntry<StringPoolValueType> StringPoolEntryType;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   static StringPoolEntryType &
GetStringMapEntryFromKeyData(const char * keyData)660b57cec5SDimitry Andric   GetStringMapEntryFromKeyData(const char *keyData) {
670b57cec5SDimitry Andric     return StringPoolEntryType::GetStringMapEntryFromKeyData(keyData);
680b57cec5SDimitry Andric   }
690b57cec5SDimitry Andric 
GetConstCStringLength(const char * ccstr)700b57cec5SDimitry Andric   static size_t GetConstCStringLength(const char *ccstr) {
710b57cec5SDimitry Andric     if (ccstr != nullptr) {
720b57cec5SDimitry Andric       // Since the entry is read only, and we derive the entry entirely from
730b57cec5SDimitry Andric       // the pointer, we don't need the lock.
740b57cec5SDimitry Andric       const StringPoolEntryType &entry = GetStringMapEntryFromKeyData(ccstr);
750b57cec5SDimitry Andric       return entry.getKey().size();
760b57cec5SDimitry Andric     }
770b57cec5SDimitry Andric     return 0;
780b57cec5SDimitry Andric   }
790b57cec5SDimitry Andric 
GetMangledCounterpart(const char * ccstr) const800b57cec5SDimitry Andric   StringPoolValueType GetMangledCounterpart(const char *ccstr) const {
810b57cec5SDimitry Andric     if (ccstr != nullptr) {
820b57cec5SDimitry Andric       const uint8_t h = hash(llvm::StringRef(ccstr));
830b57cec5SDimitry Andric       llvm::sys::SmartScopedReader<false> rlock(m_string_pools[h].m_mutex);
840b57cec5SDimitry Andric       return GetStringMapEntryFromKeyData(ccstr).getValue();
850b57cec5SDimitry Andric     }
860b57cec5SDimitry Andric     return nullptr;
870b57cec5SDimitry Andric   }
880b57cec5SDimitry Andric 
GetConstCString(const char * cstr)890b57cec5SDimitry Andric   const char *GetConstCString(const char *cstr) {
900b57cec5SDimitry Andric     if (cstr != nullptr)
910b57cec5SDimitry Andric       return GetConstCStringWithLength(cstr, strlen(cstr));
920b57cec5SDimitry Andric     return nullptr;
930b57cec5SDimitry Andric   }
940b57cec5SDimitry Andric 
GetConstCStringWithLength(const char * cstr,size_t cstr_len)950b57cec5SDimitry Andric   const char *GetConstCStringWithLength(const char *cstr, size_t cstr_len) {
960b57cec5SDimitry Andric     if (cstr != nullptr)
970b57cec5SDimitry Andric       return GetConstCStringWithStringRef(llvm::StringRef(cstr, cstr_len));
980b57cec5SDimitry Andric     return nullptr;
990b57cec5SDimitry Andric   }
1000b57cec5SDimitry Andric 
GetConstCStringWithStringRef(const llvm::StringRef & string_ref)1010b57cec5SDimitry Andric   const char *GetConstCStringWithStringRef(const llvm::StringRef &string_ref) {
1020b57cec5SDimitry Andric     if (string_ref.data()) {
1030b57cec5SDimitry Andric       const uint8_t h = hash(string_ref);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric       {
1060b57cec5SDimitry Andric         llvm::sys::SmartScopedReader<false> rlock(m_string_pools[h].m_mutex);
1070b57cec5SDimitry Andric         auto it = m_string_pools[h].m_string_map.find(string_ref);
1080b57cec5SDimitry Andric         if (it != m_string_pools[h].m_string_map.end())
1090b57cec5SDimitry Andric           return it->getKeyData();
1100b57cec5SDimitry Andric       }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric       llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
1130b57cec5SDimitry Andric       StringPoolEntryType &entry =
1140b57cec5SDimitry Andric           *m_string_pools[h]
1150b57cec5SDimitry Andric                .m_string_map.insert(std::make_pair(string_ref, nullptr))
1160b57cec5SDimitry Andric                .first;
1170b57cec5SDimitry Andric       return entry.getKeyData();
1180b57cec5SDimitry Andric     }
1190b57cec5SDimitry Andric     return nullptr;
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   const char *
GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled,const char * mangled_ccstr)1230b57cec5SDimitry Andric   GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled,
1240b57cec5SDimitry Andric                                           const char *mangled_ccstr) {
1250b57cec5SDimitry Andric     const char *demangled_ccstr = nullptr;
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric     {
1280b57cec5SDimitry Andric       const uint8_t h = hash(demangled);
1290b57cec5SDimitry Andric       llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric       // Make or update string pool entry with the mangled counterpart
1320b57cec5SDimitry Andric       StringPool &map = m_string_pools[h].m_string_map;
1330b57cec5SDimitry Andric       StringPoolEntryType &entry = *map.try_emplace(demangled).first;
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric       entry.second = mangled_ccstr;
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric       // Extract the const version of the demangled_cstr
1380b57cec5SDimitry Andric       demangled_ccstr = entry.getKeyData();
1390b57cec5SDimitry Andric     }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric     {
1420b57cec5SDimitry Andric       // Now assign the demangled const string as the counterpart of the
1430b57cec5SDimitry Andric       // mangled const string...
1440b57cec5SDimitry Andric       const uint8_t h = hash(llvm::StringRef(mangled_ccstr));
1450b57cec5SDimitry Andric       llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
1460b57cec5SDimitry Andric       GetStringMapEntryFromKeyData(mangled_ccstr).setValue(demangled_ccstr);
1470b57cec5SDimitry Andric     }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric     // Return the constant demangled C string
1500b57cec5SDimitry Andric     return demangled_ccstr;
1510b57cec5SDimitry Andric   }
1520b57cec5SDimitry Andric 
GetConstTrimmedCStringWithLength(const char * cstr,size_t cstr_len)1530b57cec5SDimitry Andric   const char *GetConstTrimmedCStringWithLength(const char *cstr,
1540b57cec5SDimitry Andric                                                size_t cstr_len) {
1550b57cec5SDimitry Andric     if (cstr != nullptr) {
1560b57cec5SDimitry Andric       const size_t trimmed_len = strnlen(cstr, cstr_len);
1570b57cec5SDimitry Andric       return GetConstCStringWithLength(cstr, trimmed_len);
1580b57cec5SDimitry Andric     }
1590b57cec5SDimitry Andric     return nullptr;
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   // Return the size in bytes that this object and any items in its collection
1630b57cec5SDimitry Andric   // of uniqued strings + data count values takes in memory.
MemorySize() const1640b57cec5SDimitry Andric   size_t MemorySize() const {
1650b57cec5SDimitry Andric     size_t mem_size = sizeof(Pool);
1660b57cec5SDimitry Andric     for (const auto &pool : m_string_pools) {
1670b57cec5SDimitry Andric       llvm::sys::SmartScopedReader<false> rlock(pool.m_mutex);
1680b57cec5SDimitry Andric       for (const auto &entry : pool.m_string_map)
1690b57cec5SDimitry Andric         mem_size += sizeof(StringPoolEntryType) + entry.getKey().size();
1700b57cec5SDimitry Andric     }
1710b57cec5SDimitry Andric     return mem_size;
1720b57cec5SDimitry Andric   }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric protected:
hash(const llvm::StringRef & s) const1750b57cec5SDimitry Andric   uint8_t hash(const llvm::StringRef &s) const {
1760b57cec5SDimitry Andric     uint32_t h = llvm::djbHash(s);
1770b57cec5SDimitry Andric     return ((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff;
1780b57cec5SDimitry Andric   }
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   struct PoolEntry {
1810b57cec5SDimitry Andric     mutable llvm::sys::SmartRWMutex<false> m_mutex;
1820b57cec5SDimitry Andric     StringPool m_string_map;
1830b57cec5SDimitry Andric   };
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   std::array<PoolEntry, 256> m_string_pools;
1860b57cec5SDimitry Andric };
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric // Frameworks and dylibs aren't supposed to have global C++ initializers so we
1890b57cec5SDimitry Andric // hide the string pool in a static function so that it will get initialized on
1900b57cec5SDimitry Andric // the first call to this static function.
1910b57cec5SDimitry Andric //
1920b57cec5SDimitry Andric // Note, for now we make the string pool a pointer to the pool, because we
1930b57cec5SDimitry Andric // can't guarantee that some objects won't get destroyed after the global
1940b57cec5SDimitry Andric // destructor chain is run, and trying to make sure no destructors touch
1950b57cec5SDimitry Andric // ConstStrings is difficult.  So we leak the pool instead.
StringPool()1960b57cec5SDimitry Andric static Pool &StringPool() {
1970b57cec5SDimitry Andric   static llvm::once_flag g_pool_initialization_flag;
1980b57cec5SDimitry Andric   static Pool *g_string_pool = nullptr;
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric   llvm::call_once(g_pool_initialization_flag,
2010b57cec5SDimitry Andric                  []() { g_string_pool = new Pool(); });
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   return *g_string_pool;
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric 
ConstString(const char * cstr)2060b57cec5SDimitry Andric ConstString::ConstString(const char *cstr)
2070b57cec5SDimitry Andric     : m_string(StringPool().GetConstCString(cstr)) {}
2080b57cec5SDimitry Andric 
ConstString(const char * cstr,size_t cstr_len)2090b57cec5SDimitry Andric ConstString::ConstString(const char *cstr, size_t cstr_len)
2100b57cec5SDimitry Andric     : m_string(StringPool().GetConstCStringWithLength(cstr, cstr_len)) {}
2110b57cec5SDimitry Andric 
ConstString(const llvm::StringRef & s)2120b57cec5SDimitry Andric ConstString::ConstString(const llvm::StringRef &s)
2130b57cec5SDimitry Andric     : m_string(StringPool().GetConstCStringWithStringRef(s)) {}
2140b57cec5SDimitry Andric 
operator <(ConstString rhs) const2150b57cec5SDimitry Andric bool ConstString::operator<(ConstString rhs) const {
2160b57cec5SDimitry Andric   if (m_string == rhs.m_string)
2170b57cec5SDimitry Andric     return false;
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   llvm::StringRef lhs_string_ref(GetStringRef());
2200b57cec5SDimitry Andric   llvm::StringRef rhs_string_ref(rhs.GetStringRef());
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   // If both have valid C strings, then return the comparison
2230b57cec5SDimitry Andric   if (lhs_string_ref.data() && rhs_string_ref.data())
2240b57cec5SDimitry Andric     return lhs_string_ref < rhs_string_ref;
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   // Else one of them was nullptr, so if LHS is nullptr then it is less than
2270b57cec5SDimitry Andric   return lhs_string_ref.data() == nullptr;
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric 
operator <<(Stream & s,ConstString str)2300b57cec5SDimitry Andric Stream &lldb_private::operator<<(Stream &s, ConstString str) {
2310b57cec5SDimitry Andric   const char *cstr = str.GetCString();
2320b57cec5SDimitry Andric   if (cstr != nullptr)
2330b57cec5SDimitry Andric     s << cstr;
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   return s;
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric 
GetLength() const2380b57cec5SDimitry Andric size_t ConstString::GetLength() const {
2390b57cec5SDimitry Andric   return Pool::GetConstCStringLength(m_string);
2400b57cec5SDimitry Andric }
2410b57cec5SDimitry Andric 
Equals(ConstString lhs,ConstString rhs,const bool case_sensitive)2420b57cec5SDimitry Andric bool ConstString::Equals(ConstString lhs, ConstString rhs,
2430b57cec5SDimitry Andric                          const bool case_sensitive) {
2440b57cec5SDimitry Andric   if (lhs.m_string == rhs.m_string)
2450b57cec5SDimitry Andric     return true;
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   // Since the pointers weren't equal, and identical ConstStrings always have
2480b57cec5SDimitry Andric   // identical pointers, the result must be false for case sensitive equality
2490b57cec5SDimitry Andric   // test.
2500b57cec5SDimitry Andric   if (case_sensitive)
2510b57cec5SDimitry Andric     return false;
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric   // perform case insensitive equality test
2540b57cec5SDimitry Andric   llvm::StringRef lhs_string_ref(lhs.GetStringRef());
2550b57cec5SDimitry Andric   llvm::StringRef rhs_string_ref(rhs.GetStringRef());
256*5f7ddb14SDimitry Andric   return lhs_string_ref.equals_insensitive(rhs_string_ref);
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric 
Compare(ConstString lhs,ConstString rhs,const bool case_sensitive)2590b57cec5SDimitry Andric int ConstString::Compare(ConstString lhs, ConstString rhs,
2600b57cec5SDimitry Andric                          const bool case_sensitive) {
2610b57cec5SDimitry Andric   // If the iterators are the same, this is the same string
2620b57cec5SDimitry Andric   const char *lhs_cstr = lhs.m_string;
2630b57cec5SDimitry Andric   const char *rhs_cstr = rhs.m_string;
2640b57cec5SDimitry Andric   if (lhs_cstr == rhs_cstr)
2650b57cec5SDimitry Andric     return 0;
2660b57cec5SDimitry Andric   if (lhs_cstr && rhs_cstr) {
2670b57cec5SDimitry Andric     llvm::StringRef lhs_string_ref(lhs.GetStringRef());
2680b57cec5SDimitry Andric     llvm::StringRef rhs_string_ref(rhs.GetStringRef());
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric     if (case_sensitive) {
2710b57cec5SDimitry Andric       return lhs_string_ref.compare(rhs_string_ref);
2720b57cec5SDimitry Andric     } else {
273*5f7ddb14SDimitry Andric       return lhs_string_ref.compare_insensitive(rhs_string_ref);
2740b57cec5SDimitry Andric     }
2750b57cec5SDimitry Andric   }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   if (lhs_cstr)
2780b57cec5SDimitry Andric     return +1; // LHS isn't nullptr but RHS is
2790b57cec5SDimitry Andric   else
2800b57cec5SDimitry Andric     return -1; // LHS is nullptr but RHS isn't
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric 
Dump(Stream * s,const char * fail_value) const2830b57cec5SDimitry Andric void ConstString::Dump(Stream *s, const char *fail_value) const {
2840b57cec5SDimitry Andric   if (s != nullptr) {
2850b57cec5SDimitry Andric     const char *cstr = AsCString(fail_value);
2860b57cec5SDimitry Andric     if (cstr != nullptr)
2870b57cec5SDimitry Andric       s->PutCString(cstr);
2880b57cec5SDimitry Andric   }
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
DumpDebug(Stream * s) const2910b57cec5SDimitry Andric void ConstString::DumpDebug(Stream *s) const {
2920b57cec5SDimitry Andric   const char *cstr = GetCString();
2930b57cec5SDimitry Andric   size_t cstr_len = GetLength();
2940b57cec5SDimitry Andric   // Only print the parens if we have a non-nullptr string
2950b57cec5SDimitry Andric   const char *parens = cstr ? "\"" : "";
2960b57cec5SDimitry Andric   s->Printf("%*p: ConstString, string = %s%s%s, length = %" PRIu64,
2970b57cec5SDimitry Andric             static_cast<int>(sizeof(void *) * 2),
2980b57cec5SDimitry Andric             static_cast<const void *>(this), parens, cstr, parens,
2990b57cec5SDimitry Andric             static_cast<uint64_t>(cstr_len));
3000b57cec5SDimitry Andric }
3010b57cec5SDimitry Andric 
SetCString(const char * cstr)3020b57cec5SDimitry Andric void ConstString::SetCString(const char *cstr) {
3030b57cec5SDimitry Andric   m_string = StringPool().GetConstCString(cstr);
3040b57cec5SDimitry Andric }
3050b57cec5SDimitry Andric 
SetString(const llvm::StringRef & s)3060b57cec5SDimitry Andric void ConstString::SetString(const llvm::StringRef &s) {
3070b57cec5SDimitry Andric   m_string = StringPool().GetConstCStringWithLength(s.data(), s.size());
3080b57cec5SDimitry Andric }
3090b57cec5SDimitry Andric 
SetStringWithMangledCounterpart(llvm::StringRef demangled,ConstString mangled)3100b57cec5SDimitry Andric void ConstString::SetStringWithMangledCounterpart(llvm::StringRef demangled,
3110b57cec5SDimitry Andric                                                   ConstString mangled) {
3120b57cec5SDimitry Andric   m_string = StringPool().GetConstCStringAndSetMangledCounterPart(
3130b57cec5SDimitry Andric       demangled, mangled.m_string);
3140b57cec5SDimitry Andric }
3150b57cec5SDimitry Andric 
GetMangledCounterpart(ConstString & counterpart) const3160b57cec5SDimitry Andric bool ConstString::GetMangledCounterpart(ConstString &counterpart) const {
3170b57cec5SDimitry Andric   counterpart.m_string = StringPool().GetMangledCounterpart(m_string);
3180b57cec5SDimitry Andric   return (bool)counterpart;
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric 
SetCStringWithLength(const char * cstr,size_t cstr_len)3210b57cec5SDimitry Andric void ConstString::SetCStringWithLength(const char *cstr, size_t cstr_len) {
3220b57cec5SDimitry Andric   m_string = StringPool().GetConstCStringWithLength(cstr, cstr_len);
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric 
SetTrimmedCStringWithLength(const char * cstr,size_t cstr_len)3250b57cec5SDimitry Andric void ConstString::SetTrimmedCStringWithLength(const char *cstr,
3260b57cec5SDimitry Andric                                               size_t cstr_len) {
3270b57cec5SDimitry Andric   m_string = StringPool().GetConstTrimmedCStringWithLength(cstr, cstr_len);
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric 
StaticMemorySize()3300b57cec5SDimitry Andric size_t ConstString::StaticMemorySize() {
3310b57cec5SDimitry Andric   // Get the size of the static string pool
3320b57cec5SDimitry Andric   return StringPool().MemorySize();
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
format(const ConstString & CS,llvm::raw_ostream & OS,llvm::StringRef Options)3350b57cec5SDimitry Andric void llvm::format_provider<ConstString>::format(const ConstString &CS,
3360b57cec5SDimitry Andric                                                 llvm::raw_ostream &OS,
3370b57cec5SDimitry Andric                                                 llvm::StringRef Options) {
3385ffd83dbSDimitry Andric   format_provider<StringRef>::format(CS.GetStringRef(), OS, Options);
3395ffd83dbSDimitry Andric }
3405ffd83dbSDimitry Andric 
output(const ConstString & Val,void *,raw_ostream & Out)3415ffd83dbSDimitry Andric void llvm::yaml::ScalarTraits<ConstString>::output(const ConstString &Val,
3425ffd83dbSDimitry Andric                                                    void *, raw_ostream &Out) {
3435ffd83dbSDimitry Andric   Out << Val.GetStringRef();
3445ffd83dbSDimitry Andric }
3455ffd83dbSDimitry Andric 
3465ffd83dbSDimitry Andric llvm::StringRef
input(llvm::StringRef Scalar,void *,ConstString & Val)3475ffd83dbSDimitry Andric llvm::yaml::ScalarTraits<ConstString>::input(llvm::StringRef Scalar, void *,
3485ffd83dbSDimitry Andric                                              ConstString &Val) {
3495ffd83dbSDimitry Andric   Val = ConstString(Scalar);
3505ffd83dbSDimitry Andric   return {};
3510b57cec5SDimitry Andric }
352