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_DWARFLINKERPARALLEL_STRINGPOOL_H
10 #define LLVM_DWARFLINKERPARALLEL_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 dwarflinker_parallel {
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.
getHashValue(const StringRef & 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.
isEqual(const StringRef & LHS,const StringRef & RHS)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.
getKey(const StringEntry & 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 *
create(const StringRef & Key,parallel::PerThreadBumpPtrAllocator & Allocator)44   create(const StringRef &Key, parallel::PerThreadBumpPtrAllocator &Allocator) {
45     return StringEntry::create(Key, Allocator);
46   }
47 };
48 
49 class StringPool
50     : public ConcurrentHashTableByPtr<StringRef, StringEntry,
51                                       parallel::PerThreadBumpPtrAllocator,
52                                       StringPoolEntryInfo> {
53 public:
StringPool()54   StringPool()
55       : ConcurrentHashTableByPtr<StringRef, StringEntry,
56                                  parallel::PerThreadBumpPtrAllocator,
57                                  StringPoolEntryInfo>(Allocator) {}
58 
StringPool(size_t InitialSize)59   StringPool(size_t InitialSize)
60       : ConcurrentHashTableByPtr<StringRef, StringEntry,
61                                  parallel::PerThreadBumpPtrAllocator,
62                                  StringPoolEntryInfo>(Allocator, InitialSize) {}
63 
getAllocatorRef()64   parallel::PerThreadBumpPtrAllocator &getAllocatorRef() { return Allocator; }
65 
clear()66   void clear() { Allocator.Reset(); }
67 
68 private:
69   parallel::PerThreadBumpPtrAllocator Allocator;
70 };
71 
72 } // end of namespace dwarflinker_parallel
73 } // end namespace llvm
74 
75 #endif // LLVM_DWARFLINKERPARALLEL_STRINGPOOL_H
76