1 //===-- VMRange.h -----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_VMRange_h_
11 #define liblldb_VMRange_h_
12 
13 #include "lldb/lldb-private.h"
14 #include <vector>
15 
16 namespace lldb_private {
17 
18 //----------------------------------------------------------------------
19 // A vm address range. These can represent offsets ranges or actual
20 // addresses.
21 //----------------------------------------------------------------------
22 class VMRange
23 {
24 public:
25 
26     typedef std::vector<VMRange> collection;
27     typedef collection::iterator iterator;
28     typedef collection::const_iterator const_iterator;
29 
VMRange()30     VMRange() :
31         m_base_addr(0),
32         m_byte_size(0)
33     {
34     }
35 
VMRange(lldb::addr_t start_addr,lldb::addr_t end_addr)36     VMRange(lldb::addr_t start_addr, lldb::addr_t end_addr) :
37         m_base_addr(start_addr),
38         m_byte_size(end_addr > start_addr ? end_addr - start_addr : 0)
39     {
40     }
41 
~VMRange()42     ~VMRange()
43     {
44     }
45 
46     void
Clear()47     Clear ()
48     {
49         m_base_addr = 0;
50         m_byte_size = 0;
51     }
52 
53     // Set the start and end values
54     void
Reset(lldb::addr_t start_addr,lldb::addr_t end_addr)55     Reset (lldb::addr_t start_addr, lldb::addr_t end_addr)
56     {
57         SetBaseAddress (start_addr);
58         SetEndAddress (end_addr);
59     }
60 
61     // Set the start value for the range, and keep the same size
62     void
SetBaseAddress(lldb::addr_t base_addr)63     SetBaseAddress (lldb::addr_t base_addr)
64     {
65         m_base_addr = base_addr;
66     }
67 
68     void
SetEndAddress(lldb::addr_t end_addr)69     SetEndAddress (lldb::addr_t end_addr)
70     {
71         const lldb::addr_t base_addr = GetBaseAddress();
72         if (end_addr > base_addr)
73             m_byte_size = end_addr - base_addr;
74         else
75             m_byte_size = 0;
76     }
77 
78     lldb::addr_t
GetByteSize()79     GetByteSize () const
80     {
81         return m_byte_size;
82     }
83 
84     void
SetByteSize(lldb::addr_t byte_size)85     SetByteSize (lldb::addr_t byte_size)
86     {
87         m_byte_size = byte_size;
88     }
89 
90     lldb::addr_t
GetBaseAddress()91     GetBaseAddress () const
92     {
93         return m_base_addr;
94     }
95 
96     lldb::addr_t
GetEndAddress()97     GetEndAddress () const
98     {
99         return GetBaseAddress() + m_byte_size;
100     }
101 
102     bool
IsValid()103     IsValid() const
104     {
105         return m_byte_size > 0;
106     }
107 
108     bool
Contains(lldb::addr_t addr)109     Contains (lldb::addr_t addr) const
110     {
111         return (GetBaseAddress() <= addr) && (addr < GetEndAddress());
112     }
113 
114     bool
Contains(const VMRange & range)115     Contains (const VMRange& range) const
116     {
117         if (Contains(range.GetBaseAddress()))
118         {
119             lldb::addr_t range_end = range.GetEndAddress();
120             return (GetBaseAddress() <= range_end) && (range_end <= GetEndAddress());
121         }
122         return false;
123     }
124 
125     void
126     Dump (Stream *s, lldb::addr_t base_addr = 0, uint32_t addr_width = 8) const;
127 
128     class ValueInRangeUnaryPredicate
129     {
130     public:
ValueInRangeUnaryPredicate(lldb::addr_t value)131         ValueInRangeUnaryPredicate(lldb::addr_t value) :
132             _value(value)
133         {
134         }
operator()135         bool operator()(const VMRange& range) const
136         {
137             return range.Contains(_value);
138         }
139         lldb::addr_t _value;
140     };
141 
142     class RangeInRangeUnaryPredicate
143     {
144     public:
RangeInRangeUnaryPredicate(VMRange range)145         RangeInRangeUnaryPredicate(VMRange range) :
146             _range(range)
147         {
148         }
operator()149         bool operator()(const VMRange& range) const
150         {
151             return range.Contains(_range);
152         }
153         const VMRange& _range;
154     };
155 
156     static bool
157     ContainsValue(const VMRange::collection& coll, lldb::addr_t value);
158 
159     static bool
160     ContainsRange(const VMRange::collection& coll, const VMRange& range);
161 
162     // Returns a valid index into coll when a match is found, else UINT32_MAX
163     // is returned
164     static size_t
165     FindRangeIndexThatContainsValue (const VMRange::collection& coll, lldb::addr_t value);
166 
167 protected:
168     lldb::addr_t m_base_addr;
169     lldb::addr_t m_byte_size;
170 };
171 
172 bool operator== (const VMRange& lhs, const VMRange& rhs);
173 bool operator!= (const VMRange& lhs, const VMRange& rhs);
174 bool operator<  (const VMRange& lhs, const VMRange& rhs);
175 bool operator<= (const VMRange& lhs, const VMRange& rhs);
176 bool operator>  (const VMRange& lhs, const VMRange& rhs);
177 bool operator>= (const VMRange& lhs, const VMRange& rhs);
178 
179 } // namespace lldb_private
180 
181 #endif  // liblldb_VMRange_h_
182