• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LIBPANDABASE_MEM_MEM_RANGE_H
17 #define LIBPANDABASE_MEM_MEM_RANGE_H
18 
19 #include <ostream>
20 
21 namespace panda::mem {
22 
23 /**
24  * Represents range of bytes [start_address, end_address]
25  */
26 class MemRange {
27 public:
MemRange(uintptr_t start_address,uintptr_t end_address)28     MemRange(uintptr_t start_address, uintptr_t end_address) : start_address_(start_address), end_address_(end_address)
29     {
30         ASSERT(end_address_ >= start_address_);
31     }
32 
IsAddressInRange(uintptr_t addr)33     bool IsAddressInRange(uintptr_t addr) const
34     {
35         return (addr >= start_address_) && (addr <= end_address_);
36     }
37 
GetStartAddress()38     uintptr_t GetStartAddress() const
39     {
40         return start_address_;
41     }
42 
GetEndAddress()43     uintptr_t GetEndAddress() const
44     {
45         return end_address_;
46     }
47 
IsIntersect(const MemRange & other)48     bool IsIntersect(const MemRange &other) const
49     {
50         return ((end_address_ >= other.start_address_) && (end_address_ <= other.end_address_)) ||
51                ((start_address_ >= other.start_address_) && (start_address_ <= other.end_address_)) ||
52                ((start_address_ < other.start_address_) && (end_address_ > other.end_address_));
53     }
54 
Contains(const MemRange & other)55     bool Contains(const MemRange &other) const
56     {
57         return start_address_ <= other.start_address_ && end_address_ >= other.end_address_;
58     }
59 
Contains(uintptr_t addr)60     bool Contains(uintptr_t addr) const
61     {
62         return start_address_ <= addr && addr < end_address_;
63     }
64 
65     friend std::ostream &operator<<(std::ostream &os, MemRange const &mem_range)
66     {
67         return os << std::hex << "[ 0x" << mem_range.GetStartAddress() << " : 0x" << mem_range.GetEndAddress() << "]";
68     }
69 
70     virtual ~MemRange() = default;
71 
72     DEFAULT_COPY_SEMANTIC(MemRange);
73     DEFAULT_MOVE_SEMANTIC(MemRange);
74 
75 private:
76     uintptr_t start_address_;  /// < address of the first byte in memory range
77     uintptr_t end_address_;    /// < address of the last byte in memory range
78 };
79 
80 }  // namespace panda::mem
81 
82 #endif  // LIBPANDABASE_MEM_MEM_RANGE_H
83