| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #ifndef V8_HYDROGEN_UNIQUE_H_ | 28 #ifndef V8_HYDROGEN_UNIQUE_H_ |
| 29 #define V8_HYDROGEN_UNIQUE_H_ | 29 #define V8_HYDROGEN_UNIQUE_H_ |
| 30 | 30 |
| 31 #include "handles.h" | 31 #include "handles.h" |
| 32 #include "objects.h" |
| 32 #include "utils.h" | 33 #include "utils.h" |
| 33 #include "zone.h" | 34 #include "zone.h" |
| 34 | 35 |
| 35 namespace v8 { | 36 namespace v8 { |
| 36 namespace internal { | 37 namespace internal { |
| 37 | 38 |
| 38 | 39 |
| 39 template <typename T> | 40 template <typename T> |
| 40 class UniqueSet; | 41 class UniqueSet; |
| 41 | 42 |
| 42 | 43 |
| 43 // Represents a handle to an object on the heap, but with the additional | 44 // 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 // ability of checking for equality and hashing without accessing the heap. |
| 45 // | 46 // |
| 46 // Creating a Unique<T> requires first dereferencing the handle to obtain | 47 // 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 // 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 // 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 // and hashing use the old address of the object, without dereferencing it. |
| 50 // | 51 // |
| 51 // Careful! Comparison of two Uniques is only correct if both were created | 52 // 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 // in the same "era" of GC or if at least one is a non-movable object. |
| 53 template <typename T> | 54 template <typename T> |
| 54 class Unique V8_FINAL { | 55 class Unique V8_FINAL { |
| 55 public: | 56 public: |
| 56 // TODO(titzer): make private and introduce some builder/owner class. | 57 // TODO(titzer): make private and introduce a factory. |
| 57 explicit Unique(Handle<T> handle) { | 58 explicit Unique(Handle<T> handle) { |
| 58 if (handle.is_null()) { | 59 if (handle.is_null()) { |
| 59 raw_address_ = NULL; | 60 raw_address_ = NULL; |
| 60 } else { | 61 } else { |
| 62 // This is a best-effort check to prevent comparing Unique<T>'s created |
| 63 // in different GC eras; we require heap allocation to be disallowed at |
| 64 // creation time. |
| 65 // NOTE: we currently consider maps to be non-movable, so no special |
| 66 // assurance is required for creating a Unique<Map>. |
| 67 // TODO(titzer): other immortable immovable objects are also fine. |
| 68 ASSERT(!AllowHeapAllocation::IsAllowed() || handle->IsMap()); |
| 61 raw_address_ = reinterpret_cast<Address>(*handle); | 69 raw_address_ = reinterpret_cast<Address>(*handle); |
| 62 ASSERT_NE(raw_address_, NULL); | 70 ASSERT_NE(raw_address_, NULL); // Non-null should imply non-zero address. |
| 63 } | 71 } |
| 64 handle_ = handle; | 72 handle_ = handle; |
| 65 } | 73 } |
| 66 | 74 |
| 67 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. | 75 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. |
| 68 Unique(Address raw_address, Handle<T> handle) | 76 Unique(Address raw_address, Handle<T> handle) |
| 69 : raw_address_(raw_address), handle_(handle) { } | 77 : raw_address_(raw_address), handle_(handle) { } |
| 70 | 78 |
| 71 // Constructor for handling automatic up casting. | 79 // Constructor for handling automatic up casting. |
| 72 // Ex. Unique<JSFunction> can be passed when Unique<Object> is expected. | 80 // Eg. Unique<JSFunction> can be passed when Unique<Object> is expected. |
| 73 template <class S> Unique(Unique<S> uniq) { | 81 template <class S> Unique(Unique<S> uniq) { |
| 74 #ifdef DEBUG | 82 #ifdef DEBUG |
| 75 T* a = NULL; | 83 T* a = NULL; |
| 76 S* b = NULL; | 84 S* b = NULL; |
| 77 a = b; // Fake assignment to enforce type checks. | 85 a = b; // Fake assignment to enforce type checks. |
| 78 USE(a); | 86 USE(a); |
| 79 #endif | 87 #endif |
| 80 raw_address_ = uniq.raw_address_; | 88 raw_address_ = uniq.raw_address_; |
| 81 handle_ = uniq.handle_; // Creates a new handle sharing the same location. | 89 handle_ = uniq.handle_; |
| 82 } | 90 } |
| 83 | 91 |
| 84 template <typename U> | 92 template <typename U> |
| 85 bool operator==(const Unique<U>& other) const { | 93 bool operator==(const Unique<U>& other) const { |
| 94 ASSERT(IsInitialized() && other.IsInitialized()); |
| 86 return raw_address_ == other.raw_address_; | 95 return raw_address_ == other.raw_address_; |
| 87 } | 96 } |
| 88 | 97 |
| 89 template <typename U> | 98 template <typename U> |
| 90 bool operator!=(const Unique<U>& other) const { | 99 bool operator!=(const Unique<U>& other) const { |
| 100 ASSERT(IsInitialized() && other.IsInitialized()); |
| 91 return raw_address_ != other.raw_address_; | 101 return raw_address_ != other.raw_address_; |
| 92 } | 102 } |
| 93 | 103 |
| 94 intptr_t Hashcode() const { | 104 intptr_t Hashcode() const { |
| 105 ASSERT(IsInitialized()); |
| 95 return reinterpret_cast<intptr_t>(raw_address_); | 106 return reinterpret_cast<intptr_t>(raw_address_); |
| 96 } | 107 } |
| 97 | 108 |
| 98 bool IsNull() { | 109 bool IsNull() const { |
| 110 ASSERT(IsInitialized()); |
| 99 return raw_address_ == NULL; | 111 return raw_address_ == NULL; |
| 100 } | 112 } |
| 101 | 113 |
| 102 // Don't do this unless you have access to the heap! | 114 // Extract the handle from this Unique in order to dereference it. |
| 103 // No, seriously! You can compare and hash and set-ify uniques that were | 115 // WARNING: Only do this if you have access to the heap. |
| 104 // all created at the same time; please don't dereference. | 116 Handle<T> handle() const { |
| 105 Handle<T> handle() { | |
| 106 return handle_; | 117 return handle_; |
| 107 } | 118 } |
| 108 | 119 |
| 120 bool IsInitialized() const { |
| 121 return raw_address_ != NULL || handle_.is_null(); |
| 122 } |
| 123 |
| 124 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. |
| 125 static Unique<T> CreateUninitialized(Handle<T> handle) { |
| 126 return Unique<T>(static_cast<Address>(NULL), handle); |
| 127 } |
| 128 |
| 109 friend class UniqueSet<T>; // Uses internal details for speed. | 129 friend class UniqueSet<T>; // Uses internal details for speed. |
| 110 template <class U> | 130 template <class U> |
| 111 friend class Unique; // For comparing raw_address values. | 131 friend class Unique; // For comparing raw_address values. |
| 112 | 132 |
| 113 private: | 133 private: |
| 114 Address raw_address_; | 134 Address raw_address_; |
| 115 Handle<T> handle_; | 135 Handle<T> handle_; |
| 116 }; | 136 }; |
| 117 | 137 |
| 118 | 138 |
| 119 template <typename T> | 139 template <typename T> |
| 120 class UniqueSet V8_FINAL : public ZoneObject { | 140 class UniqueSet V8_FINAL : public ZoneObject { |
| 121 public: | 141 public: |
| 122 // Constructor. A new set will be empty. | 142 // Constructor. A new set will be empty. |
| 123 UniqueSet() : size_(0), capacity_(0), array_(NULL) { } | 143 UniqueSet() : size_(0), capacity_(0), array_(NULL) { } |
| 124 | 144 |
| 125 // Add a new element to this unique set. Mutates this set. O(|this|). | 145 // Add a new element to this unique set. Mutates this set. O(|this|). |
| 126 void Add(Unique<T> uniq, Zone* zone) { | 146 void Add(Unique<T> uniq, Zone* zone) { |
| 147 ASSERT(uniq.IsInitialized()); |
| 127 // Keep the set sorted by the {raw_address} of the unique elements. | 148 // Keep the set sorted by the {raw_address} of the unique elements. |
| 128 for (int i = 0; i < size_; i++) { | 149 for (int i = 0; i < size_; i++) { |
| 129 if (array_[i] == uniq) return; | 150 if (array_[i] == uniq) return; |
| 130 if (array_[i].raw_address_ > uniq.raw_address_) { | 151 if (array_[i].raw_address_ > uniq.raw_address_) { |
| 131 // Insert in the middle. | 152 // Insert in the middle. |
| 132 Grow(size_ + 1, zone); | 153 Grow(size_ + 1, zone); |
| 133 for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j]; | 154 for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j]; |
| 134 array_[i] = uniq; | 155 array_[i] = uniq; |
| 135 size_++; | 156 size_++; |
| 136 return; | 157 return; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 capacity_ = new_capacity; | 296 capacity_ = new_capacity; |
| 276 array_ = new_array; | 297 array_ = new_array; |
| 277 } | 298 } |
| 278 } | 299 } |
| 279 }; | 300 }; |
| 280 | 301 |
| 281 | 302 |
| 282 } } // namespace v8::internal | 303 } } // namespace v8::internal |
| 283 | 304 |
| 284 #endif // V8_HYDROGEN_UNIQUE_H_ | 305 #endif // V8_HYDROGEN_UNIQUE_H_ |
| OLD | NEW |