1 //===-- SBMemoryRegionInfoList.cpp ------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/API/SBMemoryRegionInfoList.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:
MemoryRegionInfoListImpl()23 MemoryRegionInfoListImpl() : m_regions() {}
24
MemoryRegionInfoListImpl(const MemoryRegionInfoListImpl & rhs)25 MemoryRegionInfoListImpl(const MemoryRegionInfoListImpl &rhs)
26 : m_regions(rhs.m_regions) {}
27
operator =(const MemoryRegionInfoListImpl & rhs)28 MemoryRegionInfoListImpl &operator=(const MemoryRegionInfoListImpl &rhs) {
29 if (this == &rhs)
30 return *this;
31 m_regions = rhs.m_regions;
32 return *this;
33 }
34
GetSize() const35 size_t GetSize() const { return m_regions.size(); }
36
Reserve(size_t capacity)37 void Reserve(size_t capacity) { return m_regions.reserve(capacity); }
38
Append(const MemoryRegionInfo & sb_region)39 void Append(const MemoryRegionInfo &sb_region) {
40 m_regions.push_back(sb_region);
41 }
42
Append(const MemoryRegionInfoListImpl & list)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
Clear()50 void Clear() { m_regions.clear(); }
51
GetMemoryRegionInfoAtIndex(size_t index,MemoryRegionInfo & region_info)52 bool GetMemoryRegionInfoAtIndex(size_t index,
53 MemoryRegionInfo ®ion_info) {
54 if (index >= GetSize())
55 return false;
56 region_info = m_regions[index];
57 return true;
58 }
59
Ref()60 MemoryRegionInfos &Ref() { return m_regions; }
61
Ref() const62 const MemoryRegionInfos &Ref() const { return m_regions; }
63
64 private:
65 MemoryRegionInfos m_regions;
66 };
67
ref()68 MemoryRegionInfos &SBMemoryRegionInfoList::ref() {
69 return m_opaque_ap->Ref();
70 }
71
ref() const72 const MemoryRegionInfos &SBMemoryRegionInfoList::ref() const {
73 return m_opaque_ap->Ref();
74 }
75
SBMemoryRegionInfoList()76 SBMemoryRegionInfoList::SBMemoryRegionInfoList()
77 : m_opaque_ap(new MemoryRegionInfoListImpl()) {}
78
SBMemoryRegionInfoList(const SBMemoryRegionInfoList & rhs)79 SBMemoryRegionInfoList::SBMemoryRegionInfoList(
80 const SBMemoryRegionInfoList &rhs)
81 : m_opaque_ap(new MemoryRegionInfoListImpl(*rhs.m_opaque_ap)) {}
82
~SBMemoryRegionInfoList()83 SBMemoryRegionInfoList::~SBMemoryRegionInfoList() {}
84
85 const SBMemoryRegionInfoList &SBMemoryRegionInfoList::
operator =(const SBMemoryRegionInfoList & rhs)86 operator=(const SBMemoryRegionInfoList &rhs) {
87 if (this != &rhs) {
88 *m_opaque_ap = *rhs.m_opaque_ap;
89 }
90 return *this;
91 }
92
GetSize() const93 uint32_t SBMemoryRegionInfoList::GetSize() const {
94 return m_opaque_ap->GetSize();
95 }
96
GetMemoryRegionAtIndex(uint32_t idx,SBMemoryRegionInfo & region_info)97 bool SBMemoryRegionInfoList::GetMemoryRegionAtIndex(
98 uint32_t idx, SBMemoryRegionInfo ®ion_info) {
99 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
100
101 bool result = m_opaque_ap->GetMemoryRegionInfoAtIndex(idx, region_info.ref());
102
103 if (log) {
104 SBStream sstr;
105 region_info.GetDescription(sstr);
106 log->Printf("SBMemoryRegionInfoList::GetMemoryRegionAtIndex (this.ap=%p, "
107 "idx=%d) => SBMemoryRegionInfo (this.ap=%p, '%s')",
108 static_cast<void *>(m_opaque_ap.get()), idx,
109 static_cast<void *>(region_info.m_opaque_ap.get()),
110 sstr.GetData());
111 }
112
113 return result;
114 }
115
Clear()116 void SBMemoryRegionInfoList::Clear() { m_opaque_ap->Clear(); }
117
Append(SBMemoryRegionInfo & sb_region)118 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfo &sb_region) {
119 m_opaque_ap->Append(sb_region.ref());
120 }
121
Append(SBMemoryRegionInfoList & sb_region_list)122 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfoList &sb_region_list) {
123 m_opaque_ap->Append(*sb_region_list);
124 }
125
operator ->() const126 const MemoryRegionInfoListImpl *SBMemoryRegionInfoList::operator->() const {
127 return m_opaque_ap.get();
128 }
129
operator *() const130 const MemoryRegionInfoListImpl &SBMemoryRegionInfoList::operator*() const {
131 assert(m_opaque_ap.get());
132 return *m_opaque_ap;
133 }
134