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.
8 #include <ostream> // NOLINT(readability/streams)
10 #include "src/base/functional.h"
11 #include "src/handles.h"
12 #include "src/utils.h"
23 // Represents a handle to an object on the heap, but with the additional
24 // ability of checking for equality and hashing without accessing the heap.
26 // Creating a Unique<T> requires first dereferencing the handle to obtain
27 // the address of the object, which is used as the hashcode and the basis for
28 // comparison. The object can be moved later by the GC, but comparison
29 // and hashing use the old address of the object, without dereferencing it.
31 // Careful! Comparison of two Uniques is only correct if both were created
32 // in the same "era" of GC or if at least one is a non-movable object.
36 Unique<T>() : raw_address_(NULL) {}
38 // TODO(titzer): make private and introduce a uniqueness scope.
39 explicit Unique(Handle<T> handle) {
40 if (handle.is_null()) {
43 // This is a best-effort check to prevent comparing Unique<T>'s created
44 // in different GC eras; we require heap allocation to be disallowed at
46 // NOTE: we currently consider maps to be non-movable, so no special
47 // assurance is required for creating a Unique<Map>.
48 // TODO(titzer): other immortable immovable objects are also fine.
49 DCHECK(!AllowHeapAllocation::IsAllowed() || handle->IsMap());
50 raw_address_ = reinterpret_cast<Address>(*handle);
51 DCHECK_NOT_NULL(raw_address_); // Non-null should imply non-zero address.
56 // Constructor for handling automatic up casting.
57 // Eg. Unique<JSFunction> can be passed when Unique<Object> is expected.
58 template <class S> Unique(Unique<S> uniq) {
62 a = b; // Fake assignment to enforce type checks.
65 raw_address_ = uniq.raw_address_;
66 handle_ = uniq.handle_;
70 inline bool operator==(const Unique<U>& other) const {
71 DCHECK(IsInitialized() && other.IsInitialized());
72 return raw_address_ == other.raw_address_;
76 inline bool operator!=(const Unique<U>& other) const {
77 DCHECK(IsInitialized() && other.IsInitialized());
78 return raw_address_ != other.raw_address_;
81 friend inline size_t hash_value(Unique<T> const& unique) {
82 DCHECK(unique.IsInitialized());
83 return base::hash<void*>()(unique.raw_address_);
86 inline intptr_t Hashcode() const {
87 DCHECK(IsInitialized());
88 return reinterpret_cast<intptr_t>(raw_address_);
91 inline bool IsNull() const {
92 DCHECK(IsInitialized());
93 return raw_address_ == NULL;
96 inline bool IsKnownGlobal(void* global) const {
97 DCHECK(IsInitialized());
98 return raw_address_ == reinterpret_cast<Address>(global);
101 inline Handle<T> handle() const {
105 template <class S> static Unique<T> cast(Unique<S> that) {
106 // Allow fetching location() to unsafe-cast the handle. This is necessary
107 // since we can't concurrently safe-cast. Safe-casting requires looking at
108 // the heap which may be moving concurrently to the compiler thread.
109 AllowHandleDereference allow_deref;
110 return Unique<T>(that.raw_address_,
111 Handle<T>(reinterpret_cast<T**>(that.handle_.location())));
114 inline bool IsInitialized() const {
115 return raw_address_ != NULL || handle_.is_null();
118 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally.
119 static Unique<T> CreateUninitialized(Handle<T> handle) {
120 return Unique<T>(NULL, handle);
123 static Unique<T> CreateImmovable(Handle<T> handle) {
124 return Unique<T>(reinterpret_cast<Address>(*handle), handle);
128 Unique(Address raw_address, Handle<T> handle)
129 : raw_address_(raw_address), handle_(handle) {}
131 Address raw_address_;
134 friend class UniqueSet<T>; // Uses internal details for speed.
136 friend class Unique; // For comparing raw_address values.
139 template <typename T>
140 inline std::ostream& operator<<(std::ostream& os, Unique<T> uniq) {
141 return os << Brief(*uniq.handle());
145 template <typename T>
146 class UniqueSet final : public ZoneObject {
148 // Constructor. A new set will be empty.
149 UniqueSet() : size_(0), capacity_(0), array_(NULL) { }
151 // Capacity constructor. A new set will be empty.
152 UniqueSet(int capacity, Zone* zone)
153 : size_(0), capacity_(capacity),
154 array_(zone->NewArray<Unique<T> >(capacity)) {
155 DCHECK(capacity <= kMaxCapacity);
158 // Singleton constructor.
159 UniqueSet(Unique<T> uniq, Zone* zone)
160 : size_(1), capacity_(1), array_(zone->NewArray<Unique<T> >(1)) {
164 // Add a new element to this unique set. Mutates this set. O(|this|).
165 void Add(Unique<T> uniq, Zone* zone) {
166 DCHECK(uniq.IsInitialized());
167 // Keep the set sorted by the {raw_address} of the unique elements.
168 for (int i = 0; i < size_; i++) {
169 if (array_[i] == uniq) return;
170 if (array_[i].raw_address_ > uniq.raw_address_) {
171 // Insert in the middle.
172 Grow(size_ + 1, zone);
173 for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j];
179 // Append the element to the the end.
180 Grow(size_ + 1, zone);
181 array_[size_++] = uniq;
184 // Remove an element from this set. Mutates this set. O(|this|)
185 void Remove(Unique<T> uniq) {
186 for (int i = 0; i < size_; i++) {
187 if (array_[i] == uniq) {
188 while (++i < size_) array_[i - 1] = array_[i];
195 // Compare this set against another set. O(|this|).
196 bool Equals(const UniqueSet<T>* that) const {
197 if (that->size_ != this->size_) return false;
198 for (int i = 0; i < this->size_; i++) {
199 if (this->array_[i] != that->array_[i]) return false;
204 // Check whether this set contains the given element. O(|this|)
205 // TODO(titzer): use binary search for large sets to make this O(log|this|)
206 template <typename U>
207 bool Contains(const Unique<U> elem) const {
208 for (int i = 0; i < this->size_; ++i) {
209 Unique<T> cand = this->array_[i];
210 if (cand.raw_address_ >= elem.raw_address_) {
211 return cand.raw_address_ == elem.raw_address_;
217 // Check if this set is a subset of the given set. O(|this| + |that|).
218 bool IsSubset(const UniqueSet<T>* that) const {
219 if (that->size_ < this->size_) return false;
221 for (int i = 0; i < this->size_; i++) {
222 Unique<T> sought = this->array_[i];
224 if (sought == that->array_[j++]) break;
225 // Fail whenever there are more elements in {this} than {that}.
226 if ((this->size_ - i) > (that->size_ - j)) return false;
232 // Returns a new set representing the intersection of this set and the other.
233 // O(|this| + |that|).
234 UniqueSet<T>* Intersect(const UniqueSet<T>* that, Zone* zone) const {
235 if (that->size_ == 0 || this->size_ == 0) return new(zone) UniqueSet<T>();
237 UniqueSet<T>* out = new(zone) UniqueSet<T>(
238 Min(this->size_, that->size_), zone);
240 int i = 0, j = 0, k = 0;
241 while (i < this->size_ && j < that->size_) {
242 Unique<T> a = this->array_[i];
243 Unique<T> b = that->array_[j];
245 out->array_[k++] = a;
248 } else if (a.raw_address_ < b.raw_address_) {
259 // Returns a new set representing the union of this set and the other.
260 // O(|this| + |that|).
261 UniqueSet<T>* Union(const UniqueSet<T>* that, Zone* zone) const {
262 if (that->size_ == 0) return this->Copy(zone);
263 if (this->size_ == 0) return that->Copy(zone);
265 UniqueSet<T>* out = new(zone) UniqueSet<T>(
266 this->size_ + that->size_, zone);
268 int i = 0, j = 0, k = 0;
269 while (i < this->size_ && j < that->size_) {
270 Unique<T> a = this->array_[i];
271 Unique<T> b = that->array_[j];
273 out->array_[k++] = a;
276 } else if (a.raw_address_ < b.raw_address_) {
277 out->array_[k++] = a;
280 out->array_[k++] = b;
285 while (i < this->size_) out->array_[k++] = this->array_[i++];
286 while (j < that->size_) out->array_[k++] = that->array_[j++];
292 // Returns a new set representing all elements from this set which are not in
293 // that set. O(|this| * |that|).
294 UniqueSet<T>* Subtract(const UniqueSet<T>* that, Zone* zone) const {
295 if (that->size_ == 0) return this->Copy(zone);
297 UniqueSet<T>* out = new(zone) UniqueSet<T>(this->size_, zone);
300 while (i < this->size_) {
301 Unique<T> cand = this->array_[i];
302 if (!that->Contains(cand)) {
303 out->array_[j++] = cand;
312 // Makes an exact copy of this set. O(|this|).
313 UniqueSet<T>* Copy(Zone* zone) const {
314 UniqueSet<T>* copy = new(zone) UniqueSet<T>(this->size_, zone);
315 copy->size_ = this->size_;
316 memcpy(copy->array_, this->array_, this->size_ * sizeof(Unique<T>));
324 inline int size() const {
328 inline Unique<T> at(int index) const {
329 DCHECK(index >= 0 && index < size_);
330 return array_[index];
334 // These sets should be small, since operations are implemented with simple
335 // linear algorithms. Enforce a maximum size.
336 static const int kMaxCapacity = 65535;
342 // Grow the size of internal storage to be at least {size} elements.
343 void Grow(int size, Zone* zone) {
344 CHECK(size < kMaxCapacity); // Enforce maximum size.
345 if (capacity_ < size) {
346 int new_capacity = 2 * capacity_ + size;
347 if (new_capacity > kMaxCapacity) new_capacity = kMaxCapacity;
348 Unique<T>* new_array = zone->NewArray<Unique<T> >(new_capacity);
350 memcpy(new_array, array_, size_ * sizeof(Unique<T>));
352 capacity_ = new_capacity;
358 } // namespace internal
361 #endif // V8_UNIQUE_H_