blob: dd597e99655edf6177b776b5b1839782fa98efa8 [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"
Elliott Hughes11e45072011-08-16 17:40:46 -07008#include "stringpiece.h"
Elliott Hughesc7ac37f2011-08-12 12:21:58 -07009#include "stringprintf.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070010
Elliott Hughes92b3b562011-09-08 16:32:26 -070011#include <pthread.h>
Elliott Hughes34023802011-08-30 12:06:17 -070012#include <string>
13#include <vector>
14
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070015namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070016
Elliott Hughes54e7df12011-09-16 11:47:04 -070017class Class;
Elliott Hughesa2501992011-08-26 19:39:54 -070018class Field;
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070019class Method;
Elliott Hughes11e45072011-08-16 17:40:46 -070020class Object;
Elliott Hughes5174fe62011-08-23 15:12:35 -070021class String;
Elliott Hughes11e45072011-08-16 17:40:46 -070022
Carl Shapiroa2e18e12011-06-21 18:57:55 -070023template<typename T>
24static inline bool IsPowerOfTwo(T x) {
25 return (x & (x - 1)) == 0;
26}
27
Elliott Hughes06b37d92011-10-16 11:51:29 -070028template<int n, typename T>
29static inline bool IsAligned(T x) {
30 COMPILE_ASSERT((n & (n - 1)) == 0, n_not_power_of_two);
Carl Shapiroa2e18e12011-06-21 18:57:55 -070031 return (x & (n - 1)) == 0;
32}
33
Elliott Hughes06b37d92011-10-16 11:51:29 -070034template<int n, typename T>
35static inline bool IsAligned(T* x) {
36 return IsAligned<n>(reinterpret_cast<uintptr_t>(x));
Carl Shapiroa2e18e12011-06-21 18:57:55 -070037}
38
Elliott Hughes06b37d92011-10-16 11:51:29 -070039#define CHECK_ALIGNED(value, alignment) \
40 CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<void*>(value)
41
42#define DCHECK_ALIGNED(value, alignment) \
43 DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<void*>(value)
44
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070045// Check whether an N-bit two's-complement representation can hold value.
46static inline bool IsInt(int N, word value) {
47 CHECK_LT(0, N);
48 CHECK_LT(N, kBitsPerWord);
49 word limit = static_cast<word>(1) << (N - 1);
50 return (-limit <= value) && (value < limit);
51}
52
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070053static inline bool IsUint(int N, word value) {
54 CHECK_LT(0, N);
55 CHECK_LT(N, kBitsPerWord);
56 word limit = static_cast<word>(1) << N;
57 return (0 <= value) && (value < limit);
58}
59
Carl Shapiroa2e18e12011-06-21 18:57:55 -070060static inline bool IsAbsoluteUint(int N, word value) {
61 CHECK_LT(0, N);
62 CHECK_LT(N, kBitsPerWord);
63 if (value < 0) value = -value;
64 return IsUint(N, value);
65}
66
Ian Rogersb033c752011-07-20 12:22:35 -070067static inline int32_t Low16Bits(int32_t value) {
68 return static_cast<int32_t>(value & 0xffff);
69}
70
71static inline int32_t High16Bits(int32_t value) {
72 return static_cast<int32_t>(value >> 16);
73}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070074
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070075static inline int32_t Low32Bits(int64_t value) {
76 return static_cast<int32_t>(value);
77}
78
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070079static inline int32_t High32Bits(int64_t value) {
80 return static_cast<int32_t>(value >> 32);
81}
82
Carl Shapiro61e019d2011-07-14 16:53:09 -070083template<typename T>
84static inline T RoundDown(T x, int n) {
85 CHECK(IsPowerOfTwo(n));
86 return (x & -n);
87}
88
89template<typename T>
90static inline T RoundUp(T x, int n) {
91 return RoundDown(x + n - 1, n);
92}
93
Carl Shapiroa2e18e12011-06-21 18:57:55 -070094// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070095// figure 3-3, page 48, where the function is called clp2.
96static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
97 x = x - 1;
98 x = x | (x >> 1);
99 x = x | (x >> 2);
100 x = x | (x >> 4);
101 x = x | (x >> 8);
102 x = x | (x >> 16);
103 return x + 1;
104}
105
106// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700107// figure 5-2, page 66, where the function is called pop.
108static inline int CountOneBits(uint32_t x) {
109 x = x - ((x >> 1) & 0x55555555);
110 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
111 x = (x + (x >> 4)) & 0x0F0F0F0F;
112 x = x + (x >> 8);
113 x = x + (x >> 16);
114 return static_cast<int>(x & 0x0000003F);
115}
116
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700117#define CLZ(x) __builtin_clz(x)
118
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700119static inline bool NeedsEscaping(uint16_t ch) {
120 return (ch < ' ' || ch > '~');
121}
122
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700123static inline std::string PrintableChar(uint16_t ch) {
124 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700125 result += '\'';
126 if (NeedsEscaping(ch)) {
127 StringAppendF(&result, "\\u%04x", ch);
128 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700129 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700130 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700131 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700132 return result;
133}
134
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700135// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700136template<typename StringT>
137static inline std::string PrintableString(const StringT& s) {
138 std::string result;
139 result += '"';
Elliott Hughesb465ab02011-08-24 11:21:21 -0700140 for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700141 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700142 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700143 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700144 } else {
145 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700146 }
147 }
148 result += '"';
149 return result;
150}
151
Elliott Hughes54e7df12011-09-16 11:47:04 -0700152// Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf,
153// one of which is probably more useful to you.
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700154// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
155// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
156// "java.lang.String[]", and so forth.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700157std::string PrettyDescriptor(const String* descriptor);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700158std::string PrettyDescriptor(const std::string& descriptor);
Elliott Hughes11e45072011-08-16 17:40:46 -0700159
Elliott Hughes54e7df12011-09-16 11:47:04 -0700160// Returns a human-readable signature for 'f'. Something like "a.b.C.f" or
161// "int a.b.C.f" (depending on the value of 'with_type').
162std::string PrettyField(const Field* f, bool with_type = true);
Elliott Hughesa2501992011-08-26 19:39:54 -0700163
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700164// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
165// "a.b.C.m(II)V" (depending on the value of 'with_signature').
buzbeedfd3d702011-08-28 12:56:51 -0700166std::string PrettyMethod(const Method* m, bool with_signature = true);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700167
168// Returns a human-readable form of the name of the *class* of the given object.
169// So given an instance of java.lang.String, the output would
Elliott Hughes11e45072011-08-16 17:40:46 -0700170// be "java.lang.String". Given an array of int, the output would be "int[]".
171// Given String.class, the output would be "java.lang.Class<java.lang.String>".
Elliott Hughes54e7df12011-09-16 11:47:04 -0700172std::string PrettyTypeOf(const Object* obj);
173
174// Returns a human-readable form of the name of the given class.
175// Given String.class, the output would be "java.lang.Class<java.lang.String>".
176std::string PrettyClass(const Class* c);
Elliott Hughes11e45072011-08-16 17:40:46 -0700177
Elliott Hughes79082e32011-08-25 12:07:32 -0700178// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
179// of the JNI spec.
180std::string MangleForJni(const std::string& s);
181
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700182// Turn "java.lang.String" into "Ljava/lang/String;".
183std::string DotToDescriptor(const char* class_name);
184
Brian Carlstromaded5f72011-10-07 17:15:04 -0700185// Turn "Ljava/lang/String;" into "java.lang.String".
186std::string DescriptorToDot(const std::string& descriptor);
187
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700188// Tests whether 's' is a valid class name.
189// name_or_descriptor
190// true => "java/lang/String"
191// false => "Ljava/lang/String;" (i.e. "descriptor")
192// dot_or_slash
193// true => "java.lang.String"
194// false => "java/lang/String" (i.e. "dot or slash")
195bool IsValidClassName(const char* s, bool name_or_descriptor, bool dot_or_slash);
196
Elliott Hughes79082e32011-08-25 12:07:32 -0700197// Returns the JNI native function name for the non-overloaded method 'm'.
198std::string JniShortName(const Method* m);
199// Returns the JNI native function name for the overloaded method 'm'.
200std::string JniLongName(const Method* m);
201
Elliott Hughesd92bec42011-09-02 17:04:36 -0700202bool ReadFileToString(const std::string& file_name, std::string* result);
buzbeec143c552011-08-20 17:38:58 -0700203
Elliott Hughese27955c2011-08-26 15:21:24 -0700204// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
205std::string GetIsoDate();
206
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700207// Returns the current time in nanoseconds (using the POSIX CLOCK_MONOTONIC).
208uint64_t NanoTime();
209
Elliott Hughes34023802011-08-30 12:06:17 -0700210// Splits a string using the given delimiter character into a vector of
211// strings. Empty strings will be omitted.
212void Split(const std::string& s, char delim, std::vector<std::string>& result);
213
Elliott Hughes42ee1422011-09-06 12:33:32 -0700214// Returns the calling thread's tid. (The C libraries don't expose this.)
215pid_t GetTid();
216
Elliott Hughesdcc24742011-09-07 14:02:44 -0700217// Sets the name of the current thread. The name may be truncated to an
218// implementation-defined limit.
219void SetThreadName(const char* name);
220
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700221// Returns the art-cache location, or dies trying.
Brian Carlstroma9f19782011-10-13 00:14:47 -0700222std::string GetArtCacheOrDie();
223
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700224// Returns the art-cache location for an OatFile, or dies trying.
225std::string GetArtCacheOatFilenameOrDie(const std::string& location);
226
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700227} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700228
229#endif // ART_SRC_UTILS_H_