blob: 135ea592c7919e1eb7855566b63fb73d2599fb47 [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 Hughesa2501992011-08-26 19:39:54 -070017class Field;
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070018class Method;
Elliott Hughes11e45072011-08-16 17:40:46 -070019class Object;
Elliott Hughes5174fe62011-08-23 15:12:35 -070020class String;
Elliott Hughes11e45072011-08-16 17:40:46 -070021
Carl Shapiroa2e18e12011-06-21 18:57:55 -070022template<typename T>
23static inline bool IsPowerOfTwo(T x) {
24 return (x & (x - 1)) == 0;
25}
26
Carl Shapiroa2e18e12011-06-21 18:57:55 -070027template<typename T>
28static inline bool IsAligned(T x, int n) {
29 CHECK(IsPowerOfTwo(n));
30 return (x & (n - 1)) == 0;
31}
32
Carl Shapiroa2e18e12011-06-21 18:57:55 -070033template<typename T>
34static inline bool IsAligned(T* x, int n) {
35 return IsAligned(reinterpret_cast<uintptr_t>(x), n);
36}
37
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070038// Check whether an N-bit two's-complement representation can hold value.
39static inline bool IsInt(int N, word value) {
40 CHECK_LT(0, N);
41 CHECK_LT(N, kBitsPerWord);
42 word limit = static_cast<word>(1) << (N - 1);
43 return (-limit <= value) && (value < limit);
44}
45
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070046static inline bool IsUint(int N, word value) {
47 CHECK_LT(0, N);
48 CHECK_LT(N, kBitsPerWord);
49 word limit = static_cast<word>(1) << N;
50 return (0 <= value) && (value < limit);
51}
52
Carl Shapiroa2e18e12011-06-21 18:57:55 -070053static inline bool IsAbsoluteUint(int N, word value) {
54 CHECK_LT(0, N);
55 CHECK_LT(N, kBitsPerWord);
56 if (value < 0) value = -value;
57 return IsUint(N, value);
58}
59
Ian Rogersb033c752011-07-20 12:22:35 -070060static inline int32_t Low16Bits(int32_t value) {
61 return static_cast<int32_t>(value & 0xffff);
62}
63
64static inline int32_t High16Bits(int32_t value) {
65 return static_cast<int32_t>(value >> 16);
66}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070067
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070068static inline int32_t Low32Bits(int64_t value) {
69 return static_cast<int32_t>(value);
70}
71
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070072static inline int32_t High32Bits(int64_t value) {
73 return static_cast<int32_t>(value >> 32);
74}
75
Carl Shapiro61e019d2011-07-14 16:53:09 -070076template<typename T>
77static inline T RoundDown(T x, int n) {
78 CHECK(IsPowerOfTwo(n));
79 return (x & -n);
80}
81
82template<typename T>
83static inline T RoundUp(T x, int n) {
84 return RoundDown(x + n - 1, n);
85}
86
Carl Shapiroa2e18e12011-06-21 18:57:55 -070087// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070088// figure 3-3, page 48, where the function is called clp2.
89static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
90 x = x - 1;
91 x = x | (x >> 1);
92 x = x | (x >> 2);
93 x = x | (x >> 4);
94 x = x | (x >> 8);
95 x = x | (x >> 16);
96 return x + 1;
97}
98
99// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -0700100// figure 5-2, page 66, where the function is called pop.
101static inline int CountOneBits(uint32_t x) {
102 x = x - ((x >> 1) & 0x55555555);
103 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
104 x = (x + (x >> 4)) & 0x0F0F0F0F;
105 x = x + (x >> 8);
106 x = x + (x >> 16);
107 return static_cast<int>(x & 0x0000003F);
108}
109
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700110#define CLZ(x) __builtin_clz(x)
111
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700112static inline bool NeedsEscaping(uint16_t ch) {
113 return (ch < ' ' || ch > '~');
114}
115
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700116static inline std::string PrintableChar(uint16_t ch) {
117 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700118 result += '\'';
119 if (NeedsEscaping(ch)) {
120 StringAppendF(&result, "\\u%04x", ch);
121 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700122 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700123 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700124 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700125 return result;
126}
127
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700128// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700129template<typename StringT>
130static inline std::string PrintableString(const StringT& s) {
131 std::string result;
132 result += '"';
Elliott Hughesb465ab02011-08-24 11:21:21 -0700133 for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700134 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700135 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700136 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700137 } else {
138 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700139 }
140 }
141 result += '"';
142 return result;
143}
144
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700145// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
146// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
147// "java.lang.String[]", and so forth.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700148std::string PrettyDescriptor(const String* descriptor);
Elliott Hughes11e45072011-08-16 17:40:46 -0700149
Elliott Hughesa2501992011-08-26 19:39:54 -0700150// Returns a human-readable signature for 'f'. Something like "a.b.C.f".
151std::string PrettyField(const Field* f);
152
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700153// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
154// "a.b.C.m(II)V" (depending on the value of 'with_signature').
buzbeedfd3d702011-08-28 12:56:51 -0700155std::string PrettyMethod(const Method* m, bool with_signature = true);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700156
157// Returns a human-readable form of the name of the *class* of the given object.
158// So given an instance of java.lang.String, the output would
Elliott Hughes11e45072011-08-16 17:40:46 -0700159// be "java.lang.String". Given an array of int, the output would be "int[]".
160// Given String.class, the output would be "java.lang.Class<java.lang.String>".
161std::string PrettyType(const Object* obj);
162
Elliott Hughes79082e32011-08-25 12:07:32 -0700163// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
164// of the JNI spec.
165std::string MangleForJni(const std::string& s);
166
167// Returns the JNI native function name for the non-overloaded method 'm'.
168std::string JniShortName(const Method* m);
169// Returns the JNI native function name for the overloaded method 'm'.
170std::string JniLongName(const Method* m);
171
Elliott Hughesd92bec42011-09-02 17:04:36 -0700172bool ReadFileToString(const std::string& file_name, std::string* result);
buzbeec143c552011-08-20 17:38:58 -0700173
Elliott Hughese27955c2011-08-26 15:21:24 -0700174// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
175std::string GetIsoDate();
176
Elliott Hughes34023802011-08-30 12:06:17 -0700177// Splits a string using the given delimiter character into a vector of
178// strings. Empty strings will be omitted.
179void Split(const std::string& s, char delim, std::vector<std::string>& result);
180
Elliott Hughes42ee1422011-09-06 12:33:32 -0700181// Returns the calling thread's tid. (The C libraries don't expose this.)
182pid_t GetTid();
183
Elliott Hughes92b3b562011-09-08 16:32:26 -0700184// Returns the tid of the thread that owns the given pthread mutex, or 0.
185pid_t GetOwner(pthread_mutex_t* mutex);
186
Elliott Hughesdcc24742011-09-07 14:02:44 -0700187// Sets the name of the current thread. The name may be truncated to an
188// implementation-defined limit.
189void SetThreadName(const char* name);
190
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700191} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700192
193#endif // ART_SRC_UTILS_H_