1 //===-- SBMemoryRegionInfoList.cpp ------------------------------*- C++ -*-===//
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/Log.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 GetMemoryRegionInfoAtIndex(size_t index,
52                                   MemoryRegionInfo &region_info) {
53     if (index >= GetSize())
54       return false;
55     region_info = m_regions[index];
56     return true;
57   }
58 
59   MemoryRegionInfos &Ref() { return m_regions; }
60 
61   const MemoryRegionInfos &Ref() const { return m_regions; }
62 
63 private:
64   MemoryRegionInfos m_regions;
65 };
66 
67 MemoryRegionInfos &SBMemoryRegionInfoList::ref() {
68   return m_opaque_ap->Ref();
69 }
70 
71 const MemoryRegionInfos &SBMemoryRegionInfoList::ref() const {
72   return m_opaque_ap->Ref();
73 }
74 
75 SBMemoryRegionInfoList::SBMemoryRegionInfoList()
76     : m_opaque_ap(new MemoryRegionInfoListImpl()) {}
77 
78 SBMemoryRegionInfoList::SBMemoryRegionInfoList(
79     const SBMemoryRegionInfoList &rhs)
80     : m_opaque_ap(new MemoryRegionInfoListImpl(*rhs.m_opaque_ap)) {}
81 
82 SBMemoryRegionInfoList::~SBMemoryRegionInfoList() {}
83 
84 const SBMemoryRegionInfoList &SBMemoryRegionInfoList::
85 operator=(const SBMemoryRegionInfoList &rhs) {
86   if (this != &rhs) {
87     *m_opaque_ap = *rhs.m_opaque_ap;
88   }
89   return *this;
90 }
91 
92 uint32_t SBMemoryRegionInfoList::GetSize() const {
93   return m_opaque_ap->GetSize();
94 }
95 
96 bool SBMemoryRegionInfoList::GetMemoryRegionAtIndex(
97     uint32_t idx, SBMemoryRegionInfo &region_info) {
98   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
99 
100   bool result = m_opaque_ap->GetMemoryRegionInfoAtIndex(idx, region_info.ref());
101 
102   if (log) {
103     SBStream sstr;
104     region_info.GetDescription(sstr);
105     log->Printf("SBMemoryRegionInfoList::GetMemoryRegionAtIndex (this.ap=%p, "
106                 "idx=%d) => SBMemoryRegionInfo (this.ap=%p, '%s')",
107                 static_cast<void *>(m_opaque_ap.get()), idx,
108                 static_cast<void *>(region_info.m_opaque_ap.get()),
109                 sstr.GetData());
110   }
111 
112   return result;
113 }
114 
115 void SBMemoryRegionInfoList::Clear() { m_opaque_ap->Clear(); }
116 
117 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfo &sb_region) {
118   m_opaque_ap->Append(sb_region.ref());
119 }
120 
121 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfoList &sb_region_list) {
122   m_opaque_ap->Append(*sb_region_list);
123 }
124 
125 const MemoryRegionInfoListImpl *SBMemoryRegionInfoList::operator->() const {
126   return m_opaque_ap.get();
127 }
128 
129 const MemoryRegionInfoListImpl &SBMemoryRegionInfoList::operator*() const {
130   assert(m_opaque_ap.get());
131   return *m_opaque_ap;
132 }
133