VMRange.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //===-- VMRange.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. #ifndef LLDB_UTILITY_VMRANGE_H
  9. #define LLDB_UTILITY_VMRANGE_H
  10. #include "lldb/lldb-types.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include <cstddef>
  13. #include <cstdint>
  14. #include <vector>
  15. namespace lldb_private {
  16. // A vm address range. These can represent offsets ranges or actual
  17. // addresses.
  18. class VMRange {
  19. public:
  20. typedef std::vector<VMRange> collection;
  21. typedef collection::iterator iterator;
  22. typedef collection::const_iterator const_iterator;
  23. VMRange() : m_base_addr(0), m_byte_size(0) {}
  24. VMRange(lldb::addr_t start_addr, lldb::addr_t end_addr)
  25. : m_base_addr(start_addr),
  26. m_byte_size(end_addr > start_addr ? end_addr - start_addr : 0) {}
  27. ~VMRange() {}
  28. void Clear() {
  29. m_base_addr = 0;
  30. m_byte_size = 0;
  31. }
  32. // Set the start and end values
  33. void Reset(lldb::addr_t start_addr, lldb::addr_t end_addr) {
  34. SetBaseAddress(start_addr);
  35. SetEndAddress(end_addr);
  36. }
  37. // Set the start value for the range, and keep the same size
  38. void SetBaseAddress(lldb::addr_t base_addr) { m_base_addr = base_addr; }
  39. void SetEndAddress(lldb::addr_t end_addr) {
  40. const lldb::addr_t base_addr = GetBaseAddress();
  41. if (end_addr > base_addr)
  42. m_byte_size = end_addr - base_addr;
  43. else
  44. m_byte_size = 0;
  45. }
  46. lldb::addr_t GetByteSize() const { return m_byte_size; }
  47. void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
  48. lldb::addr_t GetBaseAddress() const { return m_base_addr; }
  49. lldb::addr_t GetEndAddress() const { return GetBaseAddress() + m_byte_size; }
  50. bool IsValid() const { return m_byte_size > 0; }
  51. bool Contains(lldb::addr_t addr) const {
  52. return (GetBaseAddress() <= addr) && (addr < GetEndAddress());
  53. }
  54. bool Contains(const VMRange &range) const {
  55. if (Contains(range.GetBaseAddress())) {
  56. lldb::addr_t range_end = range.GetEndAddress();
  57. return (GetBaseAddress() <= range_end) && (range_end <= GetEndAddress());
  58. }
  59. return false;
  60. }
  61. void Dump(llvm::raw_ostream &s, lldb::addr_t base_addr = 0,
  62. uint32_t addr_width = 8) const;
  63. static bool ContainsValue(const VMRange::collection &coll,
  64. lldb::addr_t value);
  65. static bool ContainsRange(const VMRange::collection &coll,
  66. const VMRange &range);
  67. protected:
  68. lldb::addr_t m_base_addr;
  69. lldb::addr_t m_byte_size;
  70. };
  71. bool operator==(const VMRange &lhs, const VMRange &rhs);
  72. bool operator!=(const VMRange &lhs, const VMRange &rhs);
  73. bool operator<(const VMRange &lhs, const VMRange &rhs);
  74. bool operator<=(const VMRange &lhs, const VMRange &rhs);
  75. bool operator>(const VMRange &lhs, const VMRange &rhs);
  76. bool operator>=(const VMRange &lhs, const VMRange &rhs);
  77. } // namespace lldb_private
  78. #endif // LLDB_UTILITY_VMRANGE_H