blob: 6a2ac3ffeda8dc056f991b1fa98241ee4a138e60 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
14using namespace lldb_private;
15
16
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017class Pool
18{
19public:
Greg Claytonc3ae1ce2011-06-09 22:34:34 +000020 typedef const char * StringPoolValueType;
21 typedef llvm::StringMap<StringPoolValueType, llvm::BumpPtrAllocator> StringPool;
22 typedef llvm::StringMapEntry<StringPoolValueType> StringPoolEntryType;
23
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024 //------------------------------------------------------------------
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 Claytonc3ae1ce2011-06-09 22:34:34 +000043 static StringPoolEntryType &
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044 GetStringMapEntryFromKeyData (const char *keyData)
45 {
Greg Claytonc3ae1ce2011-06-09 22:34:34 +000046 char *ptr = const_cast<char*>(keyData) - sizeof (StringPoolEntryType);
47 return *reinterpret_cast<StringPoolEntryType*>(ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048 }
49
50 size_t
Greg Claytonc3ae1ce2011-06-09 22:34:34 +000051 GetConstCStringLength (const char *ccstr) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052 {
53 if (ccstr)
54 {
Greg Claytonc3ae1ce2011-06-09 22:34:34 +000055 const StringPoolEntryType&entry = GetStringMapEntryFromKeyData (ccstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056 return entry.getKey().size();
57 }
58 return 0;
59 }
60
Greg Claytonc3ae1ce2011-06-09 22:34:34 +000061 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 Lattner30fdc8d2010-06-08 16:52:24 +000081 const char *
82 GetConstCString (const char *cstr)
83 {
84 if (cstr)
Benjamin Kramereb9165c2010-06-22 15:28:34 +000085 return GetConstCStringWithLength (cstr, strlen (cstr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 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 Clayton40df35e2011-06-10 00:00:19 +000096 StringPoolEntryType& entry = m_string_map.GetOrCreateValue (string_ref, (StringPoolValueType)NULL);
Greg Claytonc982c762010-07-09 20:39:50 +000097 return entry.getKeyData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 }
99 return NULL;
100 }
101
102 const char *
Greg Claytonc3ae1ce2011-06-09 22:34:34 +0000103 GetConstCStringAndSetMangledCounterPart (const char *demangled_cstr, const char *mangled_ccstr)
104 {
105 if (demangled_cstr)
106 {
107 Mutex::Locker locker (m_mutex);
108 // Make string pool entry with the mangled counterpart already set
109 StringPoolEntryType& entry = m_string_map.GetOrCreateValue (llvm::StringRef (demangled_cstr), mangled_ccstr);
110
111 // Extract the const version of the demangled_cstr
112 const char *demangled_ccstr = entry.getKeyData();
113 // Now assign the demangled const string as the counterpart of the
114 // mangled const string...
115 GetStringMapEntryFromKeyData (mangled_ccstr).setValue(demangled_ccstr);
116 // Return the constant demangled C string
117 return demangled_ccstr;
118 }
119 return NULL;
120 }
121
122 const char *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 GetConstTrimmedCStringWithLength (const char *cstr, int cstr_len)
124 {
125 if (cstr)
126 {
Benjamin Kramereb9165c2010-06-22 15:28:34 +0000127 int trimmed_len = std::min<int> (strlen (cstr), cstr_len);
128 return GetConstCStringWithLength (cstr, trimmed_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 }
130 return NULL;
131 }
132
133 //------------------------------------------------------------------
134 // Return the size in bytes that this object and any items in its
Greg Clayton24756642011-09-12 03:55:58 +0000135 // collection of uniqued strings + data count values takes in
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136 // memory.
137 //------------------------------------------------------------------
138 size_t
139 MemorySize() const
140 {
141 Mutex::Locker locker (m_mutex);
142 size_t mem_size = sizeof(Pool);
143 const_iterator end = m_string_map.end();
144 for (const_iterator pos = m_string_map.begin(); pos != end; ++pos)
145 {
Greg Claytonc3ae1ce2011-06-09 22:34:34 +0000146 mem_size += sizeof(StringPoolEntryType) + pos->getKey().size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 }
148 return mem_size;
149 }
150
151protected:
152 //------------------------------------------------------------------
153 // Typedefs
154 //------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 typedef StringPool::iterator iterator;
156 typedef StringPool::const_iterator const_iterator;
157
158 //------------------------------------------------------------------
159 // Member variables
160 //------------------------------------------------------------------
161 mutable Mutex m_mutex;
162 StringPool m_string_map;
163};
164
165//----------------------------------------------------------------------
166// Frameworks and dylibs aren't supposed to have global C++
167// initializers so we hide the string pool in a static function so
168// that it will get initialized on the first call to this static
169// function.
170//----------------------------------------------------------------------
171static Pool &
172StringPool()
173{
174 static Pool string_pool;
175 return string_pool;
176}
177
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178ConstString::ConstString (const char *cstr) :
179 m_string (StringPool().GetConstCString (cstr))
180{
181}
182
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183ConstString::ConstString (const char *cstr, size_t cstr_len) :
184 m_string (StringPool().GetConstCStringWithLength (cstr, cstr_len))
185{
186}
187
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188bool
189ConstString::operator < (const ConstString& rhs) const
190{
191 if (m_string == rhs.m_string)
192 return false;
193
194 llvm::StringRef lhs_string_ref (m_string, StringPool().GetConstCStringLength (m_string));
195 llvm::StringRef rhs_string_ref (rhs.m_string, StringPool().GetConstCStringLength (rhs.m_string));
196
197 // If both have valid C strings, then return the comparison
198 if (lhs_string_ref.data() && rhs_string_ref.data())
199 return lhs_string_ref < rhs_string_ref;
200
201 // Else one of them was NULL, so if LHS is NULL then it is less than
202 return lhs_string_ref.data() == NULL;
203}
204
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205Stream&
206lldb_private::operator << (Stream& s, const ConstString& str)
207{
208 const char *cstr = str.GetCString();
209 if (cstr)
210 s << cstr;
211
212 return s;
213}
214
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215size_t
216ConstString::GetLength () const
217{
218 return StringPool().GetConstCStringLength (m_string);
219}
220
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221int
222ConstString::Compare (const ConstString& lhs, const ConstString& rhs)
223{
224 // If the iterators are the same, this is the same string
225 register const char *lhs_cstr = lhs.m_string;
226 register const char *rhs_cstr = rhs.m_string;
227 if (lhs_cstr == rhs_cstr)
228 return 0;
229 if (lhs_cstr && rhs_cstr)
230 {
231 llvm::StringRef lhs_string_ref (lhs_cstr, StringPool().GetConstCStringLength (lhs_cstr));
232 llvm::StringRef rhs_string_ref (rhs_cstr, StringPool().GetConstCStringLength (rhs_cstr));
233 return lhs_string_ref.compare(rhs_string_ref);
234 }
235
236 if (lhs_cstr)
237 return +1; // LHS isn't NULL but RHS is
238 else
239 return -1; // LHS is NULL but RHS isn't
240}
241
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242void
243ConstString::Dump(Stream *s, const char *fail_value) const
244{
245 const char *cstr = AsCString (fail_value);
246 if (cstr)
247 s->PutCString (cstr);
248}
249
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250void
251ConstString::DumpDebug(Stream *s) const
252{
253 const char *cstr = GetCString ();
254 size_t cstr_len = GetLength();
255 // Only print the parens if we have a non-NULL string
256 const char *parens = cstr ? "\"" : "";
257 s->Printf("%*p: ConstString, string = %s%s%s, length = %zu", (int)sizeof(void*) * 2, this, parens, cstr, parens, cstr_len);
258}
259
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260void
261ConstString::SetCString (const char *cstr)
262{
263 m_string = StringPool().GetConstCString (cstr);
264}
265
Greg Claytonc3ae1ce2011-06-09 22:34:34 +0000266void
267ConstString::SetCStringWithMangledCounterpart (const char *demangled, const ConstString &mangled)
268{
269 m_string = StringPool().GetConstCStringAndSetMangledCounterPart (demangled, mangled.m_string);
270}
271
272bool
273ConstString::GetMangledCounterpart (ConstString &counterpart) const
274{
275 counterpart.m_string = StringPool().GetMangledCounterpart(m_string);
276 return counterpart;
277}
278
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279void
280ConstString::SetCStringWithLength (const char *cstr, size_t cstr_len)
281{
282 m_string = StringPool().GetConstCStringWithLength(cstr, cstr_len);
283}
284
285void
286ConstString::SetTrimmedCStringWithLength (const char *cstr, size_t cstr_len)
287{
288 m_string = StringPool().GetConstTrimmedCStringWithLength (cstr, cstr_len);
289}
290
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291size_t
292ConstString::StaticMemorySize()
293{
294 // Get the size of the static string pool
295 return StringPool().MemorySize();
296}