Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ConstString.cpp -----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | #include "lldb/Core/ConstString.h" |
| 10 | #include "lldb/Core/Stream.h" |
| 11 | #include "lldb/Host/Mutex.h" |
| 12 | #include "llvm/ADT/StringMap.h" |
| 13 | |
| 14 | using namespace lldb_private; |
| 15 | |
| 16 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | class Pool |
| 18 | { |
| 19 | public: |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 20 | typedef const char * StringPoolValueType; |
| 21 | typedef llvm::StringMap<StringPoolValueType, llvm::BumpPtrAllocator> StringPool; |
| 22 | typedef llvm::StringMapEntry<StringPoolValueType> StringPoolEntryType; |
| 23 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | //------------------------------------------------------------------ |
| 25 | // Default constructor |
| 26 | // |
| 27 | // Initialize the member variables and create the empty string. |
| 28 | //------------------------------------------------------------------ |
| 29 | Pool () : |
| 30 | m_mutex (Mutex::eMutexTypeRecursive), |
| 31 | m_string_map () |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | //------------------------------------------------------------------ |
| 36 | // Destructor |
| 37 | //------------------------------------------------------------------ |
| 38 | ~Pool () |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 43 | static StringPoolEntryType & |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | GetStringMapEntryFromKeyData (const char *keyData) |
| 45 | { |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 46 | char *ptr = const_cast<char*>(keyData) - sizeof (StringPoolEntryType); |
| 47 | return *reinterpret_cast<StringPoolEntryType*>(ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | size_t |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 51 | GetConstCStringLength (const char *ccstr) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | { |
| 53 | if (ccstr) |
| 54 | { |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 55 | const StringPoolEntryType&entry = GetStringMapEntryFromKeyData (ccstr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | return entry.getKey().size(); |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 61 | StringPoolValueType |
| 62 | GetMangledCounterpart (const char *ccstr) const |
| 63 | { |
| 64 | if (ccstr) |
| 65 | return GetStringMapEntryFromKeyData (ccstr).getValue(); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | bool |
| 70 | SetMangledCounterparts (const char *key_ccstr, const char *value_ccstr) |
| 71 | { |
| 72 | if (key_ccstr && value_ccstr) |
| 73 | { |
| 74 | GetStringMapEntryFromKeyData (key_ccstr).setValue(value_ccstr); |
| 75 | GetStringMapEntryFromKeyData (value_ccstr).setValue(key_ccstr); |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | const char * |
| 82 | GetConstCString (const char *cstr) |
| 83 | { |
| 84 | if (cstr) |
Benjamin Kramer | eb9165c | 2010-06-22 15:28:34 +0000 | [diff] [blame] | 85 | return GetConstCStringWithLength (cstr, strlen (cstr)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | const char * |
| 90 | GetConstCStringWithLength (const char *cstr, int cstr_len) |
| 91 | { |
| 92 | if (cstr) |
| 93 | { |
| 94 | Mutex::Locker locker (m_mutex); |
| 95 | llvm::StringRef string_ref (cstr, cstr_len); |
Greg Clayton | 40df35e | 2011-06-10 00:00:19 +0000 | [diff] [blame] | 96 | StringPoolEntryType& entry = m_string_map.GetOrCreateValue (string_ref, (StringPoolValueType)NULL); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 97 | return entry.getKeyData(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | } |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | const char * |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame^] | 103 | GetConstCStringWithStringRef (const llvm::StringRef &string_ref) |
| 104 | { |
| 105 | if (string_ref.data()) |
| 106 | { |
| 107 | Mutex::Locker locker (m_mutex); |
| 108 | StringPoolEntryType& entry = m_string_map.GetOrCreateValue (string_ref, (StringPoolValueType)NULL); |
| 109 | return entry.getKeyData(); |
| 110 | } |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | const char * |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 115 | GetConstCStringAndSetMangledCounterPart (const char *demangled_cstr, const char *mangled_ccstr) |
| 116 | { |
| 117 | if (demangled_cstr) |
| 118 | { |
| 119 | Mutex::Locker locker (m_mutex); |
| 120 | // Make string pool entry with the mangled counterpart already set |
| 121 | StringPoolEntryType& entry = m_string_map.GetOrCreateValue (llvm::StringRef (demangled_cstr), mangled_ccstr); |
| 122 | |
| 123 | // Extract the const version of the demangled_cstr |
| 124 | const char *demangled_ccstr = entry.getKeyData(); |
| 125 | // Now assign the demangled const string as the counterpart of the |
| 126 | // mangled const string... |
| 127 | GetStringMapEntryFromKeyData (mangled_ccstr).setValue(demangled_ccstr); |
| 128 | // Return the constant demangled C string |
| 129 | return demangled_ccstr; |
| 130 | } |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | const char * |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | GetConstTrimmedCStringWithLength (const char *cstr, int cstr_len) |
| 136 | { |
| 137 | if (cstr) |
| 138 | { |
Benjamin Kramer | eb9165c | 2010-06-22 15:28:34 +0000 | [diff] [blame] | 139 | int trimmed_len = std::min<int> (strlen (cstr), cstr_len); |
| 140 | return GetConstCStringWithLength (cstr, trimmed_len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | //------------------------------------------------------------------ |
| 146 | // Return the size in bytes that this object and any items in its |
Greg Clayton | 2475664 | 2011-09-12 03:55:58 +0000 | [diff] [blame] | 147 | // collection of uniqued strings + data count values takes in |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | // memory. |
| 149 | //------------------------------------------------------------------ |
| 150 | size_t |
| 151 | MemorySize() const |
| 152 | { |
| 153 | Mutex::Locker locker (m_mutex); |
| 154 | size_t mem_size = sizeof(Pool); |
| 155 | const_iterator end = m_string_map.end(); |
| 156 | for (const_iterator pos = m_string_map.begin(); pos != end; ++pos) |
| 157 | { |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 158 | mem_size += sizeof(StringPoolEntryType) + pos->getKey().size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | } |
| 160 | return mem_size; |
| 161 | } |
| 162 | |
| 163 | protected: |
| 164 | //------------------------------------------------------------------ |
| 165 | // Typedefs |
| 166 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | typedef StringPool::iterator iterator; |
| 168 | typedef StringPool::const_iterator const_iterator; |
| 169 | |
| 170 | //------------------------------------------------------------------ |
| 171 | // Member variables |
| 172 | //------------------------------------------------------------------ |
| 173 | mutable Mutex m_mutex; |
| 174 | StringPool m_string_map; |
| 175 | }; |
| 176 | |
| 177 | //---------------------------------------------------------------------- |
| 178 | // Frameworks and dylibs aren't supposed to have global C++ |
| 179 | // initializers so we hide the string pool in a static function so |
| 180 | // that it will get initialized on the first call to this static |
| 181 | // function. |
Jim Ingham | 5ce45fd | 2012-05-09 18:37:10 +0000 | [diff] [blame] | 182 | // |
| 183 | // Note, for now we make the string pool a pointer to the pool, because |
| 184 | // we can't guarantee that some objects won't get destroyed after the |
| 185 | // global destructor chain is run, and trying to make sure no destructors |
| 186 | // touch ConstStrings is difficult. So we leak the pool instead. |
| 187 | // |
| 188 | // FIXME: If we are going to keep it this way we should come up with some |
| 189 | // abstraction to "pthread_once" so we don't have to check the pointer |
| 190 | // every time. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 191 | //---------------------------------------------------------------------- |
| 192 | static Pool & |
| 193 | StringPool() |
| 194 | { |
Jim Ingham | 5ce45fd | 2012-05-09 18:37:10 +0000 | [diff] [blame] | 195 | static Mutex g_pool_initialization_mutex; |
| 196 | static Pool *g_string_pool = NULL; |
| 197 | |
| 198 | if (g_string_pool == NULL) |
| 199 | { |
| 200 | Mutex::Locker initialization_locker(g_pool_initialization_mutex); |
| 201 | if (g_string_pool == NULL) |
| 202 | { |
| 203 | g_string_pool = new Pool(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return *g_string_pool; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | ConstString::ConstString (const char *cstr) : |
| 211 | m_string (StringPool().GetConstCString (cstr)) |
| 212 | { |
| 213 | } |
| 214 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 215 | ConstString::ConstString (const char *cstr, size_t cstr_len) : |
| 216 | m_string (StringPool().GetConstCStringWithLength (cstr, cstr_len)) |
| 217 | { |
| 218 | } |
| 219 | |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 220 | ConstString::ConstString (const llvm::StringRef &s) : |
| 221 | m_string (StringPool().GetConstCStringWithLength (s.data(), s.size())) |
| 222 | { |
| 223 | } |
| 224 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 225 | bool |
| 226 | ConstString::operator < (const ConstString& rhs) const |
| 227 | { |
| 228 | if (m_string == rhs.m_string) |
| 229 | return false; |
| 230 | |
| 231 | llvm::StringRef lhs_string_ref (m_string, StringPool().GetConstCStringLength (m_string)); |
| 232 | llvm::StringRef rhs_string_ref (rhs.m_string, StringPool().GetConstCStringLength (rhs.m_string)); |
| 233 | |
| 234 | // If both have valid C strings, then return the comparison |
| 235 | if (lhs_string_ref.data() && rhs_string_ref.data()) |
| 236 | return lhs_string_ref < rhs_string_ref; |
| 237 | |
| 238 | // Else one of them was NULL, so if LHS is NULL then it is less than |
| 239 | return lhs_string_ref.data() == NULL; |
| 240 | } |
| 241 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | Stream& |
| 243 | lldb_private::operator << (Stream& s, const ConstString& str) |
| 244 | { |
| 245 | const char *cstr = str.GetCString(); |
| 246 | if (cstr) |
| 247 | s << cstr; |
| 248 | |
| 249 | return s; |
| 250 | } |
| 251 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | size_t |
| 253 | ConstString::GetLength () const |
| 254 | { |
| 255 | return StringPool().GetConstCStringLength (m_string); |
| 256 | } |
| 257 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | int |
| 259 | ConstString::Compare (const ConstString& lhs, const ConstString& rhs) |
| 260 | { |
| 261 | // If the iterators are the same, this is the same string |
| 262 | register const char *lhs_cstr = lhs.m_string; |
| 263 | register const char *rhs_cstr = rhs.m_string; |
| 264 | if (lhs_cstr == rhs_cstr) |
| 265 | return 0; |
| 266 | if (lhs_cstr && rhs_cstr) |
| 267 | { |
| 268 | llvm::StringRef lhs_string_ref (lhs_cstr, StringPool().GetConstCStringLength (lhs_cstr)); |
| 269 | llvm::StringRef rhs_string_ref (rhs_cstr, StringPool().GetConstCStringLength (rhs_cstr)); |
| 270 | return lhs_string_ref.compare(rhs_string_ref); |
| 271 | } |
| 272 | |
| 273 | if (lhs_cstr) |
| 274 | return +1; // LHS isn't NULL but RHS is |
| 275 | else |
| 276 | return -1; // LHS is NULL but RHS isn't |
| 277 | } |
| 278 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 279 | void |
| 280 | ConstString::Dump(Stream *s, const char *fail_value) const |
| 281 | { |
| 282 | const char *cstr = AsCString (fail_value); |
| 283 | if (cstr) |
| 284 | s->PutCString (cstr); |
| 285 | } |
| 286 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 287 | void |
| 288 | ConstString::DumpDebug(Stream *s) const |
| 289 | { |
| 290 | const char *cstr = GetCString (); |
| 291 | size_t cstr_len = GetLength(); |
| 292 | // Only print the parens if we have a non-NULL string |
| 293 | const char *parens = cstr ? "\"" : ""; |
| 294 | s->Printf("%*p: ConstString, string = %s%s%s, length = %zu", (int)sizeof(void*) * 2, this, parens, cstr, parens, cstr_len); |
| 295 | } |
| 296 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 297 | void |
| 298 | ConstString::SetCString (const char *cstr) |
| 299 | { |
| 300 | m_string = StringPool().GetConstCString (cstr); |
| 301 | } |
| 302 | |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 303 | void |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 304 | ConstString::SetString (const llvm::StringRef &s) |
| 305 | { |
| 306 | m_string = StringPool().GetConstCStringWithLength (s.data(), s.size()); |
| 307 | } |
| 308 | |
| 309 | void |
Greg Clayton | c3ae1ce | 2011-06-09 22:34:34 +0000 | [diff] [blame] | 310 | ConstString::SetCStringWithMangledCounterpart (const char *demangled, const ConstString &mangled) |
| 311 | { |
| 312 | m_string = StringPool().GetConstCStringAndSetMangledCounterPart (demangled, mangled.m_string); |
| 313 | } |
| 314 | |
| 315 | bool |
| 316 | ConstString::GetMangledCounterpart (ConstString &counterpart) const |
| 317 | { |
| 318 | counterpart.m_string = StringPool().GetMangledCounterpart(m_string); |
| 319 | return counterpart; |
| 320 | } |
| 321 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 322 | void |
| 323 | ConstString::SetCStringWithLength (const char *cstr, size_t cstr_len) |
| 324 | { |
| 325 | m_string = StringPool().GetConstCStringWithLength(cstr, cstr_len); |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | ConstString::SetTrimmedCStringWithLength (const char *cstr, size_t cstr_len) |
| 330 | { |
| 331 | m_string = StringPool().GetConstTrimmedCStringWithLength (cstr, cstr_len); |
| 332 | } |
| 333 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 334 | size_t |
| 335 | ConstString::StaticMemorySize() |
| 336 | { |
| 337 | // Get the size of the static string pool |
| 338 | return StringPool().MemorySize(); |
| 339 | } |