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 #ifndef _LIBUNWINDSTACK_MEMORY_RANGE_H
18 #define _LIBUNWINDSTACK_MEMORY_RANGE_H
19 
20 #include <stdint.h>
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 
26 #include <unwindstack/Memory.h>
27 
28 namespace unwindstack {
29 
30 // MemoryRange maps one address range onto another.
31 // The range [src_begin, src_begin + length) in the underlying Memory is mapped onto offset,
32 // such that range.read(offset) is equivalent to underlying.read(src_begin).
33 class MemoryRange : public Memory {
34  public:
35   MemoryRange(const std::shared_ptr<Memory>& memory, uint64_t begin, uint64_t length,
36               uint64_t offset);
37   virtual ~MemoryRange() = default;
38 
39   size_t Read(uint64_t addr, void* dst, size_t size) override;
40 
offset()41   uint64_t offset() { return offset_; }
length()42   uint64_t length() { return length_; }
43 
44  private:
45   std::shared_ptr<Memory> memory_;
46   uint64_t begin_;
47   uint64_t length_;
48   uint64_t offset_;
49 };
50 
51 class MemoryRanges : public Memory {
52  public:
53   MemoryRanges() = default;
54   virtual ~MemoryRanges() = default;
55 
56   void Insert(MemoryRange* memory);
57 
58   size_t Read(uint64_t addr, void* dst, size_t size) override;
59 
60  private:
61   std::map<uint64_t, std::unique_ptr<MemoryRange>> maps_;
62 };
63 
64 }  // namespace unwindstack
65 
66 #endif  // _LIBUNWINDSTACK_MEMORY_RANGE_H
67