blob: 7778918bc7514fd08a3cd79d4702a81dac92f329 [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
Carl Shapiroa2e18e12011-06-21 18:57:55 -070028template<typename T>
29static inline bool IsAligned(T x, int n) {
30 CHECK(IsPowerOfTwo(n));
31 return (x & (n - 1)) == 0;
32}
33
Carl Shapiroa2e18e12011-06-21 18:57:55 -070034template<typename T>
35static inline bool IsAligned(T* x, int n) {
36 return IsAligned(reinterpret_cast<uintptr_t>(x), n);
37}
38
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070039// Check whether an N-bit two's-complement representation can hold value.
40static inline bool IsInt(int N, word value) {
41 CHECK_LT(0, N);
42 CHECK_LT(N, kBitsPerWord);
43 word limit = static_cast<word>(1) << (N - 1);
44 return (-limit <= value) && (value < limit);
45}
46
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070047static inline bool IsUint(int N, word value) {
48 CHECK_LT(0, N);
49 CHECK_LT(N, kBitsPerWord);
50 word limit = static_cast<word>(1) << N;
51 return (0 <= value) && (value < limit);
52}
53
Carl Shapiroa2e18e12011-06-21 18:57:55 -070054static inline bool IsAbsoluteUint(int N, word value) {
55 CHECK_LT(0, N);
56 CHECK_LT(N, kBitsPerWord);
57 if (value < 0) value = -value;
58 return IsUint(N, value);
59}
60
Ian Rogersb033c752011-07-20 12:22:35 -070061static inline int32_t Low16Bits(int32_t value) {
62 return static_cast<int32_t>(value & 0xffff);
63}
64
65static inline int32_t High16Bits(int32_t value) {
66 return static_cast<int32_t>(value >> 16);
67}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070068
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070069static inline int32_t Low32Bits(int64_t value) {
70 return static_cast<int32_t>(value);
71}
72
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070073static inline int32_t High32Bits(int64_t value) {
74 return static_cast<int32_t>(value >> 32);
75}
76
Carl Shapiro61e019d2011-07-14 16:53:09 -070077template<typename T>
78static inline T RoundDown(T x, int n) {
79 CHECK(IsPowerOfTwo(n));
80 return (x & -n);
81}
82
83template<typename T>
84static inline T RoundUp(T x, int n) {
85 return RoundDown(x + n - 1, n);
86}
87
Carl Shapiroa2e18e12011-06-21 18:57:55 -070088// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070089// figure 3-3, page 48, where the function is called clp2.
90static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
91 x = x - 1;
92 x = x | (x >> 1);
93 x = x | (x >> 2);
94 x = x | (x >> 4);
95 x = x | (x >> 8);
96 x = x | (x >> 16);
97 return x + 1;
98}
99
100// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700101// figure 5-2, page 66, where the function is called pop.
102static inline int CountOneBits(uint32_t x) {
103 x = x - ((x >> 1) & 0x55555555);
104 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
105 x = (x + (x >> 4)) & 0x0F0F0F0F;
106 x = x + (x >> 8);
107 x = x + (x >> 16);
108 return static_cast<int>(x & 0x0000003F);
109}
110
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700111#define CLZ(x) __builtin_clz(x)
112
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700113static inline bool NeedsEscaping(uint16_t ch) {
114 return (ch < ' ' || ch > '~');
115}
116
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700117static inline std::string PrintableChar(uint16_t ch) {
118 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700119 result += '\'';
120 if (NeedsEscaping(ch)) {
121 StringAppendF(&result, "\\u%04x", ch);
122 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700123 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700124 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700125 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700126 return result;
127}
128
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700129// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700130template<typename StringT>
131static inline std::string PrintableString(const StringT& s) {
132 std::string result;
133 result += '"';
Elliott Hughesb465ab02011-08-24 11:21:21 -0700134 for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700135 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700136 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700137 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700138 } else {
139 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700140 }
141 }
142 result += '"';
143 return result;
144}
145
Elliott Hughes54e7df12011-09-16 11:47:04 -0700146// Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf,
147// one of which is probably more useful to you.
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700148// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
149// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
150// "java.lang.String[]", and so forth.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700151std::string PrettyDescriptor(const String* descriptor);
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700152std::string PrettyDescriptor(const std::string& descriptor);
Elliott Hughes11e45072011-08-16 17:40:46 -0700153
Elliott Hughes54e7df12011-09-16 11:47:04 -0700154// Returns a human-readable signature for 'f'. Something like "a.b.C.f" or
155// "int a.b.C.f" (depending on the value of 'with_type').
156std::string PrettyField(const Field* f, bool with_type = true);
Elliott Hughesa2501992011-08-26 19:39:54 -0700157
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700158// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
159// "a.b.C.m(II)V" (depending on the value of 'with_signature').
buzbeedfd3d702011-08-28 12:56:51 -0700160std::string PrettyMethod(const Method* m, bool with_signature = true);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700161
162// Returns a human-readable form of the name of the *class* of the given object.
163// So given an instance of java.lang.String, the output would
Elliott Hughes11e45072011-08-16 17:40:46 -0700164// be "java.lang.String". Given an array of int, the output would be "int[]".
165// Given String.class, the output would be "java.lang.Class<java.lang.String>".
Elliott Hughes54e7df12011-09-16 11:47:04 -0700166std::string PrettyTypeOf(const Object* obj);
167
168// Returns a human-readable form of the name of the given class.
169// Given String.class, the output would be "java.lang.Class<java.lang.String>".
170std::string PrettyClass(const Class* c);
Elliott Hughes11e45072011-08-16 17:40:46 -0700171
Elliott Hughes79082e32011-08-25 12:07:32 -0700172// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
173// of the JNI spec.
174std::string MangleForJni(const std::string& s);
175
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700176// Turn "java.lang.String" into "Ljava/lang/String;".
177std::string DotToDescriptor(const char* class_name);
178
Brian Carlstromaded5f72011-10-07 17:15:04 -0700179// Turn "Ljava/lang/String;" into "java.lang.String".
180std::string DescriptorToDot(const std::string& descriptor);
181
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700182// Tests whether 's' is a valid class name.
183// name_or_descriptor
184// true => "java/lang/String"
185// false => "Ljava/lang/String;" (i.e. "descriptor")
186// dot_or_slash
187// true => "java.lang.String"
188// false => "java/lang/String" (i.e. "dot or slash")
189bool IsValidClassName(const char* s, bool name_or_descriptor, bool dot_or_slash);
190
Elliott Hughes79082e32011-08-25 12:07:32 -0700191// Returns the JNI native function name for the non-overloaded method 'm'.
192std::string JniShortName(const Method* m);
193// Returns the JNI native function name for the overloaded method 'm'.
194std::string JniLongName(const Method* m);
195
Elliott Hughesd92bec42011-09-02 17:04:36 -0700196bool ReadFileToString(const std::string& file_name, std::string* result);
buzbeec143c552011-08-20 17:38:58 -0700197
Elliott Hughese27955c2011-08-26 15:21:24 -0700198// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
199std::string GetIsoDate();
200
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700201// Returns the current time in nanoseconds (using the POSIX CLOCK_MONOTONIC).
202uint64_t NanoTime();
203
Elliott Hughes34023802011-08-30 12:06:17 -0700204// Splits a string using the given delimiter character into a vector of
205// strings. Empty strings will be omitted.
206void Split(const std::string& s, char delim, std::vector<std::string>& result);
207
Elliott Hughes42ee1422011-09-06 12:33:32 -0700208// Returns the calling thread's tid. (The C libraries don't expose this.)
209pid_t GetTid();
210
Elliott Hughesdcc24742011-09-07 14:02:44 -0700211// Sets the name of the current thread. The name may be truncated to an
212// implementation-defined limit.
213void SetThreadName(const char* name);
214
Brian Carlstroma9f19782011-10-13 00:14:47 -0700215// Returns the art-cache location or dies trying
216std::string GetArtCacheOrDie();
217
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700218} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700219
220#endif // ART_SRC_UTILS_H_