unique.h 10.1 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_HYDROGEN_UNIQUE_H_
#define V8_HYDROGEN_UNIQUE_H_

8 9 10 11
#include "src/handles.h"
#include "src/objects.h"
#include "src/utils.h"
#include "src/zone.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

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>
class Unique V8_FINAL {
 public:
34
  // TODO(titzer): make private and introduce a uniqueness scope.
35 36 37 38
  explicit Unique(Handle<T> handle) {
    if (handle.is_null()) {
      raw_address_ = NULL;
    } else {
39 40 41 42 43 44 45
      // 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.
      ASSERT(!AllowHeapAllocation::IsAllowed() || handle->IsMap());
46
      raw_address_ = reinterpret_cast<Address>(*handle);
47
      ASSERT_NE(raw_address_, NULL);  // Non-null should imply non-zero address.
48 49 50 51
    }
    handle_ = handle;
  }

52 53 54 55
  // 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) { }

56
  // Constructor for handling automatic up casting.
57
  // Eg. Unique<JSFunction> can be passed when Unique<Object> is expected.
58 59 60 61 62 63 64 65
  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_;
66
    handle_ = uniq.handle_;
67 68 69
  }

  template <typename U>
70
  inline bool operator==(const Unique<U>& other) const {
71
    ASSERT(IsInitialized() && other.IsInitialized());
72 73 74 75
    return raw_address_ == other.raw_address_;
  }

  template <typename U>
76
  inline bool operator!=(const Unique<U>& other) const {
77
    ASSERT(IsInitialized() && other.IsInitialized());
78 79 80
    return raw_address_ != other.raw_address_;
  }

81
  inline intptr_t Hashcode() const {
82
    ASSERT(IsInitialized());
83 84 85
    return reinterpret_cast<intptr_t>(raw_address_);
  }

86
  inline bool IsNull() const {
87
    ASSERT(IsInitialized());
88 89 90
    return raw_address_ == NULL;
  }

91 92 93 94 95
  inline bool IsKnownGlobal(void* global) const {
    ASSERT(IsInitialized());
    return raw_address_ == reinterpret_cast<Address>(global);
  }

96
  inline Handle<T> handle() const {
97 98 99
    return handle_;
  }

100 101 102 103
  template <class S> static Unique<T> cast(Unique<S> that) {
    return Unique<T>(that.raw_address_, Handle<T>::cast(that.handle_));
  }

104
  inline bool IsInitialized() const {
105 106 107 108 109
    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) {
110 111 112 113 114
    return Unique<T>(reinterpret_cast<Address>(NULL), handle);
  }

  static Unique<T> CreateImmovable(Handle<T> handle) {
    return Unique<T>(reinterpret_cast<Address>(*handle), handle);
115 116
  }

117 118 119 120 121
  friend class UniqueSet<T>;  // Uses internal details for speed.
  template <class U>
  friend class Unique;  // For comparing raw_address values.

 private:
122 123
  Unique<T>() : raw_address_(NULL) { }

124 125
  Address raw_address_;
  Handle<T> handle_;
126 127

  friend class SideEffectsTracker;
128 129 130 131 132 133 134 135 136
};


template <typename T>
class UniqueSet V8_FINAL : public ZoneObject {
 public:
  // Constructor. A new set will be empty.
  UniqueSet() : size_(0), capacity_(0), array_(NULL) { }

137 138 139 140 141 142 143 144 145 146 147 148 149
  // Capacity constructor. A new set will be empty.
  UniqueSet(int capacity, Zone* zone)
      : size_(0), capacity_(capacity),
        array_(zone->NewArray<Unique<T> >(capacity)) {
    ASSERT(capacity <= kMaxCapacity);
  }

  // Singleton constructor.
  UniqueSet(Unique<T> uniq, Zone* zone)
      : size_(1), capacity_(1), array_(zone->NewArray<Unique<T> >(1)) {
    array_[0] = uniq;
  }

150 151
  // Add a new element to this unique set. Mutates this set. O(|this|).
  void Add(Unique<T> uniq, Zone* zone) {
152
    ASSERT(uniq.IsInitialized());
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    // 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;
  }

170 171 172 173 174 175 176 177 178 179 180
  // 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;
      }
    }
  }

181
  // Compare this set against another set. O(|this|).
182
  bool Equals(const UniqueSet<T>* that) const {
183 184 185 186 187 188 189
    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;
  }

190 191
  // Check whether this set contains the given element. O(|this|)
  // TODO(titzer): use binary search for large sets to make this O(log|this|)
192
  template <typename U>
193
  bool Contains(const Unique<U> elem) const {
194 195 196 197 198
    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_;
      }
199 200 201 202
    }
    return false;
  }

203
  // Check if this set is a subset of the given set. O(|this| + |that|).
204
  bool IsSubset(const UniqueSet<T>* that) const {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    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|).
220
  UniqueSet<T>* Intersect(const UniqueSet<T>* that, Zone* zone) const {
221 222
    if (that->size_ == 0 || this->size_ == 0) return new(zone) UniqueSet<T>();

223 224
    UniqueSet<T>* out = new(zone) UniqueSet<T>(
        Min(this->size_, that->size_), zone);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

    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|).
247
  UniqueSet<T>* Union(const UniqueSet<T>* that, Zone* zone) const {
248 249 250
    if (that->size_ == 0) return this->Copy(zone);
    if (this->size_ == 0) return that->Copy(zone);

251 252
    UniqueSet<T>* out = new(zone) UniqueSet<T>(
        this->size_ + that->size_, zone);
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

    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;
  }

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  // 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;
  }

298
  // Makes an exact copy of this set. O(|this|).
299
  UniqueSet<T>* Copy(Zone* zone) const {
300
    UniqueSet<T>* copy = new(zone) UniqueSet<T>(this->size_, zone);
301 302 303 304 305
    copy->size_ = this->size_;
    memcpy(copy->array_, this->array_, this->size_ * sizeof(Unique<T>));
    return copy;
  }

306 307 308 309
  void Clear() {
    size_ = 0;
  }

310
  inline int size() const {
311 312 313
    return size_;
  }

314 315 316 317 318
  inline Unique<T> at(int index) const {
    ASSERT(index >= 0 && index < size_);
    return array_[index];
  }

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
 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_