Line data Source code
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 : using Address = uintptr_t;
19 :
20 7357471 : AddressRegion() = default;
21 :
22 : AddressRegion(Address address, size_t size)
23 241842 : : address_(address), size_(size) {}
24 :
25 : Address begin() const { return address_; }
26 11261551 : Address end() const { return address_ + size_; }
27 :
28 : size_t size() const { return size_; }
29 3069253 : void set_size(size_t size) { size_ = size; }
30 :
31 : bool is_empty() const { return size_ == 0; }
32 :
33 : bool contains(Address address) const {
34 : STATIC_ASSERT(std::is_unsigned<Address>::value);
35 1063212 : return (address - begin()) < size();
36 : }
37 :
38 : bool contains(Address address, size_t size) const {
39 : STATIC_ASSERT(std::is_unsigned<Address>::value);
40 8098809 : Address offset = address - begin();
41 8098836 : return (offset < size_) && (offset + size <= size_);
42 : }
43 :
44 : bool contains(AddressRegion region) const {
45 : return contains(region.address_, region.size_);
46 : }
47 :
48 : bool operator==(AddressRegion other) const {
49 21 : return address_ == other.address_ && size_ == other.size_;
50 : }
51 :
52 : bool operator!=(AddressRegion other) const {
53 : return address_ != other.address_ || size_ != other.size_;
54 : }
55 :
56 : private:
57 : Address address_ = 0;
58 : size_t size_ = 0;
59 : };
60 : ASSERT_TRIVIALLY_COPYABLE(AddressRegion);
61 :
62 : inline std::ostream& operator<<(std::ostream& out, AddressRegion region) {
63 : return out << "[" << reinterpret_cast<void*>(region.begin()) << "+"
64 : << region.size() << "]";
65 : }
66 :
67 : } // namespace base
68 : } // namespace v8
69 :
70 : #endif // V8_BASE_ADDRESS_REGION_H_
|