OLD | NEW |
---|---|
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 } | 76 } |
77 | 77 |
78 | 78 |
79 // Return the smallest multiple of m which is >= x. | 79 // Return the smallest multiple of m which is >= x. |
80 template <typename T> | 80 template <typename T> |
81 static inline T RoundUp(T x, int m) { | 81 static inline T RoundUp(T x, int m) { |
82 return RoundDown(x + m - 1, m); | 82 return RoundDown(x + m - 1, m); |
83 } | 83 } |
84 | 84 |
85 | 85 |
86 template <typename T> | |
87 static int Spaceship(const T& a, const T& b) { | |
iposva
2008/11/26 06:56:22
Comments please. What kind of spaceship is this? T
Christian Plesner Hansen
2008/11/26 07:49:51
I'll rename it. The name "spaceship" is apparentl
| |
88 if (a == b) | |
89 return 0; | |
90 else if (a < b) | |
91 return -1; | |
92 else | |
93 return 1; | |
94 } | |
95 | |
96 | |
97 template <typename T> | |
98 static int PointerSpaceship(const T* a, const T* b) { | |
99 return Spaceship<T>(*a, *b); | |
100 } | |
101 | |
102 | |
86 // Returns the smallest power of two which is >= x. If you pass in a | 103 // Returns the smallest power of two which is >= x. If you pass in a |
87 // number that is already a power of two, it is returned as is. | 104 // number that is already a power of two, it is returned as is. |
88 uint32_t RoundUpToPowerOf2(uint32_t x); | 105 uint32_t RoundUpToPowerOf2(uint32_t x); |
89 | 106 |
90 | 107 |
91 template <typename T> | 108 template <typename T> |
92 static inline bool IsAligned(T value, T alignment) { | 109 static inline bool IsAligned(T value, T alignment) { |
93 ASSERT(IsPowerOf2(alignment)); | 110 ASSERT(IsPowerOf2(alignment)); |
94 return (value & (alignment - 1)) == 0; | 111 return (value & (alignment - 1)) == 0; |
95 } | 112 } |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 public: | 293 public: |
277 Vector() : start_(NULL), length_(0) {} | 294 Vector() : start_(NULL), length_(0) {} |
278 Vector(T* data, int length) : start_(data), length_(length) { | 295 Vector(T* data, int length) : start_(data), length_(length) { |
279 ASSERT(length == 0 || (length > 0 && data != NULL)); | 296 ASSERT(length == 0 || (length > 0 && data != NULL)); |
280 } | 297 } |
281 | 298 |
282 static Vector<T> New(int length) { | 299 static Vector<T> New(int length) { |
283 return Vector<T>(NewArray<T>(length), length); | 300 return Vector<T>(NewArray<T>(length), length); |
284 } | 301 } |
285 | 302 |
303 // Returns a vector using the same backing storage as this one, | |
304 // spanning from and including 'from', to but not including 'to'. | |
305 Vector<T> SubVector(int from, int to) { | |
306 ASSERT(from < length_); | |
307 ASSERT(to <= length_); | |
308 ASSERT(from < to); | |
309 return Vector<T>(start() + from, to - from); | |
310 } | |
311 | |
286 // Returns the length of the vector. | 312 // Returns the length of the vector. |
287 int length() const { return length_; } | 313 int length() const { return length_; } |
288 | 314 |
289 // Returns whether or not the vector is empty. | 315 // Returns whether or not the vector is empty. |
290 bool is_empty() const { return length_ == 0; } | 316 bool is_empty() const { return length_ == 0; } |
291 | 317 |
292 // Returns the pointer to the start of the data in the vector. | 318 // Returns the pointer to the start of the data in the vector. |
293 T* start() const { return start_; } | 319 T* start() const { return start_; } |
294 | 320 |
295 // Access individual vector elements - checks bounds in debug mode. | 321 // Access individual vector elements - checks bounds in debug mode. |
296 T& operator[](int index) const { | 322 T& operator[](int index) const { |
297 ASSERT(0 <= index && index < length_); | 323 ASSERT(0 <= index && index < length_); |
298 return start_[index]; | 324 return start_[index]; |
299 } | 325 } |
300 | 326 |
327 T& first() { return start_[0]; } | |
328 | |
329 T& last() { return start_[length_ - 1]; } | |
330 | |
301 // Returns a clone of this vector with a new backing store. | 331 // Returns a clone of this vector with a new backing store. |
302 Vector<T> Clone() const { | 332 Vector<T> Clone() const { |
303 T* result = NewArray<T>(length_); | 333 T* result = NewArray<T>(length_); |
304 for (int i = 0; i < length_; i++) result[i] = start_[i]; | 334 for (int i = 0; i < length_; i++) result[i] = start_[i]; |
305 return Vector<T>(result, length_); | 335 return Vector<T>(result, length_); |
306 } | 336 } |
307 | 337 |
338 void Sort(int (*cmp)(const T*, const T*)) { | |
339 typedef int (*RawComparer)(const void*, const void*); | |
340 qsort(start(), | |
341 length(), | |
342 sizeof(T), | |
343 reinterpret_cast<RawComparer>(cmp)); | |
344 } | |
345 | |
346 void Sort() { | |
347 Sort(PointerSpaceship<T>); | |
348 } | |
349 | |
308 // Releases the array underlying this vector. Once disposed the | 350 // Releases the array underlying this vector. Once disposed the |
309 // vector is empty. | 351 // vector is empty. |
310 void Dispose() { | 352 void Dispose() { |
311 if (is_empty()) return; | 353 if (is_empty()) return; |
312 DeleteArray(start_); | 354 DeleteArray(start_); |
313 start_ = NULL; | 355 start_ = NULL; |
314 length_ = 0; | 356 length_ = 0; |
315 } | 357 } |
316 | 358 |
317 inline Vector<T> operator+(int offset) { | 359 inline Vector<T> operator+(int offset) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
458 dest += kStepSize; | 500 dest += kStepSize; |
459 src += kStepSize; | 501 src += kStepSize; |
460 } | 502 } |
461 } | 503 } |
462 #endif | 504 #endif |
463 while (dest < limit) { | 505 while (dest < limit) { |
464 *dest++ = static_cast<sinkchar>(*src++); | 506 *dest++ = static_cast<sinkchar>(*src++); |
465 } | 507 } |
466 } | 508 } |
467 | 509 |
510 | |
511 static inline int Load16(const byte* pc) { | |
iposva
2008/11/26 06:56:22
I understand that this is most likely used when ac
Erik Corry
2008/11/26 12:18:36
Yes, that's what it's used for and in this context
| |
512 #ifdef CAN_READ_UNALIGNED | |
513 return *reinterpret_cast<const uint16_t*>(pc); | |
514 #else | |
515 uint32_t word; | |
516 word = pc[1]; | |
517 word |= pc[0] << 8; | |
518 return word; | |
519 #endif | |
520 } | |
521 | |
522 | |
523 static inline int Load32(const byte* pc) { | |
524 #ifdef CAN_READ_UNALIGNED | |
525 return *reinterpret_cast<const uint32_t*>(pc); | |
526 #else | |
527 uint32_t word; | |
528 word = pc[3]; | |
529 word |= pc[2] << 8; | |
530 word |= pc[1] << 16; | |
531 word |= pc[0] << 24; | |
532 return word; | |
533 #endif | |
534 } | |
535 | |
536 | |
537 static inline void Store16(byte* pc, uint16_t value) { | |
538 #ifdef CAN_READ_UNALIGNED | |
539 *reinterpret_cast<uint16_t*>(pc) = value; | |
540 #else | |
541 pc[1] = value; | |
542 pc[0] = value >> 8; | |
543 #endif | |
544 } | |
545 | |
546 | |
547 static inline void Store32(byte* pc, uint32_t value) { | |
548 #ifdef CAN_READ_UNALIGNED | |
549 *reinterpret_cast<uint32_t*>(pc) = value; | |
550 #else | |
551 pc[3] = value; | |
552 pc[2] = value >> 8; | |
553 pc[1] = value >> 16; | |
554 pc[0] = value >> 24; | |
555 #endif | |
556 } | |
557 | |
558 | |
559 | |
560 | |
561 | |
468 } } // namespace v8::internal | 562 } } // namespace v8::internal |
469 | 563 |
470 #endif // V8_UTILS_H_ | 564 #endif // V8_UTILS_H_ |
OLD | NEW |