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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 S* b = NULL; | 76 S* b = NULL; |
77 a = b; // Fake assignment to enforce type checks. | 77 a = b; // Fake assignment to enforce type checks. |
78 USE(a); | 78 USE(a); |
79 #endif | 79 #endif |
80 raw_address_ = uniq.raw_address_; | 80 raw_address_ = uniq.raw_address_; |
81 handle_ = uniq.handle_; // Creates a new handle sharing the same location. | 81 handle_ = uniq.handle_; // Creates a new handle sharing the same location. |
82 } | 82 } |
83 | 83 |
84 template <typename U> | 84 template <typename U> |
85 bool operator==(const Unique<U>& other) const { | 85 bool operator==(const Unique<U>& other) const { |
| 86 ASSERT(IsInitialized() && other.IsInitialized()); |
86 return raw_address_ == other.raw_address_; | 87 return raw_address_ == other.raw_address_; |
87 } | 88 } |
88 | 89 |
89 template <typename U> | 90 template <typename U> |
90 bool operator!=(const Unique<U>& other) const { | 91 bool operator!=(const Unique<U>& other) const { |
| 92 ASSERT(IsInitialized() && other.IsInitialized()); |
91 return raw_address_ != other.raw_address_; | 93 return raw_address_ != other.raw_address_; |
92 } | 94 } |
93 | 95 |
94 intptr_t Hashcode() const { | 96 intptr_t Hashcode() const { |
| 97 ASSERT(IsInitialized()); |
95 return reinterpret_cast<intptr_t>(raw_address_); | 98 return reinterpret_cast<intptr_t>(raw_address_); |
96 } | 99 } |
97 | 100 |
98 bool IsNull() { | 101 bool IsNull() const { |
| 102 ASSERT(IsInitialized()); |
99 return raw_address_ == NULL; | 103 return raw_address_ == NULL; |
100 } | 104 } |
101 | 105 |
102 // Don't do this unless you have access to the heap! | 106 // 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 | 107 // WARNING: Only do this if you have access to the heap. |
104 // all created at the same time; please don't dereference. | 108 Handle<T> handle() const { |
105 Handle<T> handle() { | |
106 return handle_; | 109 return handle_; |
107 } | 110 } |
108 | 111 |
| 112 bool IsInitialized() const { |
| 113 return raw_address_ != NULL || handle_.is_null(); |
| 114 } |
| 115 |
| 116 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. |
| 117 static Unique<T> CreateUninitialized(Handle<T> handle) { |
| 118 return Unique<T>(static_cast<Address>(NULL), handle); |
| 119 } |
| 120 |
109 friend class UniqueSet<T>; // Uses internal details for speed. | 121 friend class UniqueSet<T>; // Uses internal details for speed. |
110 template <class U> | 122 template <class U> |
111 friend class Unique; // For comparing raw_address values. | 123 friend class Unique; // For comparing raw_address values. |
112 | 124 |
113 private: | 125 private: |
114 Address raw_address_; | 126 Address raw_address_; |
115 Handle<T> handle_; | 127 Handle<T> handle_; |
116 }; | 128 }; |
117 | 129 |
118 | 130 |
119 template <typename T> | 131 template <typename T> |
120 class UniqueSet V8_FINAL : public ZoneObject { | 132 class UniqueSet V8_FINAL : public ZoneObject { |
121 public: | 133 public: |
122 // Constructor. A new set will be empty. | 134 // Constructor. A new set will be empty. |
123 UniqueSet() : size_(0), capacity_(0), array_(NULL) { } | 135 UniqueSet() : size_(0), capacity_(0), array_(NULL) { } |
124 | 136 |
125 // Add a new element to this unique set. Mutates this set. O(|this|). | 137 // Add a new element to this unique set. Mutates this set. O(|this|). |
126 void Add(Unique<T> uniq, Zone* zone) { | 138 void Add(Unique<T> uniq, Zone* zone) { |
| 139 ASSERT(uniq.IsInitialized()); |
127 // Keep the set sorted by the {raw_address} of the unique elements. | 140 // Keep the set sorted by the {raw_address} of the unique elements. |
128 for (int i = 0; i < size_; i++) { | 141 for (int i = 0; i < size_; i++) { |
129 if (array_[i] == uniq) return; | 142 if (array_[i] == uniq) return; |
130 if (array_[i].raw_address_ > uniq.raw_address_) { | 143 if (array_[i].raw_address_ > uniq.raw_address_) { |
131 // Insert in the middle. | 144 // Insert in the middle. |
132 Grow(size_ + 1, zone); | 145 Grow(size_ + 1, zone); |
133 for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j]; | 146 for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j]; |
134 array_[i] = uniq; | 147 array_[i] = uniq; |
135 size_++; | 148 size_++; |
136 return; | 149 return; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 capacity_ = new_capacity; | 288 capacity_ = new_capacity; |
276 array_ = new_array; | 289 array_ = new_array; |
277 } | 290 } |
278 } | 291 } |
279 }; | 292 }; |
280 | 293 |
281 | 294 |
282 } } // namespace v8::internal | 295 } } // namespace v8::internal |
283 | 296 |
284 #endif // V8_HYDROGEN_UNIQUE_H_ | 297 #endif // V8_HYDROGEN_UNIQUE_H_ |
OLD | NEW |