DWPStringPool.h 1.8 KB

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