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