File indexing completed on 2026-05-10 08:43:49
0001 #ifndef LLVM_DWP_DWPSTRINGPOOL_H
0002 #define LLVM_DWP_DWPSTRINGPOOL_H
0003
0004 #include "llvm/ADT/DenseMap.h"
0005 #include "llvm/MC/MCSection.h"
0006 #include "llvm/MC/MCStreamer.h"
0007 #include <cassert>
0008
0009 namespace llvm {
0010 class DWPStringPool {
0011
0012 struct CStrDenseMapInfo {
0013 static inline const char *getEmptyKey() {
0014 return reinterpret_cast<const char *>(~static_cast<uintptr_t>(0));
0015 }
0016 static inline const char *getTombstoneKey() {
0017 return reinterpret_cast<const char *>(~static_cast<uintptr_t>(1));
0018 }
0019 static unsigned getHashValue(const char *Val) {
0020 assert(Val != getEmptyKey() && "Cannot hash the empty key!");
0021 assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
0022 return (unsigned)hash_value(StringRef(Val));
0023 }
0024 static bool isEqual(const char *LHS, const char *RHS) {
0025 if (RHS == getEmptyKey())
0026 return LHS == getEmptyKey();
0027 if (RHS == getTombstoneKey())
0028 return LHS == getTombstoneKey();
0029 return strcmp(LHS, RHS) == 0;
0030 }
0031 };
0032
0033 MCStreamer &Out;
0034 MCSection *Sec;
0035 DenseMap<const char *, uint32_t, CStrDenseMapInfo> Pool;
0036 uint32_t Offset = 0;
0037
0038 public:
0039 DWPStringPool(MCStreamer &Out, MCSection *Sec) : Out(Out), Sec(Sec) {}
0040
0041 uint32_t getOffset(const char *Str, unsigned Length) {
0042 assert(strlen(Str) + 1 == Length && "Ensure length hint is correct");
0043
0044 auto Pair = Pool.insert(std::make_pair(Str, Offset));
0045 if (Pair.second) {
0046 Out.switchSection(Sec);
0047 Out.emitBytes(StringRef(Str, Length));
0048 Offset += Length;
0049 }
0050
0051 return Pair.first->second;
0052 }
0053 };
0054 }
0055
0056 #endif