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