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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 // Creating a Unique<T> requires first dereferencing the handle to obtain | 47 // Creating a Unique<T> requires first dereferencing the handle to obtain |
48 // 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 |
49 // 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 |
50 // 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. |
51 // | 51 // |
52 // 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 |
53 // 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. |
54 template <typename T> | 54 template <typename T> |
55 class Unique V8_FINAL { | 55 class Unique V8_FINAL { |
56 public: | 56 public: |
57 // TODO(titzer): make private and introduce a factory. | 57 // TODO(titzer): make private and introduce a uniqueness scope. |
58 explicit Unique(Handle<T> handle) { | 58 explicit Unique(Handle<T> handle) { |
59 if (handle.is_null()) { | 59 if (handle.is_null()) { |
60 raw_address_ = NULL; | 60 raw_address_ = NULL; |
61 } else { | 61 } else { |
62 // This is a best-effort check to prevent comparing Unique<T>'s created | 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 | 63 // in different GC eras; we require heap allocation to be disallowed at |
64 // creation time. | 64 // creation time. |
65 // NOTE: we currently consider maps to be non-movable, so no special | 65 // NOTE: we currently consider maps to be non-movable, so no special |
66 // assurance is required for creating a Unique<Map>. | 66 // assurance is required for creating a Unique<Map>. |
67 // TODO(titzer): other immortable immovable objects are also fine. | 67 // TODO(titzer): other immortable immovable objects are also fine. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 inline intptr_t Hashcode() const { | 104 inline intptr_t Hashcode() const { |
105 ASSERT(IsInitialized()); | 105 ASSERT(IsInitialized()); |
106 return reinterpret_cast<intptr_t>(raw_address_); | 106 return reinterpret_cast<intptr_t>(raw_address_); |
107 } | 107 } |
108 | 108 |
109 inline bool IsNull() const { | 109 inline bool IsNull() const { |
110 ASSERT(IsInitialized()); | 110 ASSERT(IsInitialized()); |
111 return raw_address_ == NULL; | 111 return raw_address_ == NULL; |
112 } | 112 } |
113 | 113 |
114 // Extract the handle from this Unique in order to dereference it. | 114 inline bool IsKnownGlobal(void* global) const { |
115 // WARNING: Only do this if you have access to the heap. | 115 ASSERT(IsInitialized()); |
Toon Verwaest
2013/09/24 09:17:54
Any reason you removed this comment?
| |
116 return raw_address_ == reinterpret_cast<Address>(global); | |
117 } | |
118 | |
116 inline Handle<T> handle() const { | 119 inline Handle<T> handle() const { |
117 return handle_; | 120 return handle_; |
118 } | 121 } |
119 | 122 |
120 inline bool IsInitialized() const { | 123 inline bool IsInitialized() const { |
121 return raw_address_ != NULL || handle_.is_null(); | 124 return raw_address_ != NULL || handle_.is_null(); |
122 } | 125 } |
123 | 126 |
124 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. | 127 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally. |
125 static Unique<T> CreateUninitialized(Handle<T> handle) { | 128 static Unique<T> CreateUninitialized(Handle<T> handle) { |
126 return Unique<T>(static_cast<Address>(NULL), handle); | 129 return Unique<T>(reinterpret_cast<Address>(NULL), handle); |
130 } | |
131 | |
132 static Unique<T> CreateImmovable(Handle<T> handle) { | |
133 return Unique<T>(reinterpret_cast<Address>(*handle), handle); | |
127 } | 134 } |
128 | 135 |
129 friend class UniqueSet<T>; // Uses internal details for speed. | 136 friend class UniqueSet<T>; // Uses internal details for speed. |
130 template <class U> | 137 template <class U> |
131 friend class Unique; // For comparing raw_address values. | 138 friend class Unique; // For comparing raw_address values. |
132 | 139 |
133 private: | 140 private: |
134 Address raw_address_; | 141 Address raw_address_; |
135 Handle<T> handle_; | 142 Handle<T> handle_; |
136 }; | 143 }; |
(...skipping 27 matching lines...) Expand all Loading... | |
164 | 171 |
165 // Compare this set against another set. O(|this|). | 172 // Compare this set against another set. O(|this|). |
166 bool Equals(UniqueSet<T>* that) const { | 173 bool Equals(UniqueSet<T>* that) const { |
167 if (that->size_ != this->size_) return false; | 174 if (that->size_ != this->size_) return false; |
168 for (int i = 0; i < this->size_; i++) { | 175 for (int i = 0; i < this->size_; i++) { |
169 if (this->array_[i] != that->array_[i]) return false; | 176 if (this->array_[i] != that->array_[i]) return false; |
170 } | 177 } |
171 return true; | 178 return true; |
172 } | 179 } |
173 | 180 |
181 // Check whether this set contains the given element. O(|this|) | |
182 // TODO(titzer): use binary search for large sets to make this O(log|this|) | |
174 template <typename U> | 183 template <typename U> |
175 bool Contains(Unique<U> elem) const { | 184 bool Contains(Unique<U> elem) const { |
176 // TODO(titzer): use binary search for larger sets. | |
177 for (int i = 0; i < size_; i++) { | 185 for (int i = 0; i < size_; i++) { |
178 if (this->array_[i] == elem) return true; | 186 if (this->array_[i] == elem) return true; |
179 } | 187 } |
180 return false; | 188 return false; |
181 } | 189 } |
182 | 190 |
183 // Check if this set is a subset of the given set. O(|this| + |that|). | 191 // Check if this set is a subset of the given set. O(|this| + |that|). |
184 bool IsSubset(UniqueSet<T>* that) const { | 192 bool IsSubset(UniqueSet<T>* that) const { |
185 if (that->size_ < this->size_) return false; | 193 if (that->size_ < this->size_) return false; |
186 int j = 0; | 194 int j = 0; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 capacity_ = new_capacity; | 304 capacity_ = new_capacity; |
297 array_ = new_array; | 305 array_ = new_array; |
298 } | 306 } |
299 } | 307 } |
300 }; | 308 }; |
301 | 309 |
302 | 310 |
303 } } // namespace v8::internal | 311 } } // namespace v8::internal |
304 | 312 |
305 #endif // V8_HYDROGEN_UNIQUE_H_ | 313 #endif // V8_HYDROGEN_UNIQUE_H_ |
OLD | NEW |