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