| 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 // Returns a uint32_t with the bit field value encoded. | 219 // Returns a uint32_t with the bit field value encoded. |
| 220 static uint32_t encode(T value) { | 220 static uint32_t encode(T value) { |
| 221 ASSERT(is_valid(value)); | 221 ASSERT(is_valid(value)); |
| 222 return static_cast<uint32_t>(value) << shift; | 222 return static_cast<uint32_t>(value) << shift; |
| 223 } | 223 } |
| 224 | 224 |
| 225 // Extracts the bit field from the value. | 225 // Extracts the bit field from the value. |
| 226 static T decode(uint32_t value) { | 226 static T decode(uint32_t value) { |
| 227 return static_cast<T>((value & mask()) >> shift); | 227 return static_cast<T>((value & mask()) >> shift); |
| 228 } | 228 } |
| 229 |
| 230 // Value for the field with all bits set. |
| 231 static T max() { |
| 232 return decode(mask()); |
| 233 } |
| 229 }; | 234 }; |
| 230 | 235 |
| 231 | 236 |
| 232 // ---------------------------------------------------------------------------- | 237 // ---------------------------------------------------------------------------- |
| 233 // Hash function. | 238 // Hash function. |
| 234 | 239 |
| 235 // Thomas Wang, Integer Hash Functions. | 240 // Thomas Wang, Integer Hash Functions. |
| 236 // http://www.concentric.net/~Ttwang/tech/inthash.htm | 241 // http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 237 static inline uint32_t ComputeIntegerHash(uint32_t key) { | 242 static inline uint32_t ComputeIntegerHash(uint32_t key) { |
| 238 uint32_t hash = key; | 243 uint32_t hash = key; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 | 324 |
| 320 // Returns the pointer to the start of the data in the vector. | 325 // Returns the pointer to the start of the data in the vector. |
| 321 T* start() const { return start_; } | 326 T* start() const { return start_; } |
| 322 | 327 |
| 323 // Access individual vector elements - checks bounds in debug mode. | 328 // Access individual vector elements - checks bounds in debug mode. |
| 324 T& operator[](int index) const { | 329 T& operator[](int index) const { |
| 325 ASSERT(0 <= index && index < length_); | 330 ASSERT(0 <= index && index < length_); |
| 326 return start_[index]; | 331 return start_[index]; |
| 327 } | 332 } |
| 328 | 333 |
| 329 T& at(int i) const { return operator[](i); } | 334 const T& at(int index) const { return operator[](index); } |
| 330 | 335 |
| 331 T& first() { return start_[0]; } | 336 T& first() { return start_[0]; } |
| 332 | 337 |
| 333 T& last() { return start_[length_ - 1]; } | 338 T& last() { return start_[length_ - 1]; } |
| 334 | 339 |
| 335 // Returns a clone of this vector with a new backing store. | 340 // Returns a clone of this vector with a new backing store. |
| 336 Vector<T> Clone() const { | 341 Vector<T> Clone() const { |
| 337 T* result = NewArray<T>(length_); | 342 T* result = NewArray<T>(length_); |
| 338 for (int i = 0; i < length_; i++) result[i] = start_[i]; | 343 for (int i = 0; i < length_; i++) result[i] = start_[i]; |
| 339 return Vector<T>(result, length_); | 344 return Vector<T>(result, length_); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 | 385 |
| 381 protected: | 386 protected: |
| 382 void set_start(T* start) { start_ = start; } | 387 void set_start(T* start) { start_ = start; } |
| 383 | 388 |
| 384 private: | 389 private: |
| 385 T* start_; | 390 T* start_; |
| 386 int length_; | 391 int length_; |
| 387 }; | 392 }; |
| 388 | 393 |
| 389 | 394 |
| 395 // A pointer that can only be set once and doesn't allow NULL values. |
| 396 template<typename T> |
| 397 class SetOncePointer { |
| 398 public: |
| 399 SetOncePointer() : pointer_(NULL) { } |
| 400 |
| 401 bool is_set() const { return pointer_ != NULL; } |
| 402 |
| 403 T* get() const { |
| 404 ASSERT(pointer_ != NULL); |
| 405 return pointer_; |
| 406 } |
| 407 |
| 408 void set(T* value) { |
| 409 ASSERT(pointer_ == NULL && value != NULL); |
| 410 pointer_ = value; |
| 411 } |
| 412 |
| 413 private: |
| 414 T* pointer_; |
| 415 }; |
| 416 |
| 417 |
| 390 template <typename T, int kSize> | 418 template <typename T, int kSize> |
| 391 class EmbeddedVector : public Vector<T> { | 419 class EmbeddedVector : public Vector<T> { |
| 392 public: | 420 public: |
| 393 EmbeddedVector() : Vector<T>(buffer_, kSize) { } | 421 EmbeddedVector() : Vector<T>(buffer_, kSize) { } |
| 394 | 422 |
| 423 explicit EmbeddedVector(T initial_value) : Vector<T>(buffer_, kSize) { |
| 424 for (int i = 0; i < kSize; ++i) { |
| 425 buffer_[i] = initial_value; |
| 426 } |
| 427 } |
| 428 |
| 395 // When copying, make underlying Vector to reference our buffer. | 429 // When copying, make underlying Vector to reference our buffer. |
| 396 EmbeddedVector(const EmbeddedVector& rhs) | 430 EmbeddedVector(const EmbeddedVector& rhs) |
| 397 : Vector<T>(rhs) { | 431 : Vector<T>(rhs) { |
| 398 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); | 432 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); |
| 399 set_start(buffer_); | 433 set_start(buffer_); |
| 400 } | 434 } |
| 401 | 435 |
| 402 EmbeddedVector& operator=(const EmbeddedVector& rhs) { | 436 EmbeddedVector& operator=(const EmbeddedVector& rhs) { |
| 403 if (this == &rhs) return *this; | 437 if (this == &rhs) return *this; |
| 404 Vector<T>::operator=(rhs); | 438 Vector<T>::operator=(rhs); |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 } | 754 } |
| 721 | 755 |
| 722 template <class Dest, class Source> | 756 template <class Dest, class Source> |
| 723 inline Dest BitCast(Source* source) { | 757 inline Dest BitCast(Source* source) { |
| 724 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source)); | 758 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source)); |
| 725 } | 759 } |
| 726 | 760 |
| 727 } } // namespace v8::internal | 761 } } // namespace v8::internal |
| 728 | 762 |
| 729 #endif // V8_UTILS_H_ | 763 #endif // V8_UTILS_H_ |
| OLD | NEW |