address-region.h 2.81 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_BASE_ADDRESS_REGION_H_
#define V8_BASE_ADDRESS_REGION_H_

8
#include <iostream>
9 10 11 12 13 14

#include "src/base/macros.h"

namespace v8 {
namespace base {

15
// Helper class representing an address region of certain size.
16 17
class AddressRegion {
 public:
18 19 20 21 22 23 24 25
  // Function object that compares the start address of two regions. Usable as
  // compare function on std data structures and algorithms.
  struct StartAddressLess {
    bool operator()(base::AddressRegion a, base::AddressRegion b) const {
      return a.begin() < b.begin();
    }
  };

26
  using Address = uintptr_t;
27

28
  constexpr AddressRegion() = default;
29

30
  constexpr AddressRegion(Address address, size_t size)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
      : address_(address), size_(size) {}

  Address begin() const { return address_; }
  Address end() const { return address_ + size_; }

  size_t size() const { return size_; }
  void set_size(size_t size) { size_ = size; }

  bool is_empty() const { return size_ == 0; }

  bool contains(Address address) const {
    STATIC_ASSERT(std::is_unsigned<Address>::value);
    return (address - begin()) < size();
  }

  bool contains(Address address, size_t size) const {
    STATIC_ASSERT(std::is_unsigned<Address>::value);
    Address offset = address - begin();
49
    return (offset < size_) && (offset + size <= size_);
50 51
  }

52
  bool contains(AddressRegion region) const {
53 54 55
    return contains(region.address_, region.size_);
  }

56 57 58 59 60 61 62
  base::AddressRegion GetOverlap(AddressRegion region) const {
    Address overlap_start = std::max(begin(), region.begin());
    Address overlap_end =
        std::max(overlap_start, std::min(end(), region.end()));
    return {overlap_start, overlap_end - overlap_start};
  }

63 64 65 66 67 68 69 70
  bool operator==(AddressRegion other) const {
    return address_ == other.address_ && size_ == other.size_;
  }

  bool operator!=(AddressRegion other) const {
    return address_ != other.address_ || size_ != other.size_;
  }

71 72 73 74
 private:
  Address address_ = 0;
  size_t size_ = 0;
};
75 76
ASSERT_TRIVIALLY_COPYABLE(AddressRegion);

77 78 79 80 81 82 83 84 85 86
// Construct an AddressRegion from anything providing a {data()} and {size()}
// accessor.
template <typename Container,
          typename = decltype(std::declval<Container>().data()),
          typename = decltype(std::declval<Container>().size())>
inline constexpr AddressRegion AddressRegionOf(Container&& c) {
  return AddressRegion{reinterpret_cast<AddressRegion::Address>(c.data()),
                       sizeof(*c.data()) * c.size()};
}

87 88 89 90
inline std::ostream& operator<<(std::ostream& out, AddressRegion region) {
  return out << "[" << reinterpret_cast<void*>(region.begin()) << "+"
             << region.size() << "]";
}
91 92 93 94 95

}  // namespace base
}  // namespace v8

#endif  // V8_BASE_ADDRESS_REGION_H_