| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 // Returns true iff x is a power of 2 (or zero). Cannot be used with the | 46 // Returns true iff x is a power of 2 (or zero). Cannot be used with the |
| 47 // maximally negative value of the type T (the -1 overflows). | 47 // maximally negative value of the type T (the -1 overflows). |
| 48 template <typename T> | 48 template <typename T> |
| 49 static inline bool IsPowerOf2(T x) { | 49 static inline bool IsPowerOf2(T x) { |
| 50 return IS_POWER_OF_TWO(x); | 50 return IS_POWER_OF_TWO(x); |
| 51 } | 51 } |
| 52 | 52 |
| 53 | 53 |
| 54 // X must be a power of 2. Returns the number of trailing zeros. | 54 // X must be a power of 2. Returns the number of trailing zeros. |
| 55 template <typename T> | 55 static inline int WhichPowerOf2(uint32_t x) { |
| 56 static inline int WhichPowerOf2(T x) { | |
| 57 ASSERT(IsPowerOf2(x)); | 56 ASSERT(IsPowerOf2(x)); |
| 58 ASSERT(x != 0); | 57 ASSERT(x != 0); |
| 59 if (x < 0) return 31; | |
| 60 int bits = 0; | 58 int bits = 0; |
| 61 #ifdef DEBUG | 59 #ifdef DEBUG |
| 62 int original_x = x; | 60 int original_x = x; |
| 63 #endif | 61 #endif |
| 64 if (x >= 0x10000) { | 62 if (x >= 0x10000) { |
| 65 bits += 16; | 63 bits += 16; |
| 66 x >>= 16; | 64 x >>= 16; |
| 67 } | 65 } |
| 68 if (x >= 0x100) { | 66 if (x >= 0x100) { |
| 69 bits += 8; | 67 bits += 8; |
| (...skipping 724 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 }; | 792 }; |
| 795 | 793 |
| 796 template <class Dest, class Source> | 794 template <class Dest, class Source> |
| 797 INLINE(Dest BitCast(const Source& source)); | 795 INLINE(Dest BitCast(const Source& source)); |
| 798 | 796 |
| 799 template <class Dest, class Source> | 797 template <class Dest, class Source> |
| 800 inline Dest BitCast(const Source& source) { | 798 inline Dest BitCast(const Source& source) { |
| 801 return BitCastHelper<Dest, Source>::cast(source); | 799 return BitCastHelper<Dest, Source>::cast(source); |
| 802 } | 800 } |
| 803 | 801 |
| 802 |
| 803 template<typename ElementType, int NumElements> |
| 804 class EmbeddedContainer { |
| 805 public: |
| 806 EmbeddedContainer() : elems_() { } |
| 807 |
| 808 int length() { return NumElements; } |
| 809 ElementType& operator[](int i) { |
| 810 ASSERT(i < length()); |
| 811 return elems_[i]; |
| 812 } |
| 813 |
| 814 private: |
| 815 ElementType elems_[NumElements]; |
| 816 }; |
| 817 |
| 818 |
| 819 template<typename ElementType> |
| 820 class EmbeddedContainer<ElementType, 0> { |
| 821 public: |
| 822 int length() { return 0; } |
| 823 ElementType& operator[](int i) { |
| 824 UNREACHABLE(); |
| 825 static ElementType t = 0; |
| 826 return t; |
| 827 } |
| 828 }; |
| 829 |
| 830 |
| 831 // Helper class for building result strings in a character buffer. The |
| 832 // purpose of the class is to use safe operations that checks the |
| 833 // buffer bounds on all operations in debug mode. |
| 834 // This simple base class does not allow formatted output. |
| 835 class SimpleStringBuilder { |
| 836 public: |
| 837 // Create a string builder with a buffer of the given size. The |
| 838 // buffer is allocated through NewArray<char> and must be |
| 839 // deallocated by the caller of Finalize(). |
| 840 explicit SimpleStringBuilder(int size); |
| 841 |
| 842 SimpleStringBuilder(char* buffer, int size) |
| 843 : buffer_(buffer, size), position_(0) { } |
| 844 |
| 845 ~SimpleStringBuilder() { if (!is_finalized()) Finalize(); } |
| 846 |
| 847 int size() const { return buffer_.length(); } |
| 848 |
| 849 // Get the current position in the builder. |
| 850 int position() const { |
| 851 ASSERT(!is_finalized()); |
| 852 return position_; |
| 853 } |
| 854 |
| 855 // Reset the position. |
| 856 void Reset() { position_ = 0; } |
| 857 |
| 858 // Add a single character to the builder. It is not allowed to add |
| 859 // 0-characters; use the Finalize() method to terminate the string |
| 860 // instead. |
| 861 void AddCharacter(char c) { |
| 862 ASSERT(c != '\0'); |
| 863 ASSERT(!is_finalized() && position_ < buffer_.length()); |
| 864 buffer_[position_++] = c; |
| 865 } |
| 866 |
| 867 // Add an entire string to the builder. Uses strlen() internally to |
| 868 // compute the length of the input string. |
| 869 void AddString(const char* s); |
| 870 |
| 871 // Add the first 'n' characters of the given string 's' to the |
| 872 // builder. The input string must have enough characters. |
| 873 void AddSubstring(const char* s, int n); |
| 874 |
| 875 // Add character padding to the builder. If count is non-positive, |
| 876 // nothing is added to the builder. |
| 877 void AddPadding(char c, int count); |
| 878 |
| 879 // Add the decimal representation of the value. |
| 880 void AddDecimalInteger(int value); |
| 881 |
| 882 // Finalize the string by 0-terminating it and returning the buffer. |
| 883 char* Finalize(); |
| 884 |
| 885 protected: |
| 886 Vector<char> buffer_; |
| 887 int position_; |
| 888 |
| 889 bool is_finalized() const { return position_ < 0; } |
| 890 private: |
| 891 DISALLOW_IMPLICIT_CONSTRUCTORS(SimpleStringBuilder); |
| 892 }; |
| 893 |
| 804 } } // namespace v8::internal | 894 } } // namespace v8::internal |
| 805 | 895 |
| 806 #endif // V8_UTILS_H_ | 896 #endif // V8_UTILS_H_ |
| OLD | NEW |