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" |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 8 | #include "primitive.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 9 | #include "stringpiece.h" |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 10 | #include "stringprintf.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 11 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 12 | #include <pthread.h> |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 16 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 17 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 18 | class Class; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 19 | class Field; |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 20 | class Method; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 21 | class Object; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 22 | class String; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 23 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 24 | template<typename T> |
| 25 | static inline bool IsPowerOfTwo(T x) { |
| 26 | return (x & (x - 1)) == 0; |
| 27 | } |
| 28 | |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 29 | template<int n, typename T> |
| 30 | static inline bool IsAligned(T x) { |
| 31 | COMPILE_ASSERT((n & (n - 1)) == 0, n_not_power_of_two); |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 32 | return (x & (n - 1)) == 0; |
| 33 | } |
| 34 | |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 35 | template<int n, typename T> |
| 36 | static inline bool IsAligned(T* x) { |
| 37 | return IsAligned<n>(reinterpret_cast<uintptr_t>(x)); |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 40 | #define CHECK_ALIGNED(value, alignment) \ |
| 41 | CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<void*>(value) |
| 42 | |
| 43 | #define DCHECK_ALIGNED(value, alignment) \ |
| 44 | DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<void*>(value) |
| 45 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 46 | // Check whether an N-bit two's-complement representation can hold value. |
| 47 | static inline bool IsInt(int N, word value) { |
| 48 | CHECK_LT(0, N); |
| 49 | CHECK_LT(N, kBitsPerWord); |
| 50 | word limit = static_cast<word>(1) << (N - 1); |
| 51 | return (-limit <= value) && (value < limit); |
| 52 | } |
| 53 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 54 | static inline bool IsUint(int N, word value) { |
| 55 | CHECK_LT(0, N); |
| 56 | CHECK_LT(N, kBitsPerWord); |
| 57 | word limit = static_cast<word>(1) << N; |
| 58 | return (0 <= value) && (value < limit); |
| 59 | } |
| 60 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 61 | static inline bool IsAbsoluteUint(int N, word value) { |
| 62 | CHECK_LT(0, N); |
| 63 | CHECK_LT(N, kBitsPerWord); |
| 64 | if (value < 0) value = -value; |
| 65 | return IsUint(N, value); |
| 66 | } |
| 67 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 68 | static inline int32_t Low16Bits(int32_t value) { |
| 69 | return static_cast<int32_t>(value & 0xffff); |
| 70 | } |
| 71 | |
| 72 | static inline int32_t High16Bits(int32_t value) { |
| 73 | return static_cast<int32_t>(value >> 16); |
| 74 | } |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 75 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 76 | static inline int32_t Low32Bits(int64_t value) { |
| 77 | return static_cast<int32_t>(value); |
| 78 | } |
| 79 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 80 | static inline int32_t High32Bits(int64_t value) { |
| 81 | return static_cast<int32_t>(value >> 32); |
| 82 | } |
| 83 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 84 | template<typename T> |
| 85 | static inline T RoundDown(T x, int n) { |
| 86 | CHECK(IsPowerOfTwo(n)); |
| 87 | return (x & -n); |
| 88 | } |
| 89 | |
| 90 | template<typename T> |
| 91 | static inline T RoundUp(T x, int n) { |
| 92 | return RoundDown(x + n - 1, n); |
| 93 | } |
| 94 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 95 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 96 | // figure 3-3, page 48, where the function is called clp2. |
| 97 | static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) { |
| 98 | x = x - 1; |
| 99 | x = x | (x >> 1); |
| 100 | x = x | (x >> 2); |
| 101 | x = x | (x >> 4); |
| 102 | x = x | (x >> 8); |
| 103 | x = x | (x >> 16); |
| 104 | return x + 1; |
| 105 | } |
| 106 | |
| 107 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 108 | // figure 5-2, page 66, where the function is called pop. |
| 109 | static inline int CountOneBits(uint32_t x) { |
| 110 | x = x - ((x >> 1) & 0x55555555); |
| 111 | x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
| 112 | x = (x + (x >> 4)) & 0x0F0F0F0F; |
| 113 | x = x + (x >> 8); |
| 114 | x = x + (x >> 16); |
| 115 | return static_cast<int>(x & 0x0000003F); |
| 116 | } |
| 117 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 118 | #define CLZ(x) __builtin_clz(x) |
| 119 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 120 | static inline bool NeedsEscaping(uint16_t ch) { |
| 121 | return (ch < ' ' || ch > '~'); |
| 122 | } |
| 123 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 124 | static inline std::string PrintableChar(uint16_t ch) { |
| 125 | std::string result; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 126 | result += '\''; |
| 127 | if (NeedsEscaping(ch)) { |
| 128 | StringAppendF(&result, "\\u%04x", ch); |
| 129 | } else { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 130 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 131 | } |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 132 | result += '\''; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 133 | return result; |
| 134 | } |
| 135 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 136 | // TODO: assume the content is UTF-8, and show code point escapes? |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 137 | template<typename StringT> |
| 138 | static inline std::string PrintableString(const StringT& s) { |
| 139 | std::string result; |
| 140 | result += '"'; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 141 | for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 142 | char ch = *it; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 143 | if (NeedsEscaping(ch)) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 144 | StringAppendF(&result, "\\x%02x", ch & 0xff); |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 145 | } else { |
| 146 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | result += '"'; |
| 150 | return result; |
| 151 | } |
| 152 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 153 | // Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf, |
| 154 | // one of which is probably more useful to you. |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 155 | // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int", |
| 156 | // "[[I" would be "int[][]", "[Ljava/lang/String;" would be |
| 157 | // "java.lang.String[]", and so forth. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 158 | std::string PrettyDescriptor(const String* descriptor); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 159 | std::string PrettyDescriptor(const std::string& descriptor); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 160 | std::string PrettyDescriptor(Primitive::Type type); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 161 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 162 | // Returns a human-readable signature for 'f'. Something like "a.b.C.f" or |
| 163 | // "int a.b.C.f" (depending on the value of 'with_type'). |
| 164 | std::string PrettyField(const Field* f, bool with_type = true); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 165 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 166 | // Returns a human-readable signature for 'm'. Something like "a.b.C.m" or |
| 167 | // "a.b.C.m(II)V" (depending on the value of 'with_signature'). |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 168 | std::string PrettyMethod(const Method* m, bool with_signature = true); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 169 | |
| 170 | // Returns a human-readable form of the name of the *class* of the given object. |
| 171 | // So given an instance of java.lang.String, the output would |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 172 | // be "java.lang.String". Given an array of int, the output would be "int[]". |
| 173 | // Given String.class, the output would be "java.lang.Class<java.lang.String>". |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 174 | std::string PrettyTypeOf(const Object* obj); |
| 175 | |
| 176 | // Returns a human-readable form of the name of the given class. |
| 177 | // Given String.class, the output would be "java.lang.Class<java.lang.String>". |
| 178 | std::string PrettyClass(const Class* c); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 179 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 180 | // Returns a human-readable form of the name of the given class with its class loader. |
| 181 | std::string PrettyClassAndClassLoader(const Class* c); |
| 182 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 183 | // Performs JNI name mangling as described in section 11.3 "Linking Native Methods" |
| 184 | // of the JNI spec. |
| 185 | std::string MangleForJni(const std::string& s); |
| 186 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 187 | // Turn "java.lang.String" into "Ljava/lang/String;". |
| 188 | std::string DotToDescriptor(const char* class_name); |
| 189 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 190 | // Turn "Ljava/lang/String;" into "java.lang.String". |
| 191 | std::string DescriptorToDot(const std::string& descriptor); |
| 192 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 193 | // Tests for whether 's' is a valid class name in the three common forms: |
| 194 | bool IsValidBinaryClassName(const char* s); // "java.lang.String" |
| 195 | bool IsValidJniClassName(const char* s); // "java/lang/String" |
| 196 | bool IsValidDescriptor(const char* s); // "Ljava/lang/String;" |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 197 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 198 | // Returns the JNI native function name for the non-overloaded method 'm'. |
| 199 | std::string JniShortName(const Method* m); |
| 200 | // Returns the JNI native function name for the overloaded method 'm'. |
| 201 | std::string JniLongName(const Method* m); |
| 202 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 203 | bool ReadFileToString(const std::string& file_name, std::string* result); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 204 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 205 | // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format. |
| 206 | std::string GetIsoDate(); |
| 207 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 208 | // Returns the current time in milliseconds (using the POSIX CLOCK_MONOTONIC). |
| 209 | uint64_t MilliTime(); |
| 210 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 211 | // Returns the current time in nanoseconds (using the POSIX CLOCK_MONOTONIC). |
| 212 | uint64_t NanoTime(); |
| 213 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 214 | // Splits a string using the given delimiter character into a vector of |
| 215 | // strings. Empty strings will be omitted. |
| 216 | void Split(const std::string& s, char delim, std::vector<std::string>& result); |
| 217 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 218 | // Returns the calling thread's tid. (The C libraries don't expose this.) |
| 219 | pid_t GetTid(); |
| 220 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 221 | // Reads data from "/proc/self/task/${tid}/stat". |
| 222 | void GetTaskStats(pid_t tid, int& utime, int& stime, int& task_cpu); |
| 223 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 224 | // Sets the name of the current thread. The name may be truncated to an |
| 225 | // implementation-defined limit. |
| 226 | void SetThreadName(const char* name); |
| 227 | |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 228 | // Returns the art-cache location, or dies trying. |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 229 | std::string GetArtCacheOrDie(); |
| 230 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 231 | // Returns the art-cache location for a DexFile or OatFile, or dies trying. |
| 232 | std::string GetArtCacheFilenameOrDie(const std::string& location); |
| 233 | |
| 234 | // Check whether the given filename has a valid zip or dex extension |
| 235 | bool IsValidZipFilename(const std::string& filename); |
| 236 | bool IsValidDexFilename(const std::string& filename); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 237 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 238 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 239 | |
| 240 | #endif // ART_SRC_UTILS_H_ |