2013-09-12 12:08:54 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-09-12 12:08:54 +00:00
|
|
|
|
|
|
|
#ifndef V8_HYDROGEN_UNIQUE_H_
|
|
|
|
#define V8_HYDROGEN_UNIQUE_H_
|
|
|
|
|
2014-09-11 10:37:49 +00:00
|
|
|
#include "src/handles-inl.h" // TODO(everyone): Fix our inl.h crap
|
|
|
|
#include "src/objects-inl.h" // TODO(everyone): Fix our inl.h crap
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "src/string-stream.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/utils.h"
|
|
|
|
#include "src/zone.h"
|
2013-09-12 12:08:54 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class UniqueSet;
|
|
|
|
|
|
|
|
|
|
|
|
// Represents a handle to an object on the heap, but with the additional
|
|
|
|
// ability of checking for equality and hashing without accessing the heap.
|
|
|
|
//
|
|
|
|
// Creating a Unique<T> requires first dereferencing the handle to obtain
|
|
|
|
// the address of the object, which is used as the hashcode and the basis for
|
|
|
|
// comparison. The object can be moved later by the GC, but comparison
|
|
|
|
// and hashing use the old address of the object, without dereferencing it.
|
|
|
|
//
|
|
|
|
// Careful! Comparison of two Uniques is only correct if both were created
|
|
|
|
// in the same "era" of GC or if at least one is a non-movable object.
|
|
|
|
template <typename T>
|
2014-07-30 13:54:45 +00:00
|
|
|
class Unique {
|
2013-09-12 12:08:54 +00:00
|
|
|
public:
|
2014-09-08 09:16:11 +00:00
|
|
|
Unique<T>() : raw_address_(NULL) {}
|
|
|
|
|
2013-09-24 09:48:39 +00:00
|
|
|
// TODO(titzer): make private and introduce a uniqueness scope.
|
2013-09-12 12:08:54 +00:00
|
|
|
explicit Unique(Handle<T> handle) {
|
|
|
|
if (handle.is_null()) {
|
|
|
|
raw_address_ = NULL;
|
|
|
|
} else {
|
2013-09-19 17:02:57 +00:00
|
|
|
// This is a best-effort check to prevent comparing Unique<T>'s created
|
|
|
|
// in different GC eras; we require heap allocation to be disallowed at
|
|
|
|
// creation time.
|
|
|
|
// NOTE: we currently consider maps to be non-movable, so no special
|
|
|
|
// assurance is required for creating a Unique<Map>.
|
|
|
|
// TODO(titzer): other immortable immovable objects are also fine.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!AllowHeapAllocation::IsAllowed() || handle->IsMap());
|
2013-09-12 12:08:54 +00:00
|
|
|
raw_address_ = reinterpret_cast<Address>(*handle);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK_NE(raw_address_, NULL); // Non-null should imply non-zero address.
|
2013-09-12 12:08:54 +00:00
|
|
|
}
|
|
|
|
handle_ = handle;
|
|
|
|
}
|
|
|
|
|
2013-09-13 12:35:36 +00:00
|
|
|
// TODO(titzer): this is a hack to migrate to Unique<T> incrementally.
|
|
|
|
Unique(Address raw_address, Handle<T> handle)
|
|
|
|
: raw_address_(raw_address), handle_(handle) { }
|
|
|
|
|
2013-09-12 12:08:54 +00:00
|
|
|
// Constructor for handling automatic up casting.
|
2013-09-19 17:02:57 +00:00
|
|
|
// Eg. Unique<JSFunction> can be passed when Unique<Object> is expected.
|
2013-09-12 12:08:54 +00:00
|
|
|
template <class S> Unique(Unique<S> uniq) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
T* a = NULL;
|
|
|
|
S* b = NULL;
|
|
|
|
a = b; // Fake assignment to enforce type checks.
|
|
|
|
USE(a);
|
|
|
|
#endif
|
|
|
|
raw_address_ = uniq.raw_address_;
|
2013-09-19 17:02:57 +00:00
|
|
|
handle_ = uniq.handle_;
|
2013-09-12 12:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
2013-09-20 12:32:31 +00:00
|
|
|
inline bool operator==(const Unique<U>& other) const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsInitialized() && other.IsInitialized());
|
2013-09-12 12:08:54 +00:00
|
|
|
return raw_address_ == other.raw_address_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
2013-09-20 12:32:31 +00:00
|
|
|
inline bool operator!=(const Unique<U>& other) const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsInitialized() && other.IsInitialized());
|
2013-09-12 12:08:54 +00:00
|
|
|
return raw_address_ != other.raw_address_;
|
|
|
|
}
|
|
|
|
|
2013-09-20 12:32:31 +00:00
|
|
|
inline intptr_t Hashcode() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsInitialized());
|
2013-09-12 12:08:54 +00:00
|
|
|
return reinterpret_cast<intptr_t>(raw_address_);
|
|
|
|
}
|
|
|
|
|
2013-09-20 12:32:31 +00:00
|
|
|
inline bool IsNull() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsInitialized());
|
2013-09-12 12:08:54 +00:00
|
|
|
return raw_address_ == NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-24 09:48:39 +00:00
|
|
|
inline bool IsKnownGlobal(void* global) const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsInitialized());
|
2013-09-24 09:48:39 +00:00
|
|
|
return raw_address_ == reinterpret_cast<Address>(global);
|
|
|
|
}
|
|
|
|
|
2013-09-20 12:32:31 +00:00
|
|
|
inline Handle<T> handle() const {
|
2013-09-12 12:08:54 +00:00
|
|
|
return handle_;
|
|
|
|
}
|
|
|
|
|
2013-09-26 16:25:57 +00:00
|
|
|
template <class S> static Unique<T> cast(Unique<S> that) {
|
|
|
|
return Unique<T>(that.raw_address_, Handle<T>::cast(that.handle_));
|
|
|
|
}
|
|
|
|
|
2013-09-20 12:32:31 +00:00
|
|
|
inline bool IsInitialized() const {
|
2013-09-19 09:07:27 +00:00
|
|
|
return raw_address_ != NULL || handle_.is_null();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(titzer): this is a hack to migrate to Unique<T> incrementally.
|
|
|
|
static Unique<T> CreateUninitialized(Handle<T> handle) {
|
2013-09-24 09:48:39 +00:00
|
|
|
return Unique<T>(reinterpret_cast<Address>(NULL), handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Unique<T> CreateImmovable(Handle<T> handle) {
|
|
|
|
return Unique<T>(reinterpret_cast<Address>(*handle), handle);
|
2013-09-19 09:07:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 12:08:54 +00:00
|
|
|
friend class UniqueSet<T>; // Uses internal details for speed.
|
|
|
|
template <class U>
|
|
|
|
friend class Unique; // For comparing raw_address values.
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
protected:
|
2013-09-12 12:08:54 +00:00
|
|
|
Address raw_address_;
|
|
|
|
Handle<T> handle_;
|
2014-03-07 10:14:03 +00:00
|
|
|
|
|
|
|
friend class SideEffectsTracker;
|
2013-09-12 12:08:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2014-09-02 07:07:52 +00:00
|
|
|
class UniqueSet FINAL : public ZoneObject {
|
2013-09-12 12:08:54 +00:00
|
|
|
public:
|
|
|
|
// Constructor. A new set will be empty.
|
|
|
|
UniqueSet() : size_(0), capacity_(0), array_(NULL) { }
|
|
|
|
|
2014-05-05 06:53:19 +00:00
|
|
|
// Capacity constructor. A new set will be empty.
|
|
|
|
UniqueSet(int capacity, Zone* zone)
|
|
|
|
: size_(0), capacity_(capacity),
|
|
|
|
array_(zone->NewArray<Unique<T> >(capacity)) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(capacity <= kMaxCapacity);
|
2014-05-05 06:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Singleton constructor.
|
|
|
|
UniqueSet(Unique<T> uniq, Zone* zone)
|
|
|
|
: size_(1), capacity_(1), array_(zone->NewArray<Unique<T> >(1)) {
|
|
|
|
array_[0] = uniq;
|
|
|
|
}
|
|
|
|
|
2013-09-12 12:08:54 +00:00
|
|
|
// Add a new element to this unique set. Mutates this set. O(|this|).
|
|
|
|
void Add(Unique<T> uniq, Zone* zone) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(uniq.IsInitialized());
|
2013-09-12 12:08:54 +00:00
|
|
|
// Keep the set sorted by the {raw_address} of the unique elements.
|
|
|
|
for (int i = 0; i < size_; i++) {
|
|
|
|
if (array_[i] == uniq) return;
|
|
|
|
if (array_[i].raw_address_ > uniq.raw_address_) {
|
|
|
|
// Insert in the middle.
|
|
|
|
Grow(size_ + 1, zone);
|
|
|
|
for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j];
|
|
|
|
array_[i] = uniq;
|
|
|
|
size_++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Append the element to the the end.
|
|
|
|
Grow(size_ + 1, zone);
|
|
|
|
array_[size_++] = uniq;
|
|
|
|
}
|
|
|
|
|
2013-09-26 16:25:57 +00:00
|
|
|
// Remove an element from this set. Mutates this set. O(|this|)
|
|
|
|
void Remove(Unique<T> uniq) {
|
|
|
|
for (int i = 0; i < size_; i++) {
|
|
|
|
if (array_[i] == uniq) {
|
|
|
|
while (++i < size_) array_[i - 1] = array_[i];
|
|
|
|
size_--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-12 12:08:54 +00:00
|
|
|
// Compare this set against another set. O(|this|).
|
2014-04-22 07:24:05 +00:00
|
|
|
bool Equals(const UniqueSet<T>* that) const {
|
2013-09-12 12:08:54 +00:00
|
|
|
if (that->size_ != this->size_) return false;
|
|
|
|
for (int i = 0; i < this->size_; i++) {
|
|
|
|
if (this->array_[i] != that->array_[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-24 09:48:39 +00:00
|
|
|
// Check whether this set contains the given element. O(|this|)
|
|
|
|
// TODO(titzer): use binary search for large sets to make this O(log|this|)
|
2013-09-13 12:35:36 +00:00
|
|
|
template <typename U>
|
2014-04-22 07:24:05 +00:00
|
|
|
bool Contains(const Unique<U> elem) const {
|
2014-05-05 06:53:19 +00:00
|
|
|
for (int i = 0; i < this->size_; ++i) {
|
|
|
|
Unique<T> cand = this->array_[i];
|
|
|
|
if (cand.raw_address_ >= elem.raw_address_) {
|
|
|
|
return cand.raw_address_ == elem.raw_address_;
|
|
|
|
}
|
2013-09-13 12:35:36 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-12 12:08:54 +00:00
|
|
|
// Check if this set is a subset of the given set. O(|this| + |that|).
|
2014-04-22 07:24:05 +00:00
|
|
|
bool IsSubset(const UniqueSet<T>* that) const {
|
2013-09-12 12:08:54 +00:00
|
|
|
if (that->size_ < this->size_) return false;
|
|
|
|
int j = 0;
|
|
|
|
for (int i = 0; i < this->size_; i++) {
|
|
|
|
Unique<T> sought = this->array_[i];
|
|
|
|
while (true) {
|
|
|
|
if (sought == that->array_[j++]) break;
|
|
|
|
// Fail whenever there are more elements in {this} than {that}.
|
|
|
|
if ((this->size_ - i) > (that->size_ - j)) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a new set representing the intersection of this set and the other.
|
|
|
|
// O(|this| + |that|).
|
2014-05-05 06:53:19 +00:00
|
|
|
UniqueSet<T>* Intersect(const UniqueSet<T>* that, Zone* zone) const {
|
2013-09-12 12:08:54 +00:00
|
|
|
if (that->size_ == 0 || this->size_ == 0) return new(zone) UniqueSet<T>();
|
|
|
|
|
2014-05-05 06:53:19 +00:00
|
|
|
UniqueSet<T>* out = new(zone) UniqueSet<T>(
|
|
|
|
Min(this->size_, that->size_), zone);
|
2013-09-12 12:08:54 +00:00
|
|
|
|
|
|
|
int i = 0, j = 0, k = 0;
|
|
|
|
while (i < this->size_ && j < that->size_) {
|
|
|
|
Unique<T> a = this->array_[i];
|
|
|
|
Unique<T> b = that->array_[j];
|
|
|
|
if (a == b) {
|
|
|
|
out->array_[k++] = a;
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
} else if (a.raw_address_ < b.raw_address_) {
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out->size_ = k;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a new set representing the union of this set and the other.
|
|
|
|
// O(|this| + |that|).
|
2014-05-05 06:53:19 +00:00
|
|
|
UniqueSet<T>* Union(const UniqueSet<T>* that, Zone* zone) const {
|
2013-09-12 12:08:54 +00:00
|
|
|
if (that->size_ == 0) return this->Copy(zone);
|
|
|
|
if (this->size_ == 0) return that->Copy(zone);
|
|
|
|
|
2014-05-05 06:53:19 +00:00
|
|
|
UniqueSet<T>* out = new(zone) UniqueSet<T>(
|
|
|
|
this->size_ + that->size_, zone);
|
2013-09-12 12:08:54 +00:00
|
|
|
|
|
|
|
int i = 0, j = 0, k = 0;
|
|
|
|
while (i < this->size_ && j < that->size_) {
|
|
|
|
Unique<T> a = this->array_[i];
|
|
|
|
Unique<T> b = that->array_[j];
|
|
|
|
if (a == b) {
|
|
|
|
out->array_[k++] = a;
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
} else if (a.raw_address_ < b.raw_address_) {
|
|
|
|
out->array_[k++] = a;
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
out->array_[k++] = b;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i < this->size_) out->array_[k++] = this->array_[i++];
|
|
|
|
while (j < that->size_) out->array_[k++] = that->array_[j++];
|
|
|
|
|
|
|
|
out->size_ = k;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2014-06-02 12:17:05 +00:00
|
|
|
// Returns a new set representing all elements from this set which are not in
|
|
|
|
// that set. O(|this| * |that|).
|
|
|
|
UniqueSet<T>* Subtract(const UniqueSet<T>* that, Zone* zone) const {
|
|
|
|
if (that->size_ == 0) return this->Copy(zone);
|
|
|
|
|
|
|
|
UniqueSet<T>* out = new(zone) UniqueSet<T>(this->size_, zone);
|
|
|
|
|
|
|
|
int i = 0, j = 0;
|
|
|
|
while (i < this->size_) {
|
|
|
|
Unique<T> cand = this->array_[i];
|
|
|
|
if (!that->Contains(cand)) {
|
|
|
|
out->array_[j++] = cand;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
out->size_ = j;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:42:21 +00:00
|
|
|
// Makes an exact copy of this set. O(|this|).
|
2013-09-13 12:35:36 +00:00
|
|
|
UniqueSet<T>* Copy(Zone* zone) const {
|
2014-05-05 06:53:19 +00:00
|
|
|
UniqueSet<T>* copy = new(zone) UniqueSet<T>(this->size_, zone);
|
2013-09-12 12:08:54 +00:00
|
|
|
copy->size_ = this->size_;
|
|
|
|
memcpy(copy->array_, this->array_, this->size_ * sizeof(Unique<T>));
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2013-09-26 16:25:57 +00:00
|
|
|
void Clear() {
|
|
|
|
size_ = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-13 12:35:36 +00:00
|
|
|
inline int size() const {
|
2013-09-12 12:08:54 +00:00
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
2013-09-13 12:35:36 +00:00
|
|
|
inline Unique<T> at(int index) const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(index >= 0 && index < size_);
|
2013-09-13 12:35:36 +00:00
|
|
|
return array_[index];
|
|
|
|
}
|
|
|
|
|
2013-09-12 12:08:54 +00:00
|
|
|
private:
|
|
|
|
// These sets should be small, since operations are implemented with simple
|
|
|
|
// linear algorithms. Enforce a maximum size.
|
|
|
|
static const int kMaxCapacity = 65535;
|
|
|
|
|
|
|
|
uint16_t size_;
|
|
|
|
uint16_t capacity_;
|
|
|
|
Unique<T>* array_;
|
|
|
|
|
|
|
|
// Grow the size of internal storage to be at least {size} elements.
|
|
|
|
void Grow(int size, Zone* zone) {
|
|
|
|
CHECK(size < kMaxCapacity); // Enforce maximum size.
|
|
|
|
if (capacity_ < size) {
|
|
|
|
int new_capacity = 2 * capacity_ + size;
|
|
|
|
if (new_capacity > kMaxCapacity) new_capacity = kMaxCapacity;
|
|
|
|
Unique<T>* new_array = zone->NewArray<Unique<T> >(new_capacity);
|
|
|
|
if (size_ > 0) {
|
|
|
|
memcpy(new_array, array_, size_ * sizeof(Unique<T>));
|
|
|
|
}
|
|
|
|
capacity_ = new_capacity;
|
|
|
|
array_ = new_array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_HYDROGEN_UNIQUE_H_
|