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 "SBReproducerPrivate.h"
11 #include "lldb/API/SBMemoryRegionInfo.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Target/MemoryRegionInfo.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() { return m_opaque_up->Ref(); }
68 
69 const MemoryRegionInfos &SBMemoryRegionInfoList::ref() const {
70   return m_opaque_up->Ref();
71 }
72 
73 SBMemoryRegionInfoList::SBMemoryRegionInfoList()
74     : m_opaque_up(new MemoryRegionInfoListImpl()) {
75   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBMemoryRegionInfoList);
76 }
77 
78 SBMemoryRegionInfoList::SBMemoryRegionInfoList(
79     const SBMemoryRegionInfoList &rhs)
80     : m_opaque_up(new MemoryRegionInfoListImpl(*rhs.m_opaque_up)) {
81   LLDB_RECORD_CONSTRUCTOR(SBMemoryRegionInfoList,
82                           (const lldb::SBMemoryRegionInfoList &), rhs);
83 }
84 
85 SBMemoryRegionInfoList::~SBMemoryRegionInfoList() = default;
86 
87 const SBMemoryRegionInfoList &SBMemoryRegionInfoList::
88 operator=(const SBMemoryRegionInfoList &rhs) {
89   LLDB_RECORD_METHOD(
90       const lldb::SBMemoryRegionInfoList &,
91       SBMemoryRegionInfoList, operator=,(const lldb::SBMemoryRegionInfoList &),
92       rhs);
93 
94   if (this != &rhs) {
95     *m_opaque_up = *rhs.m_opaque_up;
96   }
97   return LLDB_RECORD_RESULT(*this);
98 }
99 
100 uint32_t SBMemoryRegionInfoList::GetSize() const {
101   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBMemoryRegionInfoList, GetSize);
102 
103   return m_opaque_up->GetSize();
104 }
105 
106 bool SBMemoryRegionInfoList::GetMemoryRegionAtIndex(
107     uint32_t idx, SBMemoryRegionInfo &region_info) {
108   LLDB_RECORD_METHOD(bool, SBMemoryRegionInfoList, GetMemoryRegionAtIndex,
109                      (uint32_t, lldb::SBMemoryRegionInfo &), idx, region_info);
110 
111   return m_opaque_up->GetMemoryRegionInfoAtIndex(idx, region_info.ref());
112 }
113 
114 void SBMemoryRegionInfoList::Clear() {
115   LLDB_RECORD_METHOD_NO_ARGS(void, SBMemoryRegionInfoList, Clear);
116 
117   m_opaque_up->Clear();
118 }
119 
120 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfo &sb_region) {
121   LLDB_RECORD_METHOD(void, SBMemoryRegionInfoList, Append,
122                      (lldb::SBMemoryRegionInfo &), sb_region);
123 
124   m_opaque_up->Append(sb_region.ref());
125 }
126 
127 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfoList &sb_region_list) {
128   LLDB_RECORD_METHOD(void, SBMemoryRegionInfoList, Append,
129                      (lldb::SBMemoryRegionInfoList &), sb_region_list);
130 
131   m_opaque_up->Append(*sb_region_list);
132 }
133 
134 const MemoryRegionInfoListImpl *SBMemoryRegionInfoList::operator->() const {
135   return m_opaque_up.get();
136 }
137 
138 const MemoryRegionInfoListImpl &SBMemoryRegionInfoList::operator*() const {
139   assert(m_opaque_up.get());
140   return *m_opaque_up;
141 }
142 
143 namespace lldb_private {
144 namespace repro {
145 
146 template <>
147 void RegisterMethods<SBMemoryRegionInfoList>(Registry &R) {
148   LLDB_REGISTER_CONSTRUCTOR(SBMemoryRegionInfoList, ());
149   LLDB_REGISTER_CONSTRUCTOR(SBMemoryRegionInfoList,
150                             (const lldb::SBMemoryRegionInfoList &));
151   LLDB_REGISTER_METHOD(
152       const lldb::SBMemoryRegionInfoList &,
153       SBMemoryRegionInfoList, operator=,(
154                                   const lldb::SBMemoryRegionInfoList &));
155   LLDB_REGISTER_METHOD_CONST(uint32_t, SBMemoryRegionInfoList, GetSize, ());
156   LLDB_REGISTER_METHOD(bool, SBMemoryRegionInfoList, GetMemoryRegionAtIndex,
157                        (uint32_t, lldb::SBMemoryRegionInfo &));
158   LLDB_REGISTER_METHOD(void, SBMemoryRegionInfoList, Clear, ());
159   LLDB_REGISTER_METHOD(void, SBMemoryRegionInfoList, Append,
160                        (lldb::SBMemoryRegionInfo &));
161   LLDB_REGISTER_METHOD(void, SBMemoryRegionInfoList, Append,
162                        (lldb::SBMemoryRegionInfoList &));
163 }
164 
165 }
166 }
167