| 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 11 matching lines...) Expand all Loading... |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #ifndef V8_UTILS_H_ | 28 #ifndef V8_UTILS_H_ |
| 29 #define V8_UTILS_H_ | 29 #define V8_UTILS_H_ |
| 30 | 30 |
| 31 #include <stdlib.h> | 31 #include <stdlib.h> |
| 32 #include <string.h> |
| 32 | 33 |
| 33 namespace v8 { | 34 namespace v8 { |
| 34 namespace internal { | 35 namespace internal { |
| 35 | 36 |
| 36 // ---------------------------------------------------------------------------- | 37 // ---------------------------------------------------------------------------- |
| 37 // General helper functions | 38 // General helper functions |
| 38 | 39 |
| 39 // Returns true iff x is a power of 2 (or zero). Cannot be used with the | 40 // Returns true iff x is a power of 2 (or zero). Cannot be used with the |
| 40 // maximally negative value of the type T (the -1 overflows). | 41 // maximally negative value of the type T (the -1 overflows). |
| 41 template <typename T> | 42 template <typename T> |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 EmbeddedVector(const EmbeddedVector& rhs) | 390 EmbeddedVector(const EmbeddedVector& rhs) |
| 390 : Vector<T>(rhs) { | 391 : Vector<T>(rhs) { |
| 391 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); | 392 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); |
| 392 set_start(buffer_); | 393 set_start(buffer_); |
| 393 } | 394 } |
| 394 | 395 |
| 395 EmbeddedVector& operator=(const EmbeddedVector& rhs) { | 396 EmbeddedVector& operator=(const EmbeddedVector& rhs) { |
| 396 if (this == &rhs) return *this; | 397 if (this == &rhs) return *this; |
| 397 Vector<T>::operator=(rhs); | 398 Vector<T>::operator=(rhs); |
| 398 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); | 399 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); |
| 399 set_start(buffer_); | 400 this->set_start(buffer_); |
| 400 return *this; | 401 return *this; |
| 401 } | 402 } |
| 402 | 403 |
| 403 private: | 404 private: |
| 404 T buffer_[kSize]; | 405 T buffer_[kSize]; |
| 405 }; | 406 }; |
| 406 | 407 |
| 407 | 408 |
| 408 template <typename T> | 409 template <typename T> |
| 409 class ScopedVector : public Vector<T> { | 410 class ScopedVector : public Vector<T> { |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 } | 593 } |
| 593 #endif | 594 #endif |
| 594 | 595 |
| 595 #undef STOS | 596 #undef STOS |
| 596 } | 597 } |
| 597 | 598 |
| 598 | 599 |
| 599 // Calculate 10^exponent. | 600 // Calculate 10^exponent. |
| 600 int TenToThe(int exponent); | 601 int TenToThe(int exponent); |
| 601 | 602 |
| 603 |
| 604 // The type-based aliasing rule allows the compiler to assume that pointers of |
| 605 // different types (for some definition of different) never alias each other. |
| 606 // Thus the following code does not work: |
| 607 // |
| 608 // float f = foo(); |
| 609 // int fbits = *(int*)(&f); |
| 610 // |
| 611 // The compiler 'knows' that the int pointer can't refer to f since the types |
| 612 // don't match, so the compiler may cache f in a register, leaving random data |
| 613 // in fbits. Using C++ style casts makes no difference, however a pointer to |
| 614 // char data is assumed to alias any other pointer. This is the 'memcpy |
| 615 // exception'. |
| 616 // |
| 617 // Bit_cast uses the memcpy exception to move the bits from a variable of one |
| 618 // type of a variable of another type. Of course the end result is likely to |
| 619 // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005) |
| 620 // will completely optimize BitCast away. |
| 621 // |
| 622 // There is an additional use for BitCast. |
| 623 // Recent gccs will warn when they see casts that may result in breakage due to |
| 624 // the type-based aliasing rule. If you have checked that there is no breakage |
| 625 // you can use BitCast to cast one pointer type to another. This confuses gcc |
| 626 // enough that it can no longer see that you have cast one pointer type to |
| 627 // another thus avoiding the warning. |
| 628 template <class Dest, class Source> |
| 629 inline Dest BitCast(const Source& source) { |
| 630 // Compile time assertion: sizeof(Dest) == sizeof(Source) |
| 631 // A compile error here means your Dest and Source have different sizes. |
| 632 typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1]; |
| 633 |
| 634 Dest dest; |
| 635 memcpy(&dest, &source, sizeof(dest)); |
| 636 return dest; |
| 637 } |
| 638 |
| 639 |
| 602 } } // namespace v8::internal | 640 } } // namespace v8::internal |
| 603 | 641 |
| 604 #endif // V8_UTILS_H_ | 642 #endif // V8_UTILS_H_ |
| OLD | NEW |