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
3 // found in the LICENSE file.
5 #ifndef V8_HYDROGEN_UNIQUE_H_
6 #define V8_HYDROGEN_UNIQUE_H_
8 #include <ostream> // NOLINT(readability/streams)
10 #include "src/base/functional.h"
11 #include "src/handles-inl.h" // TODO(everyone): Fix our inl.h crap
12 #include "src/objects-inl.h" // TODO(everyone): Fix our inl.h crap
13 #include "src/utils.h"
24 // Represents a handle to an object on the heap, but with the additional
25 // ability of checking for equality and hashing without accessing the heap.
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
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.
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.
37 Unique<T>() : raw_address_(NULL) {}
39 // TODO(titzer): make private and introduce a uniqueness scope.
40 explicit Unique(Handle<T> handle) {
41 if (handle.is_null()) {
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
47 // NOTE: we currently consider maps to be non-movable, so no special
48 // assurance is required for creating a Unique<Map>.
49 // TODO(titzer): other immortable immovable objects are also fine.
50 DCHECK(!AllowHeapAllocation::IsAllowed() || handle->IsMap());
51 raw_address_ = reinterpret_cast<Address>(*handle);
52 DCHECK_NOT_NULL(raw_address_); // Non-null should imply non-zero address.
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) { }
61 // Constructor for handling automatic up casting.
62 // Eg. Unique<JSFunction> can be passed when Unique<Object> is expected.
63 template <class S> Unique(Unique<S> uniq) {
67 a = b; // Fake assignment to enforce type checks.
70 raw_address_ = uniq.raw_address_;
71 handle_ = uniq.handle_;
75 inline bool operator==(const Unique<U>& other) const {
76 DCHECK(IsInitialized() && other.IsInitialized());
77 return raw_address_ == other.raw_address_;
81 inline bool operator!=(const Unique<U>& other) const {
82 DCHECK(IsInitialized() && other.IsInitialized());
83 return raw_address_ != other.raw_address_;
86 friend inline size_t hash_value(Unique<T> const& unique) {
87 DCHECK(unique.IsInitialized());
88 return base::hash<void*>()(unique.raw_address_);
91 inline intptr_t Hashcode() const {
92 DCHECK(IsInitialized());
93 return reinterpret_cast<intptr_t>(raw_address_);
96 inline bool IsNull() const {
97 DCHECK(IsInitialized());
98 return raw_address_ == NULL;
101 inline bool IsKnownGlobal(void* global) const {
102 DCHECK(IsInitialized());
103 return raw_address_ == reinterpret_cast<Address>(global);
106 inline Handle<T> handle() const {
110 template <class S> static Unique<T> cast(Unique<S> that) {
111 // Allow fetching location() to unsafe-cast the handle. This is necessary
112 // since we can't concurrently safe-cast. Safe-casting requires looking at
113 // the heap which may be moving concurrently to the compiler thread.
114 AllowHandleDereference allow_deref;
115 return Unique<T>(that.raw_address_,
116 Handle<T>(reinterpret_cast<T**>(that.handle_.location())));
119 inline bool IsInitialized() const {
120 return raw_address_ != NULL || handle_.is_null();
123 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally.
124 static Unique<T> CreateUninitialized(Handle<T> handle) {
125 return Unique<T>(NULL, handle);
128 static Unique<T> CreateImmovable(Handle<T> handle) {
129 return Unique<T>(reinterpret_cast<Address>(*handle), handle);
132 friend class UniqueSet<T>; // Uses internal details for speed.
134 friend class Unique; // For comparing raw_address values.
137 Address raw_address_;
140 friend class SideEffectsTracker;
143 template <typename T>
144 inline std::ostream& operator<<(std::ostream& os, Unique<T> uniq) {
145 return os << Brief(*uniq.handle());
149 template <typename T>
150 class UniqueSet FINAL : public ZoneObject {
152 // Constructor. A new set will be empty.
153 UniqueSet() : size_(0), capacity_(0), array_(NULL) { }
155 // Capacity constructor. A new set will be empty.
156 UniqueSet(int capacity, Zone* zone)
157 : size_(0), capacity_(capacity),
158 array_(zone->NewArray<Unique<T> >(capacity)) {
159 DCHECK(capacity <= kMaxCapacity);
162 // Singleton constructor.
163 UniqueSet(Unique<T> uniq, Zone* zone)
164 : size_(1), capacity_(1), array_(zone->NewArray<Unique<T> >(1)) {
168 // Add a new element to this unique set. Mutates this set. O(|this|).
169 void Add(Unique<T> uniq, Zone* zone) {
170 DCHECK(uniq.IsInitialized());
171 // Keep the set sorted by the {raw_address} of the unique elements.
172 for (int i = 0; i < size_; i++) {
173 if (array_[i] == uniq) return;
174 if (array_[i].raw_address_ > uniq.raw_address_) {
175 // Insert in the middle.
176 Grow(size_ + 1, zone);
177 for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j];
183 // Append the element to the the end.
184 Grow(size_ + 1, zone);
185 array_[size_++] = uniq;
188 // Remove an element from this set. Mutates this set. O(|this|)
189 void Remove(Unique<T> uniq) {
190 for (int i = 0; i < size_; i++) {
191 if (array_[i] == uniq) {
192 while (++i < size_) array_[i - 1] = array_[i];
199 // Compare this set against another set. O(|this|).
200 bool Equals(const UniqueSet<T>* that) const {
201 if (that->size_ != this->size_) return false;
202 for (int i = 0; i < this->size_; i++) {
203 if (this->array_[i] != that->array_[i]) return false;
208 // Check whether this set contains the given element. O(|this|)
209 // TODO(titzer): use binary search for large sets to make this O(log|this|)
210 template <typename U>
211 bool Contains(const Unique<U> elem) const {
212 for (int i = 0; i < this->size_; ++i) {
213 Unique<T> cand = this->array_[i];
214 if (cand.raw_address_ >= elem.raw_address_) {
215 return cand.raw_address_ == elem.raw_address_;
221 // Check if this set is a subset of the given set. O(|this| + |that|).
222 bool IsSubset(const UniqueSet<T>* that) const {
223 if (that->size_ < this->size_) return false;
225 for (int i = 0; i < this->size_; i++) {
226 Unique<T> sought = this->array_[i];
228 if (sought == that->array_[j++]) break;
229 // Fail whenever there are more elements in {this} than {that}.
230 if ((this->size_ - i) > (that->size_ - j)) return false;
236 // Returns a new set representing the intersection of this set and the other.
237 // O(|this| + |that|).
238 UniqueSet<T>* Intersect(const UniqueSet<T>* that, Zone* zone) const {
239 if (that->size_ == 0 || this->size_ == 0) return new(zone) UniqueSet<T>();
241 UniqueSet<T>* out = new(zone) UniqueSet<T>(
242 Min(this->size_, that->size_), zone);
244 int i = 0, j = 0, k = 0;
245 while (i < this->size_ && j < that->size_) {
246 Unique<T> a = this->array_[i];
247 Unique<T> b = that->array_[j];
249 out->array_[k++] = a;
252 } else if (a.raw_address_ < b.raw_address_) {
263 // Returns a new set representing the union of this set and the other.
264 // O(|this| + |that|).
265 UniqueSet<T>* Union(const UniqueSet<T>* that, Zone* zone) const {
266 if (that->size_ == 0) return this->Copy(zone);
267 if (this->size_ == 0) return that->Copy(zone);
269 UniqueSet<T>* out = new(zone) UniqueSet<T>(
270 this->size_ + that->size_, zone);
272 int i = 0, j = 0, k = 0;
273 while (i < this->size_ && j < that->size_) {
274 Unique<T> a = this->array_[i];
275 Unique<T> b = that->array_[j];
277 out->array_[k++] = a;
280 } else if (a.raw_address_ < b.raw_address_) {
281 out->array_[k++] = a;
284 out->array_[k++] = b;
289 while (i < this->size_) out->array_[k++] = this->array_[i++];
290 while (j < that->size_) out->array_[k++] = that->array_[j++];
296 // Returns a new set representing all elements from this set which are not in
297 // that set. O(|this| * |that|).
298 UniqueSet<T>* Subtract(const UniqueSet<T>* that, Zone* zone) const {
299 if (that->size_ == 0) return this->Copy(zone);
301 UniqueSet<T>* out = new(zone) UniqueSet<T>(this->size_, zone);
304 while (i < this->size_) {
305 Unique<T> cand = this->array_[i];
306 if (!that->Contains(cand)) {
307 out->array_[j++] = cand;
316 // Makes an exact copy of this set. O(|this|).
317 UniqueSet<T>* Copy(Zone* zone) const {
318 UniqueSet<T>* copy = new(zone) UniqueSet<T>(this->size_, zone);
319 copy->size_ = this->size_;
320 memcpy(copy->array_, this->array_, this->size_ * sizeof(Unique<T>));
328 inline int size() const {
332 inline Unique<T> at(int index) const {
333 DCHECK(index >= 0 && index < size_);
334 return array_[index];
338 // These sets should be small, since operations are implemented with simple
339 // linear algorithms. Enforce a maximum size.
340 static const int kMaxCapacity = 65535;
346 // Grow the size of internal storage to be at least {size} elements.
347 void Grow(int size, Zone* zone) {
348 CHECK(size < kMaxCapacity); // Enforce maximum size.
349 if (capacity_ < size) {
350 int new_capacity = 2 * capacity_ + size;
351 if (new_capacity > kMaxCapacity) new_capacity = kMaxCapacity;
352 Unique<T>* new_array = zone->NewArray<Unique<T> >(new_capacity);
354 memcpy(new_array, array_, size_ * sizeof(Unique<T>));
356 capacity_ = new_capacity;
362 } } // namespace v8::internal
364 #endif // V8_HYDROGEN_UNIQUE_H_