• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_BASE_ADDRESS_REGION_H_
6 #define V8_BASE_ADDRESS_REGION_H_
7 
8 #include <iostream>
9 
10 #include "src/base/macros.h"
11 
12 namespace v8 {
13 namespace base {
14 
15 // Helper class representing an address region of certain size.
16 class AddressRegion {
17  public:
18   // Function object that compares the start address of two regions. Usable as
19   // compare function on std data structures and algorithms.
20   struct StartAddressLess {
operatorStartAddressLess21     bool operator()(base::AddressRegion a, base::AddressRegion b) const {
22       return a.begin() < b.begin();
23     }
24   };
25 
26   using Address = uintptr_t;
27 
28   constexpr AddressRegion() = default;
29 
AddressRegion(Address address,size_t size)30   constexpr AddressRegion(Address address, size_t size)
31       : address_(address), size_(size) {}
32 
begin()33   Address begin() const { return address_; }
end()34   Address end() const { return address_ + size_; }
35 
size()36   size_t size() const { return size_; }
set_size(size_t size)37   void set_size(size_t size) { size_ = size; }
38 
is_empty()39   bool is_empty() const { return size_ == 0; }
40 
contains(Address address)41   bool contains(Address address) const {
42     STATIC_ASSERT(std::is_unsigned<Address>::value);
43     return (address - begin()) < size();
44   }
45 
contains(Address address,size_t size)46   bool contains(Address address, size_t size) const {
47     STATIC_ASSERT(std::is_unsigned<Address>::value);
48     Address offset = address - begin();
49     return (offset < size_) && (offset + size <= size_);
50   }
51 
contains(AddressRegion region)52   bool contains(AddressRegion region) const {
53     return contains(region.address_, region.size_);
54   }
55 
GetOverlap(AddressRegion region)56   base::AddressRegion GetOverlap(AddressRegion region) const {
57     Address overlap_start = std::max(begin(), region.begin());
58     Address overlap_end =
59         std::max(overlap_start, std::min(end(), region.end()));
60     return {overlap_start, overlap_end - overlap_start};
61   }
62 
63   bool operator==(AddressRegion other) const {
64     return address_ == other.address_ && size_ == other.size_;
65   }
66 
67   bool operator!=(AddressRegion other) const {
68     return address_ != other.address_ || size_ != other.size_;
69   }
70 
71  private:
72   Address address_ = 0;
73   size_t size_ = 0;
74 };
75 ASSERT_TRIVIALLY_COPYABLE(AddressRegion);
76 
77 // Construct an AddressRegion from anything providing a {data()} and {size()}
78 // accessor.
79 template <typename Container,
80           typename = decltype(std::declval<Container>().data()),
81           typename = decltype(std::declval<Container>().size())>
AddressRegionOf(Container && c)82 inline constexpr AddressRegion AddressRegionOf(Container&& c) {
83   return AddressRegion{reinterpret_cast<AddressRegion::Address>(c.data()),
84                        sizeof(*c.data()) * c.size()};
85 }
86 
87 inline std::ostream& operator<<(std::ostream& out, AddressRegion region) {
88   return out << "[" << reinterpret_cast<void*>(region.begin()) << "+"
89              << region.size() << "]";
90 }
91 
92 }  // namespace base
93 }  // namespace v8
94 
95 #endif  // V8_BASE_ADDRESS_REGION_H_
96