Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1014)

Side by Side Diff: src/unique.h

Issue 153913002: A64: Synchronize with r16756. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/a64
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/stub-cache.cc ('k') | src/version.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 // TODO(titzer): this is a hack to migrate to Unique<T> incrementally.
68 Unique(Address raw_address, Handle<T> handle)
69 : raw_address_(raw_address), handle_(handle) { }
70
71 // Constructor for handling automatic up casting.
72 // Ex. Unique<JSFunction> can be passed when Unique<Object> is expected.
73 template <class S> Unique(Unique<S> uniq) {
74 #ifdef DEBUG
75 T* a = NULL;
76 S* b = NULL;
77 a = b; // Fake assignment to enforce type checks.
78 USE(a);
79 #endif
80 raw_address_ = uniq.raw_address_;
81 handle_ = uniq.handle_; // Creates a new handle sharing the same location.
82 }
83
84 template <typename U>
85 bool operator==(const Unique<U>& other) const {
86 return raw_address_ == other.raw_address_;
87 }
88
89 template <typename U>
90 bool operator!=(const Unique<U>& other) const {
91 return raw_address_ != other.raw_address_;
92 }
93
94 intptr_t Hashcode() const {
95 return reinterpret_cast<intptr_t>(raw_address_);
96 }
97
98 bool IsNull() {
99 return raw_address_ == NULL;
100 }
101
102 // Don't do this unless you have access to the heap!
103 // No, seriously! You can compare and hash and set-ify uniques that were
104 // all created at the same time; please don't dereference.
105 Handle<T> handle() {
106 return handle_;
107 }
108
109 friend class UniqueSet<T>; // Uses internal details for speed.
110 template <class U>
111 friend class Unique; // For comparing raw_address values.
112
113 private:
114 Address raw_address_;
115 Handle<T> handle_;
116 };
117
118
119 template <typename T>
120 class UniqueSet V8_FINAL : public ZoneObject {
121 public:
122 // Constructor. A new set will be empty.
123 UniqueSet() : size_(0), capacity_(0), array_(NULL) { }
124
125 // Add a new element to this unique set. Mutates this set. O(|this|).
126 void Add(Unique<T> uniq, Zone* zone) {
127 // Keep the set sorted by the {raw_address} of the unique elements.
128 for (int i = 0; i < size_; i++) {
129 if (array_[i] == uniq) return;
130 if (array_[i].raw_address_ > uniq.raw_address_) {
131 // Insert in the middle.
132 Grow(size_ + 1, zone);
133 for (int j = size_ - 1; j >= i; j--) array_[j + 1] = array_[j];
134 array_[i] = uniq;
135 size_++;
136 return;
137 }
138 }
139 // Append the element to the the end.
140 Grow(size_ + 1, zone);
141 array_[size_++] = uniq;
142 }
143
144 // Compare this set against another set. O(|this|).
145 bool Equals(UniqueSet<T>* that) const {
146 if (that->size_ != this->size_) return false;
147 for (int i = 0; i < this->size_; i++) {
148 if (this->array_[i] != that->array_[i]) return false;
149 }
150 return true;
151 }
152
153 template <typename U>
154 bool Contains(Unique<U> elem) const {
155 // TODO(titzer): use binary search for larger sets.
156 for (int i = 0; i < size_; i++) {
157 if (this->array_[i] == elem) return true;
158 }
159 return false;
160 }
161
162 // Check if this set is a subset of the given set. O(|this| + |that|).
163 bool IsSubset(UniqueSet<T>* that) const {
164 if (that->size_ < this->size_) return false;
165 int j = 0;
166 for (int i = 0; i < this->size_; i++) {
167 Unique<T> sought = this->array_[i];
168 while (true) {
169 if (sought == that->array_[j++]) break;
170 // Fail whenever there are more elements in {this} than {that}.
171 if ((this->size_ - i) > (that->size_ - j)) return false;
172 }
173 }
174 return true;
175 }
176
177 // Returns a new set representing the intersection of this set and the other.
178 // O(|this| + |that|).
179 UniqueSet<T>* Intersect(UniqueSet<T>* that, Zone* zone) const {
180 if (that->size_ == 0 || this->size_ == 0) return new(zone) UniqueSet<T>();
181
182 UniqueSet<T>* out = new(zone) UniqueSet<T>();
183 out->Grow(Min(this->size_, that->size_), zone);
184
185 int i = 0, j = 0, k = 0;
186 while (i < this->size_ && j < that->size_) {
187 Unique<T> a = this->array_[i];
188 Unique<T> b = that->array_[j];
189 if (a == b) {
190 out->array_[k++] = a;
191 i++;
192 j++;
193 } else if (a.raw_address_ < b.raw_address_) {
194 i++;
195 } else {
196 j++;
197 }
198 }
199
200 out->size_ = k;
201 return out;
202 }
203
204 // Returns a new set representing the union of this set and the other.
205 // O(|this| + |that|).
206 UniqueSet<T>* Union(UniqueSet<T>* that, Zone* zone) const {
207 if (that->size_ == 0) return this->Copy(zone);
208 if (this->size_ == 0) return that->Copy(zone);
209
210 UniqueSet<T>* out = new(zone) UniqueSet<T>();
211 out->Grow(this->size_ + that->size_, zone);
212
213 int i = 0, j = 0, k = 0;
214 while (i < this->size_ && j < that->size_) {
215 Unique<T> a = this->array_[i];
216 Unique<T> b = that->array_[j];
217 if (a == b) {
218 out->array_[k++] = a;
219 i++;
220 j++;
221 } else if (a.raw_address_ < b.raw_address_) {
222 out->array_[k++] = a;
223 i++;
224 } else {
225 out->array_[k++] = b;
226 j++;
227 }
228 }
229
230 while (i < this->size_) out->array_[k++] = this->array_[i++];
231 while (j < that->size_) out->array_[k++] = that->array_[j++];
232
233 out->size_ = k;
234 return out;
235 }
236
237 // Makes an exact copy of this set. O(|this| + |that|).
238 UniqueSet<T>* Copy(Zone* zone) const {
239 UniqueSet<T>* copy = new(zone) UniqueSet<T>();
240 copy->size_ = this->size_;
241 copy->capacity_ = this->size_;
242 copy->array_ = zone->NewArray<Unique<T> >(this->size_);
243 memcpy(copy->array_, this->array_, this->size_ * sizeof(Unique<T>));
244 return copy;
245 }
246
247 inline int size() const {
248 return size_;
249 }
250
251 inline Unique<T> at(int index) const {
252 ASSERT(index >= 0 && index < size_);
253 return array_[index];
254 }
255
256 private:
257 // These sets should be small, since operations are implemented with simple
258 // linear algorithms. Enforce a maximum size.
259 static const int kMaxCapacity = 65535;
260
261 uint16_t size_;
262 uint16_t capacity_;
263 Unique<T>* array_;
264
265 // Grow the size of internal storage to be at least {size} elements.
266 void Grow(int size, Zone* zone) {
267 CHECK(size < kMaxCapacity); // Enforce maximum size.
268 if (capacity_ < size) {
269 int new_capacity = 2 * capacity_ + size;
270 if (new_capacity > kMaxCapacity) new_capacity = kMaxCapacity;
271 Unique<T>* new_array = zone->NewArray<Unique<T> >(new_capacity);
272 if (size_ > 0) {
273 memcpy(new_array, array_, size_ * sizeof(Unique<T>));
274 }
275 capacity_ = new_capacity;
276 array_ = new_array;
277 }
278 }
279 };
280
281
282 } } // namespace v8::internal
283
284 #endif // V8_HYDROGEN_UNIQUE_H_
OLDNEW
« no previous file with comments | « src/stub-cache.cc ('k') | src/version.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698