1 //===-- VMRange.cpp ---------------------------------------------*- 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 #include "lldb/lldb-private.h"
11 
12 #include "lldb/Utility/Stream.h"
13 #include "lldb/Utility/VMRange.h"
14 #include <algorithm>
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 bool VMRange::ContainsValue(const VMRange::collection &coll,
20                             lldb::addr_t value) {
21   ValueInRangeUnaryPredicate in_range_predicate(value);
22   VMRange::const_iterator pos;
23   VMRange::const_iterator end = coll.end();
24   pos = std::find_if(coll.begin(), end, in_range_predicate);
25   if (pos != end)
26     return true;
27   return false;
28 }
29 
30 bool VMRange::ContainsRange(const VMRange::collection &coll,
31                             const VMRange &range) {
32   RangeInRangeUnaryPredicate in_range_predicate(range);
33   VMRange::const_iterator pos;
34   VMRange::const_iterator end = coll.end();
35   pos = std::find_if(coll.begin(), end, in_range_predicate);
36   if (pos != end)
37     return true;
38   return false;
39 }
40 
41 size_t VMRange::FindRangeIndexThatContainsValue(const VMRange::collection &coll,
42                                                 lldb::addr_t value) {
43   ValueInRangeUnaryPredicate in_range_predicate(value);
44   VMRange::const_iterator begin = coll.begin();
45   VMRange::const_iterator end = coll.end();
46   VMRange::const_iterator pos = std::find_if(begin, end, in_range_predicate);
47   if (pos != end)
48     return std::distance(begin, pos);
49   return UINT32_MAX;
50 }
51 
52 void VMRange::Dump(Stream *s, lldb::addr_t offset, uint32_t addr_width) const {
53   s->AddressRange(offset + GetBaseAddress(), offset + GetEndAddress(),
54                   addr_width);
55 }
56 
57 bool lldb_private::operator==(const VMRange &lhs, const VMRange &rhs) {
58   return lhs.GetBaseAddress() == rhs.GetBaseAddress() &&
59          lhs.GetEndAddress() == rhs.GetEndAddress();
60 }
61 
62 bool lldb_private::operator!=(const VMRange &lhs, const VMRange &rhs) {
63   return lhs.GetBaseAddress() != rhs.GetBaseAddress() ||
64          lhs.GetEndAddress() != rhs.GetEndAddress();
65 }
66 
67 bool lldb_private::operator<(const VMRange &lhs, const VMRange &rhs) {
68   if (lhs.GetBaseAddress() < rhs.GetBaseAddress())
69     return true;
70   else if (lhs.GetBaseAddress() > rhs.GetBaseAddress())
71     return false;
72   return lhs.GetEndAddress() < rhs.GetEndAddress();
73 }
74 
75 bool lldb_private::operator<=(const VMRange &lhs, const VMRange &rhs) {
76   if (lhs.GetBaseAddress() < rhs.GetBaseAddress())
77     return true;
78   else if (lhs.GetBaseAddress() > rhs.GetBaseAddress())
79     return false;
80   return lhs.GetEndAddress() <= rhs.GetEndAddress();
81 }
82 
83 bool lldb_private::operator>(const VMRange &lhs, const VMRange &rhs) {
84   if (lhs.GetBaseAddress() > rhs.GetBaseAddress())
85     return true;
86   else if (lhs.GetBaseAddress() < rhs.GetBaseAddress())
87     return false;
88   return lhs.GetEndAddress() > rhs.GetEndAddress();
89 }
90 
91 bool lldb_private::operator>=(const VMRange &lhs, const VMRange &rhs) {
92   if (lhs.GetBaseAddress() > rhs.GetBaseAddress())
93     return true;
94   else if (lhs.GetBaseAddress() < rhs.GetBaseAddress())
95     return false;
96   return lhs.GetEndAddress() >= rhs.GetEndAddress();
97 }
98