Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_UTILS_H_ |
| 4 | #define ART_SRC_UTILS_H_ |
| 5 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 6 | #include "globals.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 7 | #include "logging.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 8 | #include "stringpiece.h" |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 9 | #include "stringprintf.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 10 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 11 | #include <pthread.h> |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 15 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 17 | class Field; |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 18 | class Method; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 19 | class Object; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 20 | class String; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 21 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 22 | template<typename T> |
| 23 | static inline bool IsPowerOfTwo(T x) { |
| 24 | return (x & (x - 1)) == 0; |
| 25 | } |
| 26 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 27 | template<typename T> |
| 28 | static inline bool IsAligned(T x, int n) { |
| 29 | CHECK(IsPowerOfTwo(n)); |
| 30 | return (x & (n - 1)) == 0; |
| 31 | } |
| 32 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 33 | template<typename T> |
| 34 | static inline bool IsAligned(T* x, int n) { |
| 35 | return IsAligned(reinterpret_cast<uintptr_t>(x), n); |
| 36 | } |
| 37 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 38 | // Check whether an N-bit two's-complement representation can hold value. |
| 39 | static inline bool IsInt(int N, word value) { |
| 40 | CHECK_LT(0, N); |
| 41 | CHECK_LT(N, kBitsPerWord); |
| 42 | word limit = static_cast<word>(1) << (N - 1); |
| 43 | return (-limit <= value) && (value < limit); |
| 44 | } |
| 45 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 46 | static inline bool IsUint(int N, word value) { |
| 47 | CHECK_LT(0, N); |
| 48 | CHECK_LT(N, kBitsPerWord); |
| 49 | word limit = static_cast<word>(1) << N; |
| 50 | return (0 <= value) && (value < limit); |
| 51 | } |
| 52 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 53 | static inline bool IsAbsoluteUint(int N, word value) { |
| 54 | CHECK_LT(0, N); |
| 55 | CHECK_LT(N, kBitsPerWord); |
| 56 | if (value < 0) value = -value; |
| 57 | return IsUint(N, value); |
| 58 | } |
| 59 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 60 | static inline int32_t Low16Bits(int32_t value) { |
| 61 | return static_cast<int32_t>(value & 0xffff); |
| 62 | } |
| 63 | |
| 64 | static inline int32_t High16Bits(int32_t value) { |
| 65 | return static_cast<int32_t>(value >> 16); |
| 66 | } |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 67 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 68 | static inline int32_t Low32Bits(int64_t value) { |
| 69 | return static_cast<int32_t>(value); |
| 70 | } |
| 71 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 72 | static inline int32_t High32Bits(int64_t value) { |
| 73 | return static_cast<int32_t>(value >> 32); |
| 74 | } |
| 75 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 76 | template<typename T> |
| 77 | static inline T RoundDown(T x, int n) { |
| 78 | CHECK(IsPowerOfTwo(n)); |
| 79 | return (x & -n); |
| 80 | } |
| 81 | |
| 82 | template<typename T> |
| 83 | static inline T RoundUp(T x, int n) { |
| 84 | return RoundDown(x + n - 1, n); |
| 85 | } |
| 86 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 87 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 88 | // figure 3-3, page 48, where the function is called clp2. |
| 89 | static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) { |
| 90 | x = x - 1; |
| 91 | x = x | (x >> 1); |
| 92 | x = x | (x >> 2); |
| 93 | x = x | (x >> 4); |
| 94 | x = x | (x >> 8); |
| 95 | x = x | (x >> 16); |
| 96 | return x + 1; |
| 97 | } |
| 98 | |
| 99 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 100 | // figure 5-2, page 66, where the function is called pop. |
| 101 | static inline int CountOneBits(uint32_t x) { |
| 102 | x = x - ((x >> 1) & 0x55555555); |
| 103 | x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
| 104 | x = (x + (x >> 4)) & 0x0F0F0F0F; |
| 105 | x = x + (x >> 8); |
| 106 | x = x + (x >> 16); |
| 107 | return static_cast<int>(x & 0x0000003F); |
| 108 | } |
| 109 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 110 | #define CLZ(x) __builtin_clz(x) |
| 111 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 112 | static inline bool NeedsEscaping(uint16_t ch) { |
| 113 | return (ch < ' ' || ch > '~'); |
| 114 | } |
| 115 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 116 | static inline std::string PrintableChar(uint16_t ch) { |
| 117 | std::string result; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 118 | result += '\''; |
| 119 | if (NeedsEscaping(ch)) { |
| 120 | StringAppendF(&result, "\\u%04x", ch); |
| 121 | } else { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 122 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 123 | } |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 124 | result += '\''; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 125 | return result; |
| 126 | } |
| 127 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 128 | // TODO: assume the content is UTF-8, and show code point escapes? |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 129 | template<typename StringT> |
| 130 | static inline std::string PrintableString(const StringT& s) { |
| 131 | std::string result; |
| 132 | result += '"'; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 133 | for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 134 | char ch = *it; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 135 | if (NeedsEscaping(ch)) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 136 | StringAppendF(&result, "\\x%02x", ch & 0xff); |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 137 | } else { |
| 138 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | result += '"'; |
| 142 | return result; |
| 143 | } |
| 144 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 145 | // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int", |
| 146 | // "[[I" would be "int[][]", "[Ljava/lang/String;" would be |
| 147 | // "java.lang.String[]", and so forth. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 148 | std::string PrettyDescriptor(const String* descriptor); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 149 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 150 | // Returns a human-readable signature for 'f'. Something like "a.b.C.f". |
| 151 | std::string PrettyField(const Field* f); |
| 152 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 153 | // Returns a human-readable signature for 'm'. Something like "a.b.C.m" or |
| 154 | // "a.b.C.m(II)V" (depending on the value of 'with_signature'). |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 155 | std::string PrettyMethod(const Method* m, bool with_signature = true); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 156 | |
| 157 | // Returns a human-readable form of the name of the *class* of the given object. |
| 158 | // So given an instance of java.lang.String, the output would |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 159 | // be "java.lang.String". Given an array of int, the output would be "int[]". |
| 160 | // Given String.class, the output would be "java.lang.Class<java.lang.String>". |
| 161 | std::string PrettyType(const Object* obj); |
| 162 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 163 | // Performs JNI name mangling as described in section 11.3 "Linking Native Methods" |
| 164 | // of the JNI spec. |
| 165 | std::string MangleForJni(const std::string& s); |
| 166 | |
| 167 | // Returns the JNI native function name for the non-overloaded method 'm'. |
| 168 | std::string JniShortName(const Method* m); |
| 169 | // Returns the JNI native function name for the overloaded method 'm'. |
| 170 | std::string JniLongName(const Method* m); |
| 171 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 172 | bool ReadFileToString(const std::string& file_name, std::string* result); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 173 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 174 | // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format. |
| 175 | std::string GetIsoDate(); |
| 176 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 177 | // Splits a string using the given delimiter character into a vector of |
| 178 | // strings. Empty strings will be omitted. |
| 179 | void Split(const std::string& s, char delim, std::vector<std::string>& result); |
| 180 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 181 | // Returns the calling thread's tid. (The C libraries don't expose this.) |
| 182 | pid_t GetTid(); |
| 183 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 184 | // Returns the tid of the thread that owns the given pthread mutex, or 0. |
| 185 | pid_t GetOwner(pthread_mutex_t* mutex); |
| 186 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 187 | // Sets the name of the current thread. The name may be truncated to an |
| 188 | // implementation-defined limit. |
| 189 | void SetThreadName(const char* name); |
| 190 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 191 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 192 | |
| 193 | #endif // ART_SRC_UTILS_H_ |