1 //===-- SBMemoryRegionInfoList.cpp ----------------------------------------===//
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 #include "lldb/API/SBMemoryRegionInfoList.h"
10 #include "lldb/API/SBMemoryRegionInfo.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Target/MemoryRegionInfo.h"
13 #include "lldb/Utility/Instrumentation.h"
14 
15 #include <vector>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 class MemoryRegionInfoListImpl {
21 public:
22   MemoryRegionInfoListImpl() : m_regions() {}
23 
24   MemoryRegionInfoListImpl(const MemoryRegionInfoListImpl &rhs) = default;
25 
26   MemoryRegionInfoListImpl &operator=(const MemoryRegionInfoListImpl &rhs) {
27     if (this == &rhs)
28       return *this;
29     m_regions = rhs.m_regions;
30     return *this;
31   }
32 
33   size_t GetSize() const { return m_regions.size(); }
34 
35   void Reserve(size_t capacity) { return m_regions.reserve(capacity); }
36 
37   void Append(const MemoryRegionInfo &sb_region) {
38     m_regions.push_back(sb_region);
39   }
40 
41   void Append(const MemoryRegionInfoListImpl &list) {
42     Reserve(GetSize() + list.GetSize());
43 
44     for (const auto &val : list.m_regions)
45       Append(val);
46   }
47 
48   void Clear() { m_regions.clear(); }
49 
50   bool GetMemoryRegionContainingAddress(lldb::addr_t addr,
51                                         MemoryRegionInfo &region_info) {
52     for (auto &region : m_regions) {
53       if (region.GetRange().Contains(addr)) {
54         region_info = region;
55         return true;
56       }
57     }
58     return false;
59   }
60 
61   bool GetMemoryRegionInfoAtIndex(size_t index,
62                                   MemoryRegionInfo &region_info) {
63     if (index >= GetSize())
64       return false;
65     region_info = m_regions[index];
66     return true;
67   }
68 
69   MemoryRegionInfos &Ref() { return m_regions; }
70 
71   const MemoryRegionInfos &Ref() const { return m_regions; }
72 
73 private:
74   MemoryRegionInfos m_regions;
75 };
76 
77 MemoryRegionInfos &SBMemoryRegionInfoList::ref() { return m_opaque_up->Ref(); }
78 
79 const MemoryRegionInfos &SBMemoryRegionInfoList::ref() const {
80   return m_opaque_up->Ref();
81 }
82 
83 SBMemoryRegionInfoList::SBMemoryRegionInfoList()
84     : m_opaque_up(new MemoryRegionInfoListImpl()) {
85   LLDB_INSTRUMENT_VA(this);
86 }
87 
88 SBMemoryRegionInfoList::SBMemoryRegionInfoList(
89     const SBMemoryRegionInfoList &rhs)
90     : m_opaque_up(new MemoryRegionInfoListImpl(*rhs.m_opaque_up)) {
91   LLDB_INSTRUMENT_VA(this, rhs);
92 }
93 
94 SBMemoryRegionInfoList::~SBMemoryRegionInfoList() = default;
95 
96 const SBMemoryRegionInfoList &SBMemoryRegionInfoList::
97 operator=(const SBMemoryRegionInfoList &rhs) {
98   LLDB_INSTRUMENT_VA(this, rhs);
99 
100   if (this != &rhs) {
101     *m_opaque_up = *rhs.m_opaque_up;
102   }
103   return *this;
104 }
105 
106 uint32_t SBMemoryRegionInfoList::GetSize() const {
107   LLDB_INSTRUMENT_VA(this);
108 
109   return m_opaque_up->GetSize();
110 }
111 
112 bool SBMemoryRegionInfoList::GetMemoryRegionContainingAddress(
113     lldb::addr_t addr, SBMemoryRegionInfo &region_info) {
114   LLDB_INSTRUMENT_VA(this, addr, region_info);
115 
116   return m_opaque_up->GetMemoryRegionContainingAddress(addr, region_info.ref());
117 }
118 
119 bool SBMemoryRegionInfoList::GetMemoryRegionAtIndex(
120     uint32_t idx, SBMemoryRegionInfo &region_info) {
121   LLDB_INSTRUMENT_VA(this, idx, region_info);
122 
123   return m_opaque_up->GetMemoryRegionInfoAtIndex(idx, region_info.ref());
124 }
125 
126 void SBMemoryRegionInfoList::Clear() {
127   LLDB_INSTRUMENT_VA(this);
128 
129   m_opaque_up->Clear();
130 }
131 
132 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfo &sb_region) {
133   LLDB_INSTRUMENT_VA(this, sb_region);
134 
135   m_opaque_up->Append(sb_region.ref());
136 }
137 
138 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfoList &sb_region_list) {
139   LLDB_INSTRUMENT_VA(this, sb_region_list);
140 
141   m_opaque_up->Append(*sb_region_list);
142 }
143 
144 const MemoryRegionInfoListImpl *SBMemoryRegionInfoList::operator->() const {
145   return m_opaque_up.get();
146 }
147 
148 const MemoryRegionInfoListImpl &SBMemoryRegionInfoList::operator*() const {
149   assert(m_opaque_up.get());
150   return *m_opaque_up;
151 }
152