LLDB mainline
StringPool.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
9#ifndef LLDB_UTILITY_STRINGPOOL_H
10#define LLDB_UTILITY_STRINGPOOL_H
11
13
14#include "llvm/ADT/StringRef.h"
15
16#include <cstddef>
17#include <memory>
18
19namespace lldb_private {
20
21class StringPoolRef;
22
23/// A thread-safe pool of interned, null-terminated strings. A string returned
24/// by a pool stays valid for the lifetime of that pool.
26public:
27 StringPool();
29
30 StringPool(const StringPool &) = delete;
31 StringPool &operator=(const StringPool &) = delete;
32
33 /// The pool backing ConstString. It is never destroyed.
34 static StringPool &GetGlobal();
35
36 /// Set up and tear down the system pool, for strings that are not owned by a
37 /// Debugger.
38 static void Initialize();
39 static void Terminate();
40
41 /// The system pool. Only valid between Initialize and Terminate.
43
44 /// Returns the pooled copy of \p str, or nullptr if \p str has no data.
45 const char *Intern(llvm::StringRef str);
46
47 const char *GetConstCString(const char *cstr);
48 const char *GetConstCStringWithLength(const char *cstr, size_t cstr_len);
49 const char *GetConstTrimmedCStringWithLength(const char *cstr,
50 size_t cstr_len);
51
52 /// Interns \p demangled and links it with the already interned \p mangled.
53 const char *GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled,
54 llvm::StringRef mangled);
55 const char *GetMangledCounterpart(llvm::StringRef str);
56
57 /// Length of a string returned by any pool.
58 static size_t GetConstCStringLength(const char *ccstr);
59
61
62private:
63 struct PoolEntry;
64
65 PoolEntry &selectPool(uint32_t hash);
66 PoolEntry &selectPool(llvm::StringRef str);
67
68 std::unique_ptr<PoolEntry[]> m_string_pools;
69};
70
71/// A handle to a StringPool that does not own it.
73public:
74 /// Refers to the global pool.
75 StringPoolRef() : m_pool(&StringPool::GetGlobal()) {}
76 explicit StringPoolRef(StringPool &pool) : m_pool(&pool) {}
77
78 const char *Intern(llvm::StringRef str) const { return m_pool->Intern(str); }
79
80 /// Like Intern, but an empty string yields nullptr.
81 const char *InternNonEmpty(llvm::StringRef str) const {
82 return str.empty() ? nullptr : Intern(str);
83 }
84
85private:
87};
88
89} // namespace lldb_private
90
91#endif // LLDB_UTILITY_STRINGPOOL_H
A handle to a StringPool that does not own it.
Definition StringPool.h:72
const char * Intern(llvm::StringRef str) const
Definition StringPool.h:78
StringPoolRef(StringPool &pool)
Definition StringPool.h:76
StringPoolRef()
Refers to the global pool.
Definition StringPool.h:75
const char * InternNonEmpty(llvm::StringRef str) const
Like Intern, but an empty string yields nullptr.
Definition StringPool.h:81
A thread-safe pool of interned, null-terminated strings.
Definition StringPool.h:25
std::unique_ptr< PoolEntry[]> m_string_pools
Definition StringPool.h:68
const char * GetConstCString(const char *cstr)
PoolEntry & selectPool(uint32_t hash)
const char * Intern(llvm::StringRef str)
Returns the pooled copy of str, or nullptr if str has no data.
const char * GetMangledCounterpart(llvm::StringRef str)
static StringPoolRef GetSystemPool()
The system pool. Only valid between Initialize and Terminate.
ConstString::MemoryStats GetMemoryStats() const
const char * GetConstCStringWithLength(const char *cstr, size_t cstr_len)
StringPool & operator=(const StringPool &)=delete
static StringPool & GetGlobal()
The pool backing ConstString. It is never destroyed.
static size_t GetConstCStringLength(const char *ccstr)
Length of a string returned by any pool.
static void Initialize()
Set up and tear down the system pool, for strings that are not owned by a Debugger.
const char * GetConstTrimmedCStringWithLength(const char *cstr, size_t cstr_len)
StringPool(const StringPool &)=delete
const char * GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled, llvm::StringRef mangled)
Interns demangled and links it with the already interned mangled.
A class that represents a running process on the host machine.