DWPStringPool.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef TOOLS_LLVM_DWP_DWPSTRINGPOOL
  2. #define TOOLS_LLVM_DWP_DWPSTRINGPOOL
  3. #include "llvm/ADT/DenseMap.h"
  4. #include "llvm/MC/MCSection.h"
  5. #include "llvm/MC/MCStreamer.h"
  6. #include <cassert>
  7. namespace llvm {
  8. class DWPStringPool {
  9. struct CStrDenseMapInfo {
  10. static inline const char *getEmptyKey() {
  11. return reinterpret_cast<const char *>(~static_cast<uintptr_t>(0));
  12. }
  13. static inline const char *getTombstoneKey() {
  14. return reinterpret_cast<const char *>(~static_cast<uintptr_t>(1));
  15. }
  16. static unsigned getHashValue(const char *Val) {
  17. assert(Val != getEmptyKey() && "Cannot hash the empty key!");
  18. assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
  19. return (unsigned)hash_value(StringRef(Val));
  20. }
  21. static bool isEqual(const char *LHS, const char *RHS) {
  22. if (RHS == getEmptyKey())
  23. return LHS == getEmptyKey();
  24. if (RHS == getTombstoneKey())
  25. return LHS == getTombstoneKey();
  26. return strcmp(LHS, RHS) == 0;
  27. }
  28. };
  29. MCStreamer &Out;
  30. MCSection *Sec;
  31. DenseMap<const char *, uint32_t, CStrDenseMapInfo> Pool;
  32. uint32_t Offset = 0;
  33. public:
  34. DWPStringPool(MCStreamer &Out, MCSection *Sec) : Out(Out), Sec(Sec) {}
  35. uint32_t getOffset(const char *Str, unsigned Length) {
  36. assert(strlen(Str) + 1 == Length && "Ensure length hint is correct");
  37. auto Pair = Pool.insert(std::make_pair(Str, Offset));
  38. if (Pair.second) {
  39. Out.SwitchSection(Sec);
  40. Out.emitBytes(StringRef(Str, Length));
  41. Offset += Length;
  42. }
  43. return Pair.first->second;
  44. }
  45. };
  46. }
  47. #endif