1 //===- StringPool.h ---------------------------------------------*- C++ -*-===//
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 LLVM_DWARFLINKER_STRINGPOOL_H
10 #define LLVM_DWARFLINKER_STRINGPOOL_H
11 
12 #include "llvm/ADT/ConcurrentHashtable.h"
13 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/PerThreadBumpPtrAllocator.h"
16 #include <string_view>
17 
18 namespace llvm {
19 namespace dwarf_linker {
20 
21 /// StringEntry keeps data of the string: the length, external offset
22 /// and a string body which is placed right after StringEntry.
23 using StringEntry = StringMapEntry<std::nullopt_t>;
24 
25 class StringPoolEntryInfo {
26 public:
27   /// \returns Hash value for the specified \p Key.
28   static inline uint64_t getHashValue(const StringRef &Key) {
29     return xxh3_64bits(Key);
30   }
31 
32   /// \returns true if both \p LHS and \p RHS are equal.
33   static inline bool isEqual(const StringRef &LHS, const StringRef &RHS) {
34     return LHS == RHS;
35   }
36 
37   /// \returns key for the specified \p KeyData.
38   static inline StringRef getKey(const StringEntry &KeyData) {
39     return KeyData.getKey();
40   }
41 
42   /// \returns newly created object of KeyDataTy type.
43   static inline StringEntry *
44   create(const StringRef &Key,
45          llvm::parallel::PerThreadBumpPtrAllocator &Allocator) {
46     return StringEntry::create(Key, Allocator);
47   }
48 };
49 
50 class StringPool
51     : public ConcurrentHashTableByPtr<StringRef, StringEntry,
52                                       llvm::parallel::PerThreadBumpPtrAllocator,
53                                       StringPoolEntryInfo> {
54 public:
55   StringPool()
56       : ConcurrentHashTableByPtr<StringRef, StringEntry,
57                                  llvm::parallel::PerThreadBumpPtrAllocator,
58                                  StringPoolEntryInfo>(Allocator) {}
59 
60   StringPool(size_t InitialSize)
61       : ConcurrentHashTableByPtr<StringRef, StringEntry,
62                                  llvm::parallel::PerThreadBumpPtrAllocator,
63                                  StringPoolEntryInfo>(Allocator, InitialSize) {}
64 
65   llvm::parallel::PerThreadBumpPtrAllocator &getAllocatorRef() {
66     return Allocator;
67   }
68 
69   void clear() { Allocator.Reset(); }
70 
71 private:
72   llvm::parallel::PerThreadBumpPtrAllocator Allocator;
73 };
74 
75 } // namespace dwarf_linker
76 } // end namespace llvm
77 
78 #endif // LLVM_DWARFLINKER_STRINGPOOL_H
79