• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <map>
22 #include <memory>
23 #include <string>
24 
25 #include <unwindstack/Memory.h>
26 
27 namespace unwindstack {
28 
29 // MemoryRange maps one address range onto another.
30 // The range [src_begin, src_begin + length) in the underlying Memory is mapped onto offset,
31 // such that range.read(offset) is equivalent to underlying.read(src_begin).
32 class MemoryRange : public Memory {
33  public:
34   MemoryRange(const std::shared_ptr<Memory>& memory, uint64_t begin, uint64_t length,
35               uint64_t offset);
36   virtual ~MemoryRange() = default;
37 
38   size_t Read(uint64_t addr, void* dst, size_t size) override;
39 
offset()40   uint64_t offset() { return offset_; }
length()41   uint64_t length() { return length_; }
42 
43  private:
44   std::shared_ptr<Memory> memory_;
45   uint64_t begin_;
46   uint64_t length_;
47   uint64_t offset_;
48 };
49 
50 class MemoryRanges : public Memory {
51  public:
52   MemoryRanges() = default;
53   virtual ~MemoryRanges() = default;
54 
55   bool Insert(MemoryRange* memory);
56 
57   size_t Read(uint64_t addr, void* dst, size_t size) override;
58 
59  private:
60   std::map<uint64_t, std::unique_ptr<MemoryRange>> maps_;
61 };
62 
63 }  // namespace unwindstack
64