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"
13360784Sdim#include "llvm/Support/raw_ostream.h"
14317027Sdim
15344779Sdim#include <stddef.h>
16344779Sdim#include <stdint.h>
17317027Sdim#include <vector>
18317027Sdim
19317027Sdimnamespace lldb_private {
20317027Sdim
21317027Sdim// A vm address range. These can represent offsets ranges or actual
22317027Sdim// addresses.
23317027Sdimclass VMRange {
24317027Sdimpublic:
25317027Sdim  typedef std::vector<VMRange> collection;
26317027Sdim  typedef collection::iterator iterator;
27317027Sdim  typedef collection::const_iterator const_iterator;
28317027Sdim
29317027Sdim  VMRange() : m_base_addr(0), m_byte_size(0) {}
30317027Sdim
31317027Sdim  VMRange(lldb::addr_t start_addr, lldb::addr_t end_addr)
32317027Sdim      : m_base_addr(start_addr),
33317027Sdim        m_byte_size(end_addr > start_addr ? end_addr - start_addr : 0) {}
34317027Sdim
35317027Sdim  ~VMRange() {}
36317027Sdim
37317027Sdim  void Clear() {
38317027Sdim    m_base_addr = 0;
39317027Sdim    m_byte_size = 0;
40317027Sdim  }
41317027Sdim
42317027Sdim  // Set the start and end values
43317027Sdim  void Reset(lldb::addr_t start_addr, lldb::addr_t end_addr) {
44317027Sdim    SetBaseAddress(start_addr);
45317027Sdim    SetEndAddress(end_addr);
46317027Sdim  }
47317027Sdim
48317027Sdim  // Set the start value for the range, and keep the same size
49317027Sdim  void SetBaseAddress(lldb::addr_t base_addr) { m_base_addr = base_addr; }
50317027Sdim
51317027Sdim  void SetEndAddress(lldb::addr_t end_addr) {
52317027Sdim    const lldb::addr_t base_addr = GetBaseAddress();
53317027Sdim    if (end_addr > base_addr)
54317027Sdim      m_byte_size = end_addr - base_addr;
55317027Sdim    else
56317027Sdim      m_byte_size = 0;
57317027Sdim  }
58317027Sdim
59317027Sdim  lldb::addr_t GetByteSize() const { return m_byte_size; }
60317027Sdim
61317027Sdim  void SetByteSize(lldb::addr_t byte_size) { m_byte_size = byte_size; }
62317027Sdim
63317027Sdim  lldb::addr_t GetBaseAddress() const { return m_base_addr; }
64317027Sdim
65317027Sdim  lldb::addr_t GetEndAddress() const { return GetBaseAddress() + m_byte_size; }
66317027Sdim
67317027Sdim  bool IsValid() const { return m_byte_size > 0; }
68317027Sdim
69317027Sdim  bool Contains(lldb::addr_t addr) const {
70317027Sdim    return (GetBaseAddress() <= addr) && (addr < GetEndAddress());
71317027Sdim  }
72317027Sdim
73317027Sdim  bool Contains(const VMRange &range) const {
74317027Sdim    if (Contains(range.GetBaseAddress())) {
75317027Sdim      lldb::addr_t range_end = range.GetEndAddress();
76317027Sdim      return (GetBaseAddress() <= range_end) && (range_end <= GetEndAddress());
77317027Sdim    }
78317027Sdim    return false;
79317027Sdim  }
80317027Sdim
81360784Sdim  void Dump(llvm::raw_ostream &s, lldb::addr_t base_addr = 0,
82317027Sdim            uint32_t addr_width = 8) const;
83317027Sdim
84317027Sdim  static bool ContainsValue(const VMRange::collection &coll,
85317027Sdim                            lldb::addr_t value);
86317027Sdim
87317027Sdim  static bool ContainsRange(const VMRange::collection &coll,
88317027Sdim                            const VMRange &range);
89317027Sdim
90317027Sdimprotected:
91317027Sdim  lldb::addr_t m_base_addr;
92317027Sdim  lldb::addr_t m_byte_size;
93317027Sdim};
94317027Sdim
95317027Sdimbool operator==(const VMRange &lhs, const VMRange &rhs);
96317027Sdimbool operator!=(const VMRange &lhs, const VMRange &rhs);
97317027Sdimbool operator<(const VMRange &lhs, const VMRange &rhs);
98317027Sdimbool operator<=(const VMRange &lhs, const VMRange &rhs);
99317027Sdimbool operator>(const VMRange &lhs, const VMRange &rhs);
100317027Sdimbool operator>=(const VMRange &lhs, const VMRange &rhs);
101317027Sdim
102317027Sdim} // namespace lldb_private
103317027Sdim
104317027Sdim#endif // liblldb_VMRange_h_
105