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() { 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 
76 SBMemoryRegionInfoList::SBMemoryRegionInfoList(
77     const SBMemoryRegionInfoList &rhs)
78     : m_opaque_up(new MemoryRegionInfoListImpl(*rhs.m_opaque_up)) {}
79 
80 SBMemoryRegionInfoList::~SBMemoryRegionInfoList() {}
81 
82 const SBMemoryRegionInfoList &SBMemoryRegionInfoList::
83 operator=(const SBMemoryRegionInfoList &rhs) {
84   if (this != &rhs) {
85     *m_opaque_up = *rhs.m_opaque_up;
86   }
87   return *this;
88 }
89 
90 uint32_t SBMemoryRegionInfoList::GetSize() const {
91   return m_opaque_up->GetSize();
92 }
93 
94 bool SBMemoryRegionInfoList::GetMemoryRegionAtIndex(
95     uint32_t idx, SBMemoryRegionInfo &region_info) {
96   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
97 
98   bool result = m_opaque_up->GetMemoryRegionInfoAtIndex(idx, region_info.ref());
99 
100   if (log) {
101     SBStream sstr;
102     region_info.GetDescription(sstr);
103     log->Printf("SBMemoryRegionInfoList::GetMemoryRegionAtIndex (this.ap=%p, "
104                 "idx=%d) => SBMemoryRegionInfo (this.ap=%p, '%s')",
105                 static_cast<void *>(m_opaque_up.get()), idx,
106                 static_cast<void *>(region_info.m_opaque_up.get()),
107                 sstr.GetData());
108   }
109 
110   return result;
111 }
112 
113 void SBMemoryRegionInfoList::Clear() { m_opaque_up->Clear(); }
114 
115 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfo &sb_region) {
116   m_opaque_up->Append(sb_region.ref());
117 }
118 
119 void SBMemoryRegionInfoList::Append(SBMemoryRegionInfoList &sb_region_list) {
120   m_opaque_up->Append(*sb_region_list);
121 }
122 
123 const MemoryRegionInfoListImpl *SBMemoryRegionInfoList::operator->() const {
124   return m_opaque_up.get();
125 }
126 
127 const MemoryRegionInfoListImpl &SBMemoryRegionInfoList::operator*() const {
128   assert(m_opaque_up.get());
129   return *m_opaque_up;
130 }
131