180814287SRaphael Isemann //===-- SBMemoryRegionInfoList.cpp ----------------------------------------===//
226036843SHoward Hellyer //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
626036843SHoward Hellyer //
726036843SHoward Hellyer //===----------------------------------------------------------------------===//
826036843SHoward Hellyer 
926036843SHoward Hellyer #include "lldb/API/SBMemoryRegionInfoList.h"
106f9e6901SZachary Turner #include "lldb/API/SBMemoryRegionInfo.h"
1126036843SHoward Hellyer #include "lldb/API/SBStream.h"
1226036843SHoward Hellyer #include "lldb/Target/MemoryRegionInfo.h"
131755f5b1SJonas Devlieghere #include "lldb/Utility/Instrumentation.h"
1426036843SHoward Hellyer 
1526036843SHoward Hellyer #include <vector>
1626036843SHoward Hellyer 
1726036843SHoward Hellyer using namespace lldb;
1826036843SHoward Hellyer using namespace lldb_private;
1926036843SHoward Hellyer 
20b9c1b51eSKate Stone class MemoryRegionInfoListImpl {
2126036843SHoward Hellyer public:
MemoryRegionInfoListImpl()22b9c1b51eSKate Stone   MemoryRegionInfoListImpl() : m_regions() {}
2326036843SHoward Hellyer 
24*24f9a2f5SShafik Yaghmour   MemoryRegionInfoListImpl(const MemoryRegionInfoListImpl &rhs) = default;
2526036843SHoward Hellyer 
operator =(const MemoryRegionInfoListImpl & rhs)26b9c1b51eSKate Stone   MemoryRegionInfoListImpl &operator=(const MemoryRegionInfoListImpl &rhs) {
2726036843SHoward Hellyer     if (this == &rhs)
2826036843SHoward Hellyer       return *this;
2926036843SHoward Hellyer     m_regions = rhs.m_regions;
3026036843SHoward Hellyer     return *this;
3126036843SHoward Hellyer   }
3226036843SHoward Hellyer 
GetSize() const3336788bbbSTatyana Krasnukha   size_t GetSize() const { return m_regions.size(); }
3426036843SHoward Hellyer 
Reserve(size_t capacity)3536788bbbSTatyana Krasnukha   void Reserve(size_t capacity) { return m_regions.reserve(capacity); }
3636788bbbSTatyana Krasnukha 
Append(const MemoryRegionInfo & sb_region)3736788bbbSTatyana Krasnukha   void Append(const MemoryRegionInfo &sb_region) {
3826036843SHoward Hellyer     m_regions.push_back(sb_region);
3926036843SHoward Hellyer   }
4026036843SHoward Hellyer 
Append(const MemoryRegionInfoListImpl & list)41b9c1b51eSKate Stone   void Append(const MemoryRegionInfoListImpl &list) {
4236788bbbSTatyana Krasnukha     Reserve(GetSize() + list.GetSize());
4336788bbbSTatyana Krasnukha 
4436788bbbSTatyana Krasnukha     for (const auto &val : list.m_regions)
4526036843SHoward Hellyer       Append(val);
4626036843SHoward Hellyer   }
4726036843SHoward Hellyer 
Clear()48b9c1b51eSKate Stone   void Clear() { m_regions.clear(); }
4926036843SHoward Hellyer 
GetMemoryRegionContainingAddress(lldb::addr_t addr,MemoryRegionInfo & region_info)50a758c9f7SMed Ismail Bennani   bool GetMemoryRegionContainingAddress(lldb::addr_t addr,
51a758c9f7SMed Ismail Bennani                                         MemoryRegionInfo &region_info) {
52a758c9f7SMed Ismail Bennani     for (auto &region : m_regions) {
53a758c9f7SMed Ismail Bennani       if (region.GetRange().Contains(addr)) {
54a758c9f7SMed Ismail Bennani         region_info = region;
55a758c9f7SMed Ismail Bennani         return true;
56a758c9f7SMed Ismail Bennani       }
57a758c9f7SMed Ismail Bennani     }
58a758c9f7SMed Ismail Bennani     return false;
59a758c9f7SMed Ismail Bennani   }
60a758c9f7SMed Ismail Bennani 
GetMemoryRegionInfoAtIndex(size_t index,MemoryRegionInfo & region_info)6136788bbbSTatyana Krasnukha   bool GetMemoryRegionInfoAtIndex(size_t index,
6236788bbbSTatyana Krasnukha                                   MemoryRegionInfo &region_info) {
6326036843SHoward Hellyer     if (index >= GetSize())
6426036843SHoward Hellyer       return false;
6526036843SHoward Hellyer     region_info = m_regions[index];
6626036843SHoward Hellyer     return true;
6726036843SHoward Hellyer   }
6826036843SHoward Hellyer 
Ref()6936788bbbSTatyana Krasnukha   MemoryRegionInfos &Ref() { return m_regions; }
7036788bbbSTatyana Krasnukha 
Ref() const7136788bbbSTatyana Krasnukha   const MemoryRegionInfos &Ref() const { return m_regions; }
7236788bbbSTatyana Krasnukha 
7326036843SHoward Hellyer private:
7436788bbbSTatyana Krasnukha   MemoryRegionInfos m_regions;
7526036843SHoward Hellyer };
7626036843SHoward Hellyer 
ref()77d5b44036SJonas Devlieghere MemoryRegionInfos &SBMemoryRegionInfoList::ref() { return m_opaque_up->Ref(); }
7836788bbbSTatyana Krasnukha 
ref() const7936788bbbSTatyana Krasnukha const MemoryRegionInfos &SBMemoryRegionInfoList::ref() const {
80d5b44036SJonas Devlieghere   return m_opaque_up->Ref();
8136788bbbSTatyana Krasnukha }
8236788bbbSTatyana Krasnukha 
SBMemoryRegionInfoList()83b9c1b51eSKate Stone SBMemoryRegionInfoList::SBMemoryRegionInfoList()
84baf5664fSJonas Devlieghere     : m_opaque_up(new MemoryRegionInfoListImpl()) {
851755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
86baf5664fSJonas Devlieghere }
8726036843SHoward Hellyer 
SBMemoryRegionInfoList(const SBMemoryRegionInfoList & rhs)88b9c1b51eSKate Stone SBMemoryRegionInfoList::SBMemoryRegionInfoList(
89b9c1b51eSKate Stone     const SBMemoryRegionInfoList &rhs)
90baf5664fSJonas Devlieghere     : m_opaque_up(new MemoryRegionInfoListImpl(*rhs.m_opaque_up)) {
911755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
92baf5664fSJonas Devlieghere }
9326036843SHoward Hellyer 
94866b7a65SJonas Devlieghere SBMemoryRegionInfoList::~SBMemoryRegionInfoList() = default;
9526036843SHoward Hellyer 
96b9c1b51eSKate Stone const SBMemoryRegionInfoList &SBMemoryRegionInfoList::
operator =(const SBMemoryRegionInfoList & rhs)97b9c1b51eSKate Stone operator=(const SBMemoryRegionInfoList &rhs) {
981755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, rhs);
99baf5664fSJonas Devlieghere 
100b9c1b51eSKate Stone   if (this != &rhs) {
101d5b44036SJonas Devlieghere     *m_opaque_up = *rhs.m_opaque_up;
10226036843SHoward Hellyer   }
103d232abc3SJonas Devlieghere   return *this;
10426036843SHoward Hellyer }
10526036843SHoward Hellyer 
GetSize() const106b9c1b51eSKate Stone uint32_t SBMemoryRegionInfoList::GetSize() const {
1071755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
108baf5664fSJonas Devlieghere 
109d5b44036SJonas Devlieghere   return m_opaque_up->GetSize();
11026036843SHoward Hellyer }
11126036843SHoward Hellyer 
GetMemoryRegionContainingAddress(lldb::addr_t addr,SBMemoryRegionInfo & region_info)112a758c9f7SMed Ismail Bennani bool SBMemoryRegionInfoList::GetMemoryRegionContainingAddress(
113a758c9f7SMed Ismail Bennani     lldb::addr_t addr, SBMemoryRegionInfo &region_info) {
1141755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, addr, region_info);
115a758c9f7SMed Ismail Bennani 
116a758c9f7SMed Ismail Bennani   return m_opaque_up->GetMemoryRegionContainingAddress(addr, region_info.ref());
117a758c9f7SMed Ismail Bennani }
118a758c9f7SMed Ismail Bennani 
GetMemoryRegionAtIndex(uint32_t idx,SBMemoryRegionInfo & region_info)119b9c1b51eSKate Stone bool SBMemoryRegionInfoList::GetMemoryRegionAtIndex(
120b9c1b51eSKate Stone     uint32_t idx, SBMemoryRegionInfo &region_info) {
1211755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, idx, region_info);
122baf5664fSJonas Devlieghere 
123581af8b0SJonas Devlieghere   return m_opaque_up->GetMemoryRegionInfoAtIndex(idx, region_info.ref());
12426036843SHoward Hellyer }
12526036843SHoward Hellyer 
Clear()126baf5664fSJonas Devlieghere void SBMemoryRegionInfoList::Clear() {
1271755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this);
128baf5664fSJonas Devlieghere 
129baf5664fSJonas Devlieghere   m_opaque_up->Clear();
130baf5664fSJonas Devlieghere }
13126036843SHoward Hellyer 
Append(SBMemoryRegionInfo & sb_region)132b9c1b51eSKate Stone void SBMemoryRegionInfoList::Append(SBMemoryRegionInfo &sb_region) {
1331755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, sb_region);
134baf5664fSJonas Devlieghere 
135d5b44036SJonas Devlieghere   m_opaque_up->Append(sb_region.ref());
13626036843SHoward Hellyer }
13726036843SHoward Hellyer 
Append(SBMemoryRegionInfoList & sb_region_list)138b9c1b51eSKate Stone void SBMemoryRegionInfoList::Append(SBMemoryRegionInfoList &sb_region_list) {
1391755f5b1SJonas Devlieghere   LLDB_INSTRUMENT_VA(this, sb_region_list);
140baf5664fSJonas Devlieghere 
141d5b44036SJonas Devlieghere   m_opaque_up->Append(*sb_region_list);
14226036843SHoward Hellyer }
14326036843SHoward Hellyer 
operator ->() const144b9c1b51eSKate Stone const MemoryRegionInfoListImpl *SBMemoryRegionInfoList::operator->() const {
145d5b44036SJonas Devlieghere   return m_opaque_up.get();
14626036843SHoward Hellyer }
14726036843SHoward Hellyer 
operator *() const148b9c1b51eSKate Stone const MemoryRegionInfoListImpl &SBMemoryRegionInfoList::operator*() const {
149d5b44036SJonas Devlieghere   assert(m_opaque_up.get());
150d5b44036SJonas Devlieghere   return *m_opaque_up;
15126036843SHoward Hellyer }
152