OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_HYDROGEN_UNIQUE_H_ | 5 #ifndef V8_HYDROGEN_UNIQUE_H_ |
6 #define V8_HYDROGEN_UNIQUE_H_ | 6 #define V8_HYDROGEN_UNIQUE_H_ |
7 | 7 |
8 #include <ostream> // NOLINT(readability/streams) | 8 #include <ostream> // NOLINT(readability/streams) |
9 | 9 |
10 #include "src/base/functional.h" | 10 #include "src/base/functional.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 // ability of checking for equality and hashing without accessing the heap. | 25 // ability of checking for equality and hashing without accessing the heap. |
26 // | 26 // |
27 // Creating a Unique<T> requires first dereferencing the handle to obtain | 27 // Creating a Unique<T> requires first dereferencing the handle to obtain |
28 // the address of the object, which is used as the hashcode and the basis for | 28 // the address of the object, which is used as the hashcode and the basis for |
29 // comparison. The object can be moved later by the GC, but comparison | 29 // comparison. The object can be moved later by the GC, but comparison |
30 // and hashing use the old address of the object, without dereferencing it. | 30 // and hashing use the old address of the object, without dereferencing it. |
31 // | 31 // |
32 // Careful! Comparison of two Uniques is only correct if both were created | 32 // Careful! Comparison of two Uniques is only correct if both were created |
33 // in the same "era" of GC or if at least one is a non-movable object. | 33 // in the same "era" of GC or if at least one is a non-movable object. |
34 template <typename T> | 34 template <typename T> |
35 class Unique { | 35 class Unique final { |
36 public: | 36 public: |
37 Unique<T>() : raw_address_(NULL) {} | 37 Unique<T>() : raw_address_(NULL) {} |
38 | 38 |
39 // TODO(titzer): make private and introduce a uniqueness scope. | 39 // TODO(titzer): make private and introduce a uniqueness scope. |
40 explicit Unique(Handle<T> handle) { | 40 explicit Unique(Handle<T> handle) { |
41 if (handle.is_null()) { | 41 if (handle.is_null()) { |
42 raw_address_ = NULL; | 42 raw_address_ = NULL; |
43 } else { | 43 } else { |
44 // This is a best-effort check to prevent comparing Unique<T>'s created | 44 // This is a best-effort check to prevent comparing Unique<T>'s created |
45 // in different GC eras; we require heap allocation to be disallowed at | 45 // in different GC eras; we require heap allocation to be disallowed at |
46 // creation time. | 46 // creation time. |
47 // NOTE: we currently consider maps to be non-movable, so no special | 47 // NOTE: we currently consider maps to be non-movable, so no special |
48 // assurance is required for creating a Unique<Map>. | 48 // assurance is required for creating a Unique<Map>. |
49 // TODO(titzer): other immortable immovable objects are also fine. | 49 // TODO(titzer): other immortable immovable objects are also fine. |
50 DCHECK(!AllowHeapAllocation::IsAllowed() || handle->IsMap()); | 50 DCHECK(!AllowHeapAllocation::IsAllowed() || handle->IsMap()); |
51 raw_address_ = reinterpret_cast<Address>(*handle); | 51 raw_address_ = reinterpret_cast<Address>(*handle); |
52 DCHECK_NOT_NULL(raw_address_); // Non-null should imply non-zero address. | 52 DCHECK_NOT_NULL(raw_address_); // Non-null should imply non-zero address. |
53 } | 53 } |
54 handle_ = handle; | 54 handle_ = handle; |
55 } | 55 } |
56 | 56 |
57 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. | |
58 Unique(Address raw_address, Handle<T> handle) | |
59 : raw_address_(raw_address), handle_(handle) { } | |
60 | |
61 // Constructor for handling automatic up casting. | 57 // Constructor for handling automatic up casting. |
62 // Eg. Unique<JSFunction> can be passed when Unique<Object> is expected. | 58 // Eg. Unique<JSFunction> can be passed when Unique<Object> is expected. |
63 template <class S> Unique(Unique<S> uniq) { | 59 template <class S> Unique(Unique<S> uniq) { |
64 #ifdef DEBUG | 60 #ifdef DEBUG |
65 T* a = NULL; | 61 T* a = NULL; |
66 S* b = NULL; | 62 S* b = NULL; |
67 a = b; // Fake assignment to enforce type checks. | 63 a = b; // Fake assignment to enforce type checks. |
68 USE(a); | 64 USE(a); |
69 #endif | 65 #endif |
70 raw_address_ = uniq.raw_address_; | 66 raw_address_ = uniq.raw_address_; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 | 118 |
123 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. | 119 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. |
124 static Unique<T> CreateUninitialized(Handle<T> handle) { | 120 static Unique<T> CreateUninitialized(Handle<T> handle) { |
125 return Unique<T>(NULL, handle); | 121 return Unique<T>(NULL, handle); |
126 } | 122 } |
127 | 123 |
128 static Unique<T> CreateImmovable(Handle<T> handle) { | 124 static Unique<T> CreateImmovable(Handle<T> handle) { |
129 return Unique<T>(reinterpret_cast<Address>(*handle), handle); | 125 return Unique<T>(reinterpret_cast<Address>(*handle), handle); |
130 } | 126 } |
131 | 127 |
| 128 private: |
| 129 Unique(Address raw_address, Handle<T> handle) |
| 130 : raw_address_(raw_address), handle_(handle) {} |
| 131 |
| 132 Address raw_address_; |
| 133 Handle<T> handle_; |
| 134 |
132 friend class UniqueSet<T>; // Uses internal details for speed. | 135 friend class UniqueSet<T>; // Uses internal details for speed. |
133 template <class U> | 136 template <class U> |
134 friend class Unique; // For comparing raw_address values. | 137 friend class Unique; // For comparing raw_address values. |
135 | |
136 protected: | |
137 Address raw_address_; | |
138 Handle<T> handle_; | |
139 | |
140 friend class SideEffectsTracker; | |
141 }; | 138 }; |
142 | 139 |
143 template <typename T> | 140 template <typename T> |
144 inline std::ostream& operator<<(std::ostream& os, Unique<T> uniq) { | 141 inline std::ostream& operator<<(std::ostream& os, Unique<T> uniq) { |
145 return os << Brief(*uniq.handle()); | 142 return os << Brief(*uniq.handle()); |
146 } | 143 } |
147 | 144 |
148 | 145 |
149 template <typename T> | 146 template <typename T> |
150 class UniqueSet final : public ZoneObject { | 147 class UniqueSet final : public ZoneObject { |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 } | 352 } |
356 capacity_ = new_capacity; | 353 capacity_ = new_capacity; |
357 array_ = new_array; | 354 array_ = new_array; |
358 } | 355 } |
359 } | 356 } |
360 }; | 357 }; |
361 | 358 |
362 } } // namespace v8::internal | 359 } } // namespace v8::internal |
363 | 360 |
364 #endif // V8_HYDROGEN_UNIQUE_H_ | 361 #endif // V8_HYDROGEN_UNIQUE_H_ |
OLD | NEW |