blob: bf8191eeaa60f29f2afda949af4b4af79727e074 [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;
Elliott Hughesa2501992011-08-26 19:39:54 -070019class Field;
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070020class Method;
Elliott Hughes11e45072011-08-16 17:40:46 -070021class Object;
Elliott Hughes5174fe62011-08-23 15:12:35 -070022class String;
Elliott Hughes11e45072011-08-16 17:40:46 -070023
Carl Shapiroa2e18e12011-06-21 18:57:55 -070024template<typename T>
25static inline bool IsPowerOfTwo(T x) {
26 return (x & (x - 1)) == 0;
27}
28
Elliott Hughes06b37d92011-10-16 11:51:29 -070029template<int n, typename T>
30static inline bool IsAligned(T x) {
31 COMPILE_ASSERT((n & (n - 1)) == 0, n_not_power_of_two);
Carl Shapiroa2e18e12011-06-21 18:57:55 -070032 return (x & (n - 1)) == 0;
33}
34
Elliott Hughes06b37d92011-10-16 11:51:29 -070035template<int n, typename T>
36static inline bool IsAligned(T* x) {
37 return IsAligned<n>(reinterpret_cast<uintptr_t>(x));
Carl Shapiroa2e18e12011-06-21 18:57:55 -070038}
39
Elliott Hughes06b37d92011-10-16 11:51:29 -070040#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 Shapiroa5d5cfd2011-06-21 12:46:59 -070046// Check whether an N-bit two's-complement representation can hold value.
47static 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 Shapiroa5d5cfd2011-06-21 12:46:59 -070054static 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 Shapiroa2e18e12011-06-21 18:57:55 -070061static 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 Rogersb033c752011-07-20 12:22:35 -070068static inline int32_t Low16Bits(int32_t value) {
69 return static_cast<int32_t>(value & 0xffff);
70}
71
72static inline int32_t High16Bits(int32_t value) {
73 return static_cast<int32_t>(value >> 16);
74}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070075
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070076static inline int32_t Low32Bits(int64_t value) {
77 return static_cast<int32_t>(value);
78}
79
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070080static inline int32_t High32Bits(int64_t value) {
81 return static_cast<int32_t>(value >> 32);
82}
83
Carl Shapiro61e019d2011-07-14 16:53:09 -070084template<typename T>
85static inline T RoundDown(T x, int n) {
86 CHECK(IsPowerOfTwo(n));
87 return (x & -n);
88}
89
90template<typename T>
91static inline T RoundUp(T x, int n) {
92 return RoundDown(x + n - 1, n);
93}
94
Carl Shapiroa2e18e12011-06-21 18:57:55 -070095// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070096// figure 3-3, page 48, where the function is called clp2.
97static 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 Shapiroa2e18e12011-06-21 18:57:55 -0700108// figure 5-2, page 66, where the function is called pop.
109static 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 Carlstromdb4d5402011-08-09 12:18:28 -0700118#define CLZ(x) __builtin_clz(x)
119
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700120static inline bool NeedsEscaping(uint16_t ch) {
121 return (ch < ' ' || ch > '~');
122}
123
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700124static inline std::string PrintableChar(uint16_t ch) {
125 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700126 result += '\'';
127 if (NeedsEscaping(ch)) {
128 StringAppendF(&result, "\\u%04x", ch);
129 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700130 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700131 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700132 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700133 return result;
134}
135
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700136// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700137template<typename StringT>
138static inline std::string PrintableString(const StringT& s) {
139 std::string result;
140 result += '"';
Elliott Hughesb465ab02011-08-24 11:21:21 -0700141 for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700142 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700143 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700144 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700145 } else {
146 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700147 }
148 }
149 result += '"';
150 return result;
151}
152
Elliott Hughes54e7df12011-09-16 11:47:04 -0700153// Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf,
154// one of which is probably more useful to you.
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700155// 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 Hughes5174fe62011-08-23 15:12:35 -0700158std::string PrettyDescriptor(const String* descriptor);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700159std::string PrettyDescriptor(const std::string& descriptor);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700160std::string PrettyDescriptor(Primitive::Type type);
Elliott Hughes11e45072011-08-16 17:40:46 -0700161
Elliott Hughes54e7df12011-09-16 11:47:04 -0700162// 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').
164std::string PrettyField(const Field* f, bool with_type = true);
Elliott Hughesa2501992011-08-26 19:39:54 -0700165
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700166// 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').
buzbeedfd3d702011-08-28 12:56:51 -0700168std::string PrettyMethod(const Method* m, bool with_signature = true);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700169
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 Hughes11e45072011-08-16 17:40:46 -0700172// 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 Hughes54e7df12011-09-16 11:47:04 -0700174std::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>".
178std::string PrettyClass(const Class* c);
Elliott Hughes11e45072011-08-16 17:40:46 -0700179
Ian Rogersd81871c2011-10-03 13:57:23 -0700180// Returns a human-readable form of the name of the given class with its class loader.
181std::string PrettyClassAndClassLoader(const Class* c);
182
Elliott Hughes79082e32011-08-25 12:07:32 -0700183// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
184// of the JNI spec.
185std::string MangleForJni(const std::string& s);
186
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700187// Turn "java.lang.String" into "Ljava/lang/String;".
188std::string DotToDescriptor(const char* class_name);
189
Brian Carlstromaded5f72011-10-07 17:15:04 -0700190// Turn "Ljava/lang/String;" into "java.lang.String".
191std::string DescriptorToDot(const std::string& descriptor);
192
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700193// Tests whether 's' is a valid class name.
194// name_or_descriptor
195// true => "java/lang/String"
196// false => "Ljava/lang/String;" (i.e. "descriptor")
197// dot_or_slash
198// true => "java.lang.String"
199// false => "java/lang/String" (i.e. "dot or slash")
200bool IsValidClassName(const char* s, bool name_or_descriptor, bool dot_or_slash);
201
Elliott Hughes79082e32011-08-25 12:07:32 -0700202// Returns the JNI native function name for the non-overloaded method 'm'.
203std::string JniShortName(const Method* m);
204// Returns the JNI native function name for the overloaded method 'm'.
205std::string JniLongName(const Method* m);
206
Elliott Hughesd92bec42011-09-02 17:04:36 -0700207bool ReadFileToString(const std::string& file_name, std::string* result);
buzbeec143c552011-08-20 17:38:58 -0700208
Elliott Hughese27955c2011-08-26 15:21:24 -0700209// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
210std::string GetIsoDate();
211
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700212// Returns the current time in nanoseconds (using the POSIX CLOCK_MONOTONIC).
213uint64_t NanoTime();
214
Elliott Hughes34023802011-08-30 12:06:17 -0700215// Splits a string using the given delimiter character into a vector of
216// strings. Empty strings will be omitted.
217void Split(const std::string& s, char delim, std::vector<std::string>& result);
218
Elliott Hughes42ee1422011-09-06 12:33:32 -0700219// Returns the calling thread's tid. (The C libraries don't expose this.)
220pid_t GetTid();
221
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700222// Reads data from "/proc/self/task/${tid}/stat".
223void GetTaskStats(pid_t tid, int& utime, int& stime, int& task_cpu);
224
Elliott Hughesdcc24742011-09-07 14:02:44 -0700225// Sets the name of the current thread. The name may be truncated to an
226// implementation-defined limit.
227void SetThreadName(const char* name);
228
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700229// Returns the art-cache location, or dies trying.
Brian Carlstroma9f19782011-10-13 00:14:47 -0700230std::string GetArtCacheOrDie();
231
jeffhao262bf462011-10-20 18:36:32 -0700232// Returns the art-cache location for a DexFile or OatFile, or dies trying.
233std::string GetArtCacheFilenameOrDie(const std::string& location);
234
235// Check whether the given filename has a valid zip or dex extension
236bool IsValidZipFilename(const std::string& filename);
237bool IsValidDexFilename(const std::string& filename);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700238
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700239} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700240
241#endif // ART_SRC_UTILS_H_