dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame^] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_HYDROGEN_UNIQUE_H_ |
| 29 | #define V8_HYDROGEN_UNIQUE_H_ |
| 30 | |
| 31 | #include "handles.h" |
| 32 | #include "utils.h" |
| 33 | #include "zone.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | |
| 39 | template <typename T> |
| 40 | class UniqueSet; |
| 41 | |
| 42 | |
| 43 | // Represents a handle to an object on the heap, but with the additional |
| 44 | // ability of checking for equality and hashing without accessing the heap. |
| 45 | // |
| 46 | // Creating a Unique<T> requires first dereferencing the handle to obtain |
| 47 | // the address of the object, which is used as the hashcode and the basis for |
| 48 | // comparison. The object can be moved later by the GC, but comparison |
| 49 | // and hashing use the old address of the object, without dereferencing it. |
| 50 | // |
| 51 | // Careful! Comparison of two Uniques is only correct if both were created |
| 52 | // in the same "era" of GC or if at least one is a non-movable object. |
| 53 | template <typename T> |
| 54 | class Unique V8_FINAL { |
| 55 | public: |
| 56 | // TODO(titzer): make private and introduce some builder/owner class. |
| 57 | explicit Unique(Handle<T> handle) { |
| 58 | if (handle.is_null()) { |
| 59 | raw_address_ = NULL; |
| 60 | } else { |
| 61 | raw_address_ = reinterpret_cast<Address>(*handle); |
| 62 | ASSERT_NE(raw_address_, NULL); |
| 63 | } |
| 64 | handle_ = handle; |
| 65 | } |
| 66 | |
| 67 | // Constructor for handling automatic up casting. |
| 68 | // Ex. Unique<JSFunction> can be passed when Unique<Object> is expected. |
| 69 | template <class S> Unique(Unique<S> uniq) { |
| 70 | #ifdef DEBUG |
| 71 | T* a = NULL; |
| 72 | S* b = NULL; |
| 73 | a = b; // Fake assignment to enforce type checks. |
| 74 | USE(a); |
| 75 | #endif |
| 76 | raw_address_ = uniq.raw_address_; |
| 77 | handle_ = uniq.handle_; // Creates a new handle sharing the same location. |
| 78 | } |
| 79 | |
| 80 | template <typename U> |
| 81 | bool operator==(const Unique<U>& other) const { |
| 82 | return raw_address_ == other.raw_address_; |
| 83 | } |
| 84 | |
| 85 | template <typename U> |
| 86 | bool operator!=(const Unique<U>& other) const { |
| 87 | return raw_address_ != other.raw_address_; |
| 88 | } |
| 89 | |
| 90 | intptr_t Hashcode() const { |
| 91 | return reinterpret_cast<intptr_t>(raw_address_); |
| 92 | } |
| 93 | |
| 94 | bool IsNull() { |
| 95 | return raw_address_ == NULL; |
| 96 | } |
| 97 | |
| 98 | // Don't do this unless you have access to the heap! |
| 99 | // No, seriously! You can compare and hash and set-ify uniques that were |
| 100 | // all created at the same time; please don't dereference. |
| 101 | Handle<T> handle() { |
| 102 | return handle_; |
| 103 | } |
| 104 | |
| 105 | friend class UniqueSet<T>; // Uses internal details for speed. |
| 106 | template <class U> |
| 107 | friend class Unique; // For comparing raw_address values. |
| 108 | |
| 109 | private: |
| 110 | Address raw_address_; |
| 111 | Handle<T> handle_; |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | template <typename T> |
| 116 | class UniqueSet V8_FINAL : public ZoneObject { |
| 117 | public: |
| 118 | // Constructor. A new set will be empty. |
| 119 | UniqueSet() : size_(0), capacity_(0), array_(NULL) { } |
| 120 | |
| 121 | // Add a new element to this unique set. Mutates this set. O(|this|). |
| 122 | void Add(Unique<T> uniq, Zone* zone) { |
| 123 | // Keep the set sorted by the {raw_address} of the unique elements. |
| 124 | for (int i = 0; i < size_; i++) { |
| 125 | if (array_[i] == uniq) return; |
| 126 | if (array_[i].raw_address_ > uniq.raw_address_) { |
| 127 | // Insert in the middle. |
| 128 | Grow(size_ + 1, zone); |
| 129 | for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j]; |
| 130 | array_[i] = uniq; |
| 131 | size_++; |
| 132 | return; |
| 133 | } |
| 134 | } |
| 135 | // Append the element to the the end. |
| 136 | Grow(size_ + 1, zone); |
| 137 | array_[size_++] = uniq; |
| 138 | } |
| 139 | |
| 140 | // Compare this set against another set. O(|this|). |
| 141 | bool Equals(UniqueSet<T>* that) { |
| 142 | if (that->size_ != this->size_) return false; |
| 143 | for (int i = 0; i < this->size_; i++) { |
| 144 | if (this->array_[i] != that->array_[i]) return false; |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | // Check if this set is a subset of the given set. O(|this| + |that|). |
| 150 | bool IsSubset(UniqueSet<T>* that) { |
| 151 | if (that->size_ < this->size_) return false; |
| 152 | int j = 0; |
| 153 | for (int i = 0; i < this->size_; i++) { |
| 154 | Unique<T> sought = this->array_[i]; |
| 155 | while (true) { |
| 156 | if (sought == that->array_[j++]) break; |
| 157 | // Fail whenever there are more elements in {this} than {that}. |
| 158 | if ((this->size_ - i) > (that->size_ - j)) return false; |
| 159 | } |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | // Returns a new set representing the intersection of this set and the other. |
| 165 | // O(|this| + |that|). |
| 166 | UniqueSet<T>* Intersect(UniqueSet<T>* that, Zone* zone) { |
| 167 | if (that->size_ == 0 || this->size_ == 0) return new(zone) UniqueSet<T>(); |
| 168 | |
| 169 | UniqueSet<T>* out = new(zone) UniqueSet<T>(); |
| 170 | out->Grow(Min(this->size_, that->size_), zone); |
| 171 | |
| 172 | int i = 0, j = 0, k = 0; |
| 173 | while (i < this->size_ && j < that->size_) { |
| 174 | Unique<T> a = this->array_[i]; |
| 175 | Unique<T> b = that->array_[j]; |
| 176 | if (a == b) { |
| 177 | out->array_[k++] = a; |
| 178 | i++; |
| 179 | j++; |
| 180 | } else if (a.raw_address_ < b.raw_address_) { |
| 181 | i++; |
| 182 | } else { |
| 183 | j++; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | out->size_ = k; |
| 188 | return out; |
| 189 | } |
| 190 | |
| 191 | // Returns a new set representing the union of this set and the other. |
| 192 | // O(|this| + |that|). |
| 193 | UniqueSet<T>* Union(UniqueSet<T>* that, Zone* zone) { |
| 194 | if (that->size_ == 0) return this->Copy(zone); |
| 195 | if (this->size_ == 0) return that->Copy(zone); |
| 196 | |
| 197 | UniqueSet<T>* out = new(zone) UniqueSet<T>(); |
| 198 | out->Grow(this->size_ + that->size_, zone); |
| 199 | |
| 200 | int i = 0, j = 0, k = 0; |
| 201 | while (i < this->size_ && j < that->size_) { |
| 202 | Unique<T> a = this->array_[i]; |
| 203 | Unique<T> b = that->array_[j]; |
| 204 | if (a == b) { |
| 205 | out->array_[k++] = a; |
| 206 | i++; |
| 207 | j++; |
| 208 | } else if (a.raw_address_ < b.raw_address_) { |
| 209 | out->array_[k++] = a; |
| 210 | i++; |
| 211 | } else { |
| 212 | out->array_[k++] = b; |
| 213 | j++; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | while (i < this->size_) out->array_[k++] = this->array_[i++]; |
| 218 | while (j < that->size_) out->array_[k++] = that->array_[j++]; |
| 219 | |
| 220 | out->size_ = k; |
| 221 | return out; |
| 222 | } |
| 223 | |
| 224 | // Makes an exact copy of this set. O(|this| + |that|). |
| 225 | UniqueSet<T>* Copy(Zone* zone) { |
| 226 | UniqueSet<T>* copy = new(zone) UniqueSet<T>(); |
| 227 | copy->size_ = this->size_; |
| 228 | copy->capacity_ = this->size_; |
| 229 | copy->array_ = zone->NewArray<Unique<T> >(this->size_); |
| 230 | memcpy(copy->array_, this->array_, this->size_ * sizeof(Unique<T>)); |
| 231 | return copy; |
| 232 | } |
| 233 | |
| 234 | inline int size() { |
| 235 | return size_; |
| 236 | } |
| 237 | |
| 238 | private: |
| 239 | // These sets should be small, since operations are implemented with simple |
| 240 | // linear algorithms. Enforce a maximum size. |
| 241 | static const int kMaxCapacity = 65535; |
| 242 | |
| 243 | uint16_t size_; |
| 244 | uint16_t capacity_; |
| 245 | Unique<T>* array_; |
| 246 | |
| 247 | // Grow the size of internal storage to be at least {size} elements. |
| 248 | void Grow(int size, Zone* zone) { |
| 249 | CHECK(size < kMaxCapacity); // Enforce maximum size. |
| 250 | if (capacity_ < size) { |
| 251 | int new_capacity = 2 * capacity_ + size; |
| 252 | if (new_capacity > kMaxCapacity) new_capacity = kMaxCapacity; |
| 253 | Unique<T>* new_array = zone->NewArray<Unique<T> >(new_capacity); |
| 254 | if (size_ > 0) { |
| 255 | memcpy(new_array, array_, size_ * sizeof(Unique<T>)); |
| 256 | } |
| 257 | capacity_ = new_capacity; |
| 258 | array_ = new_array; |
| 259 | } |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | |
| 264 | } } // namespace v8::internal |
| 265 | |
| 266 | #endif // V8_HYDROGEN_UNIQUE_H_ |