v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
address-region.h
Go to the documentation of this file.
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
12namespace v8 {
13namespace base {
14
15// Helper class representing an address region of certain size.
17 public:
18 // Function object that compares the start address of two regions. Usable as
19 // compare function on std data structures and algorithms.
22 return a.begin() < b.begin();
23 }
24 };
25
26 using Address = uintptr_t;
27
28 constexpr AddressRegion() = default;
29
30 constexpr AddressRegion(Address address, size_t size)
31 : address_(address), size_(size) {}
32
33 Address begin() const { return address_; }
34 Address end() const { return address_ + size_; }
35
36 size_t size() const { return size_; }
37 void set_size(size_t size) { size_ = size; }
38
39 bool is_empty() const { return size_ == 0; }
40
41 bool contains(Address address) const {
42 static_assert(std::is_unsigned<Address>::value);
43 return (address - begin()) < size();
44 }
45
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
52 bool contains(AddressRegion region) const {
53 return contains(region.address_, region.size_);
54 }
55
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:
73 size_t size_ = 0;
74};
76
77// Construct an AddressRegion from a start pointer and a size.
78template <typename T>
79inline AddressRegion AddressRegionOf(T* ptr, size_t size) {
80 return AddressRegion{reinterpret_cast<AddressRegion::Address>(ptr),
81 sizeof(T) * size};
82}
83
84// Construct an AddressRegion from anything providing a {data()} and {size()}
85// accessor.
86template <typename Container>
87inline auto AddressRegionOf(Container&& c)
88 -> decltype(AddressRegionOf(c.data(), c.size())) {
89 return AddressRegionOf(c.data(), c.size());
90}
91
92inline std::ostream& operator<<(std::ostream& out, AddressRegion region) {
93 return out << "[" << reinterpret_cast<void*>(region.begin()) << "+"
94 << region.size() << "]";
95}
96
97} // namespace base
98} // namespace v8
99
100#endif // V8_BASE_ADDRESS_REGION_H_
constexpr AddressRegion(Address address, size_t size)
base::AddressRegion GetOverlap(AddressRegion region) const
bool operator==(AddressRegion other) const
bool contains(Address address, size_t size) const
bool contains(Address address) const
bool contains(AddressRegion region) const
void set_size(size_t size)
constexpr AddressRegion()=default
bool operator!=(AddressRegion other) const
int32_t offset
AddressRegion AddressRegionOf(T *ptr, size_t size)
std::ostream & operator<<(std::ostream &out, AddressRegion region)
#define ASSERT_TRIVIALLY_COPYABLE(T)
Definition macros.h:267
bool operator()(base::AddressRegion a, base::AddressRegion b) const