blob: 6a18138eb522a2ae3154d95437396e0a20587ed5 [file] [log] [blame]
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_UTILS_H_
4#define ART_SRC_UTILS_H_
5
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "globals.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07007#include "logging.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07008#include "primitive.h"
Elliott Hughes11e45072011-08-16 17:40:46 -07009#include "stringpiece.h"
Elliott Hughesc7ac37f2011-08-12 12:21:58 -070010#include "stringprintf.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070011
Elliott Hughes92b3b562011-09-08 16:32:26 -070012#include <pthread.h>
Elliott Hughes34023802011-08-30 12:06:17 -070013#include <string>
14#include <vector>
15
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070016namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070017
Elliott Hughes54e7df12011-09-16 11:47:04 -070018class Class;
Ian Rogers0571d352011-11-03 19:51:38 -070019class DexFile;
Elliott Hughesa2501992011-08-26 19:39:54 -070020class Field;
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070021class Method;
Elliott Hughes11e45072011-08-16 17:40:46 -070022class Object;
Elliott Hughes5174fe62011-08-23 15:12:35 -070023class String;
Elliott Hughes11e45072011-08-16 17:40:46 -070024
Carl Shapiroa2e18e12011-06-21 18:57:55 -070025template<typename T>
26static inline bool IsPowerOfTwo(T x) {
27 return (x & (x - 1)) == 0;
28}
29
Elliott Hughes06b37d92011-10-16 11:51:29 -070030template<int n, typename T>
31static inline bool IsAligned(T x) {
32 COMPILE_ASSERT((n & (n - 1)) == 0, n_not_power_of_two);
Carl Shapiroa2e18e12011-06-21 18:57:55 -070033 return (x & (n - 1)) == 0;
34}
35
Elliott Hughes06b37d92011-10-16 11:51:29 -070036template<int n, typename T>
37static inline bool IsAligned(T* x) {
Brian Carlstrom89521892011-12-07 22:05:07 -080038 return IsAligned<n>(reinterpret_cast<const uintptr_t>(x));
Carl Shapiroa2e18e12011-06-21 18:57:55 -070039}
40
Elliott Hughes06b37d92011-10-16 11:51:29 -070041#define CHECK_ALIGNED(value, alignment) \
Brian Carlstrom89521892011-12-07 22:05:07 -080042 CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
Elliott Hughes06b37d92011-10-16 11:51:29 -070043
44#define DCHECK_ALIGNED(value, alignment) \
Brian Carlstrom89521892011-12-07 22:05:07 -080045 DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
Elliott Hughes06b37d92011-10-16 11:51:29 -070046
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070047// Check whether an N-bit two's-complement representation can hold value.
48static inline bool IsInt(int N, word value) {
49 CHECK_LT(0, N);
50 CHECK_LT(N, kBitsPerWord);
51 word limit = static_cast<word>(1) << (N - 1);
52 return (-limit <= value) && (value < limit);
53}
54
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070055static inline bool IsUint(int N, word value) {
56 CHECK_LT(0, N);
57 CHECK_LT(N, kBitsPerWord);
58 word limit = static_cast<word>(1) << N;
59 return (0 <= value) && (value < limit);
60}
61
Carl Shapiroa2e18e12011-06-21 18:57:55 -070062static inline bool IsAbsoluteUint(int N, word value) {
63 CHECK_LT(0, N);
64 CHECK_LT(N, kBitsPerWord);
65 if (value < 0) value = -value;
66 return IsUint(N, value);
67}
68
Ian Rogersb033c752011-07-20 12:22:35 -070069static inline int32_t Low16Bits(int32_t value) {
70 return static_cast<int32_t>(value & 0xffff);
71}
72
73static inline int32_t High16Bits(int32_t value) {
74 return static_cast<int32_t>(value >> 16);
75}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070076
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070077static inline int32_t Low32Bits(int64_t value) {
78 return static_cast<int32_t>(value);
79}
80
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070081static inline int32_t High32Bits(int64_t value) {
82 return static_cast<int32_t>(value >> 32);
83}
84
Carl Shapiro61e019d2011-07-14 16:53:09 -070085template<typename T>
86static inline T RoundDown(T x, int n) {
87 CHECK(IsPowerOfTwo(n));
88 return (x & -n);
89}
90
91template<typename T>
92static inline T RoundUp(T x, int n) {
93 return RoundDown(x + n - 1, n);
94}
95
Carl Shapiroa2e18e12011-06-21 18:57:55 -070096// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070097// figure 3-3, page 48, where the function is called clp2.
98static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
99 x = x - 1;
100 x = x | (x >> 1);
101 x = x | (x >> 2);
102 x = x | (x >> 4);
103 x = x | (x >> 8);
104 x = x | (x >> 16);
105 return x + 1;
106}
107
108// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700109// figure 5-2, page 66, where the function is called pop.
110static inline int CountOneBits(uint32_t x) {
111 x = x - ((x >> 1) & 0x55555555);
112 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
113 x = (x + (x >> 4)) & 0x0F0F0F0F;
114 x = x + (x >> 8);
115 x = x + (x >> 16);
116 return static_cast<int>(x & 0x0000003F);
117}
118
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700119#define CLZ(x) __builtin_clz(x)
120
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700121static inline bool NeedsEscaping(uint16_t ch) {
122 return (ch < ' ' || ch > '~');
123}
124
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700125static inline std::string PrintableChar(uint16_t ch) {
126 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700127 result += '\'';
128 if (NeedsEscaping(ch)) {
129 StringAppendF(&result, "\\u%04x", ch);
130 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700131 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700132 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700133 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700134 return result;
135}
136
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700137// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700138template<typename StringT>
139static inline std::string PrintableString(const StringT& s) {
140 std::string result;
141 result += '"';
Elliott Hughesb465ab02011-08-24 11:21:21 -0700142 for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700143 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700144 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700145 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700146 } else {
147 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700148 }
149 }
150 result += '"';
151 return result;
152}
153
Elliott Hughes54e7df12011-09-16 11:47:04 -0700154// Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf,
155// one of which is probably more useful to you.
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700156// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
157// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
158// "java.lang.String[]", and so forth.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700159std::string PrettyDescriptor(const String* descriptor);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700160std::string PrettyDescriptor(const std::string& descriptor);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161std::string PrettyDescriptor(Primitive::Type type);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800162std::string PrettyDescriptor(const Class* klass);
Elliott Hughes11e45072011-08-16 17:40:46 -0700163
Elliott Hughes54e7df12011-09-16 11:47:04 -0700164// Returns a human-readable signature for 'f'. Something like "a.b.C.f" or
165// "int a.b.C.f" (depending on the value of 'with_type').
166std::string PrettyField(const Field* f, bool with_type = true);
Elliott Hughesa2501992011-08-26 19:39:54 -0700167
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700168// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
169// "a.b.C.m(II)V" (depending on the value of 'with_signature').
buzbeedfd3d702011-08-28 12:56:51 -0700170std::string PrettyMethod(const Method* m, bool with_signature = true);
Ian Rogers0571d352011-11-03 19:51:38 -0700171std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature = true);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700172
173// Returns a human-readable form of the name of the *class* of the given object.
174// So given an instance of java.lang.String, the output would
Elliott Hughes11e45072011-08-16 17:40:46 -0700175// be "java.lang.String". Given an array of int, the output would be "int[]".
176// Given String.class, the output would be "java.lang.Class<java.lang.String>".
Elliott Hughes54e7df12011-09-16 11:47:04 -0700177std::string PrettyTypeOf(const Object* obj);
178
179// Returns a human-readable form of the name of the given class.
180// Given String.class, the output would be "java.lang.Class<java.lang.String>".
181std::string PrettyClass(const Class* c);
Elliott Hughes11e45072011-08-16 17:40:46 -0700182
Ian Rogersd81871c2011-10-03 13:57:23 -0700183// Returns a human-readable form of the name of the given class with its class loader.
184std::string PrettyClassAndClassLoader(const Class* c);
185
Elliott Hughes79082e32011-08-25 12:07:32 -0700186// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
187// of the JNI spec.
188std::string MangleForJni(const std::string& s);
189
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700190// Turn "java.lang.String" into "Ljava/lang/String;".
191std::string DotToDescriptor(const char* class_name);
192
Brian Carlstromaded5f72011-10-07 17:15:04 -0700193// Turn "Ljava/lang/String;" into "java.lang.String".
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800194std::string DescriptorToDot(const StringPiece& descriptor);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700195
Elliott Hughes906e6852011-10-28 14:52:10 -0700196// Tests for whether 's' is a valid class name in the three common forms:
197bool IsValidBinaryClassName(const char* s); // "java.lang.String"
198bool IsValidJniClassName(const char* s); // "java/lang/String"
199bool IsValidDescriptor(const char* s); // "Ljava/lang/String;"
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700200
jeffhao10037c82012-01-23 15:06:23 -0800201// Returns whether the given string is a valid field or method name,
202// additionally allowing names that begin with '<' and end with '>'.
203bool IsValidMemberName(const char* s);
204
Elliott Hughes79082e32011-08-25 12:07:32 -0700205// Returns the JNI native function name for the non-overloaded method 'm'.
206std::string JniShortName(const Method* m);
207// Returns the JNI native function name for the overloaded method 'm'.
208std::string JniLongName(const Method* m);
209
Elliott Hughesd92bec42011-09-02 17:04:36 -0700210bool ReadFileToString(const std::string& file_name, std::string* result);
buzbeec143c552011-08-20 17:38:58 -0700211
Elliott Hughese27955c2011-08-26 15:21:24 -0700212// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
213std::string GetIsoDate();
214
Elliott Hughes7162ad92011-10-27 14:08:42 -0700215// Returns the current time in milliseconds (using the POSIX CLOCK_MONOTONIC).
216uint64_t MilliTime();
217
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800218// Returns the current time in microseconds (using the POSIX CLOCK_MONOTONIC).
219uint64_t MicroTime();
220
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700221// Returns the current time in nanoseconds (using the POSIX CLOCK_MONOTONIC).
222uint64_t NanoTime();
223
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800224// Returns the current time in microseconds (using the POSIX CLOCK_THREAD_CPUTIME_ID).
225uint64_t ThreadCpuMicroTime();
226
Elliott Hughes34023802011-08-30 12:06:17 -0700227// Splits a string using the given delimiter character into a vector of
228// strings. Empty strings will be omitted.
229void Split(const std::string& s, char delim, std::vector<std::string>& result);
230
Elliott Hughes42ee1422011-09-06 12:33:32 -0700231// Returns the calling thread's tid. (The C libraries don't expose this.)
232pid_t GetTid();
233
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700234// Reads data from "/proc/self/task/${tid}/stat".
235void GetTaskStats(pid_t tid, int& utime, int& stime, int& task_cpu);
236
Elliott Hughesdcc24742011-09-07 14:02:44 -0700237// Sets the name of the current thread. The name may be truncated to an
238// implementation-defined limit.
239void SetThreadName(const char* name);
240
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700241// Returns the art-cache location, or dies trying.
Brian Carlstroma9f19782011-10-13 00:14:47 -0700242std::string GetArtCacheOrDie();
243
jeffhao262bf462011-10-20 18:36:32 -0700244// Returns the art-cache location for a DexFile or OatFile, or dies trying.
245std::string GetArtCacheFilenameOrDie(const std::string& location);
246
247// Check whether the given filename has a valid zip or dex extension
248bool IsValidZipFilename(const std::string& filename);
249bool IsValidDexFilename(const std::string& filename);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700250
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700251} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700252
253#endif // ART_SRC_UTILS_H_