VMRange.h revision 353358
1317027Sdim//===-- VMRange.h -----------------------------------------------*- C++ -*-===//
2317027Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6317027Sdim//
7317027Sdim//===----------------------------------------------------------------------===//
8317027Sdim
9317027Sdim#ifndef liblldb_VMRange_h_
10317027Sdim#define liblldb_VMRange_h_
11317027Sdim
12344779Sdim#include "lldb/lldb-types.h"
13317027Sdim
14344779Sdim#include <stddef.h>
15344779Sdim#include <stdint.h>
16317027Sdim#include <vector>
17317027Sdim
18317027Sdimnamespace lldb_private {
19317027Sdimclass Stream;
20317027Sdim}
21317027Sdim
22317027Sdimnamespace lldb_private {
23317027Sdim
24317027Sdim// A vm address range. These can represent offsets ranges or actual
25317027Sdim// addresses.
26317027Sdimclass VMRange {
27317027Sdimpublic:
28317027Sdim  typedef std::vector<VMRange> collection;
29317027Sdim  typedef collection::iterator iterator;
30317027Sdim  typedef collection::const_iterator const_iterator;
31317027Sdim
32317027Sdim  VMRange() : m_base_addr(0), m_byte_size(0) {}
33317027Sdim
34317027Sdim  VMRange(lldb::addr_t start_addr, lldb::addr_t end_addr)
35317027Sdim      : m_base_addr(start_addr),
36317027Sdim        m_byte_size(end_addr > start_addr ? end_addr - start_addr : 0) {}
37317027Sdim
38317027Sdim  ~VMRange() {}
39317027Sdim
40317027Sdim  void Clear() {
41317027Sdim    m_base_addr = 0;
42317027Sdim    m_byte_size = 0;
43317027Sdim  }
44317027Sdim
45317027Sdim  // Set the start and end values
46317027Sdim  void Reset(lldb::addr_t start_addr, lldb::addr_t end_addr) {
47317027Sdim    SetBaseAddress(start_addr);
48317027Sdim    SetEndAddress(end_addr);
49317027Sdim  }
50317027Sdim
51317027Sdim  // Set the start value for the range, and keep the same size
52317027Sdim  void SetBaseAddress(lldb::addr_t base_addr) { m_base_addr = base_addr; }
53317027Sdim
54317027Sdim  void SetEndAddress(lldb::addr_t end_addr) {
55317027Sdim    const lldb::addr_t base_addr = GetBaseAddress();
56317027Sdim    if (end_addr > base_addr)
57317027Sdim      m_byte_size = end_addr - base_addr;
58317027Sdim    else
59317027Sdim      m_byte_size = 0;
60317027Sdim  }
61317027Sdim
62317027Sdim  lldb::addr_t GetByteSize() const { return m_byte_size; }
63317027Sdim
64317027Sdim  void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
65317027Sdim
66317027Sdim  lldb::addr_t GetBaseAddress() const { return m_base_addr; }
67317027Sdim
68317027Sdim  lldb::addr_t GetEndAddress() const { return GetBaseAddress() + m_byte_size; }
69317027Sdim
70317027Sdim  bool IsValid() const { return m_byte_size > 0; }
71317027Sdim
72317027Sdim  bool Contains(lldb::addr_t addr) const {
73317027Sdim    return (GetBaseAddress() <= addr) && (addr < GetEndAddress());
74317027Sdim  }
75317027Sdim
76317027Sdim  bool Contains(const VMRange &range) const {
77317027Sdim    if (Contains(range.GetBaseAddress())) {
78317027Sdim      lldb::addr_t range_end = range.GetEndAddress();
79317027Sdim      return (GetBaseAddress() <= range_end) && (range_end <= GetEndAddress());
80317027Sdim    }
81317027Sdim    return false;
82317027Sdim  }
83317027Sdim
84317027Sdim  void Dump(Stream *s, lldb::addr_t base_addr = 0,
85317027Sdim            uint32_t addr_width = 8) const;
86317027Sdim
87317027Sdim  static bool ContainsValue(const VMRange::collection &coll,
88317027Sdim                            lldb::addr_t value);
89317027Sdim
90317027Sdim  static bool ContainsRange(const VMRange::collection &coll,
91317027Sdim                            const VMRange &range);
92317027Sdim
93317027Sdimprotected:
94317027Sdim  lldb::addr_t m_base_addr;
95317027Sdim  lldb::addr_t m_byte_size;
96317027Sdim};
97317027Sdim
98317027Sdimbool operator==(const VMRange &lhs, const VMRange &rhs);
99317027Sdimbool operator!=(const VMRange &lhs, const VMRange &rhs);
100317027Sdimbool operator<(const VMRange &lhs, const VMRange &rhs);
101317027Sdimbool operator<=(const VMRange &lhs, const VMRange &rhs);
102317027Sdimbool operator>(const VMRange &lhs, const VMRange &rhs);
103317027Sdimbool operator>=(const VMRange &lhs, const VMRange &rhs);
104317027Sdim
105317027Sdim} // namespace lldb_private
106317027Sdim
107317027Sdim#endif // liblldb_VMRange_h_
108