12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
16a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_UTILS_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_UTILS_H_
19a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
2092b3b5623ec8b65f3e099c076e247bb8273692f8Elliott Hughes#include <pthread.h>
21e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes
2253cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light#include <limits>
23be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko#include <memory>
2434023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes#include <string>
2534023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes#include <vector>
2634023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes
27e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "base/logging.h"
289ee0f776095c0a93f388a29f2c37689892baa2dfMathieu Chartier#include "base/mutex.h"
29e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "globals.h"
300e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom#include "instruction_set.h"
319ee0f776095c0a93f388a29f2c37689892baa2dfMathieu Chartier#include "primitive.h"
32e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes
33bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#ifdef HAVE_ANDROID_OS
34bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#include "cutils/properties.h"
35bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle#endif
36bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle
376b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapironamespace art {
38a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
390571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogersclass DexFile;
402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers
412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersnamespace mirror {
42ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromclass ArtField;
43ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromclass ArtMethod;
442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersclass Class;
4511e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughesclass Object;
465174fe6e4e931c423e910366ff22ce0838567940Elliott Hughesclass String;
472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers}  // namespace mirror
4811e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes
490325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartierenum TimeUnit {
500325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier  kTimeUnitNanosecond,
510325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier  kTimeUnitMicrosecond,
520325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier  kTimeUnitMillisecond,
530325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier  kTimeUnitSecond,
540325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier};
550325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier
5653cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Lighttemplate <typename T>
5753cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Lightbool ParseUint(const char *in, T* out) {
5853cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  char* end;
5953cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  unsigned long long int result = strtoull(in, &end, 0);  // NOLINT(runtime/int)
6053cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  if (in == end || *end != '\0') {
6153cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light    return false;
6253cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  }
6353cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  if (std::numeric_limits<T>::max() < result) {
6453cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light    return false;
6553cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  }
6653cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  *out = static_cast<T>(result);
6753cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  return true;
6853cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light}
6953cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light
7053cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Lighttemplate <typename T>
7153cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Lightbool ParseInt(const char* in, T* out) {
7253cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  char* end;
7353cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  long long int result = strtoll(in, &end, 0);  // NOLINT(runtime/int)
7453cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  if (in == end || *end != '\0') {
7553cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light    return false;
7653cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  }
7753cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  if (result < std::numeric_limits<T>::min() || std::numeric_limits<T>::max() < result) {
7853cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light    return false;
7953cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  }
8053cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  *out = static_cast<T>(result);
8153cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light  return true;
8253cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light}
8353cb16b98acf3cf6f3a1e2204ad4958ecf1b5a3cAlex Light
84a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirotemplate<typename T>
858194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr bool IsPowerOfTwo(T x) {
86a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  return (x & (x - 1)) == 0;
87a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
88a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
8906b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughestemplate<int n, typename T>
9006b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughesstatic inline bool IsAligned(T x) {
9106b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes  COMPILE_ASSERT((n & (n - 1)) == 0, n_not_power_of_two);
92a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  return (x & (n - 1)) == 0;
93a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
94a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
9506b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughestemplate<int n, typename T>
9606b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughesstatic inline bool IsAligned(T* x) {
9789521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom  return IsAligned<n>(reinterpret_cast<const uintptr_t>(x));
98a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
99a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
100af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampetemplate<typename T>
101af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampestatic inline bool IsAlignedParam(T x, int n) {
102af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe  return (x & (n - 1)) == 0;
103af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe}
104af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe
10506b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes#define CHECK_ALIGNED(value, alignment) \
10689521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom  CHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
10706b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes
10806b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes#define DCHECK_ALIGNED(value, alignment) \
10989521898b56f2ebc3fb68acfb6bc6dde9b6f5c38Brian Carlstrom  DCHECK(::art::IsAligned<alignment>(value)) << reinterpret_cast<const void*>(value)
110af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe
111af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe#define DCHECK_ALIGNED_PARAM(value, alignment) \
112af13ad9fd18b6f75fe82e7995224c55654594f93Andreas Gampe  DCHECK(::art::IsAlignedParam(value, alignment)) << reinterpret_cast<const void*>(value)
11306b37d91bb3d543002b1aee9829691f5e8bebc7eElliott Hughes
114a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro// Check whether an N-bit two's-complement representation can hold value.
115a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline bool IsInt(int N, word value) {
116a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  CHECK_LT(0, N);
117a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  CHECK_LT(N, kBitsPerWord);
118a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  word limit = static_cast<word>(1) << (N - 1);
119a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  return (-limit <= value) && (value < limit);
120a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro}
121a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
122a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapirostatic inline bool IsUint(int N, word value) {
123a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  CHECK_LT(0, N);
124a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  CHECK_LT(N, kBitsPerWord);
125a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  word limit = static_cast<word>(1) << N;
126a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro  return (0 <= value) && (value < limit);
127a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro}
128a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
129a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapirostatic inline bool IsAbsoluteUint(int N, word value) {
130a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  CHECK_LT(0, N);
131a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  CHECK_LT(N, kBitsPerWord);
132a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  if (value < 0) value = -value;
133a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro  return IsUint(N, value);
134a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
135a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
1364ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint16_t Low16Bits(uint32_t value) {
1374ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  return static_cast<uint16_t>(value);
138b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers}
139b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers
1404ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint16_t High16Bits(uint32_t value) {
1414ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  return static_cast<uint16_t>(value >> 16);
142b033c75ebda80ac75f936366fe78d1edf5cec937Ian Rogers}
143a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
1444ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint32_t Low32Bits(uint64_t value) {
1454ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  return static_cast<uint32_t>(value);
146a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro}
147a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
1484ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbeestatic inline uint32_t High32Bits(uint64_t value) {
1494ef3e45d7c6ec3c482a1a48f4df470811aa3cf0abuzbee  return static_cast<uint32_t>(value >> 32);
150a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro}
151a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
1520a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier// A static if which determines whether to return type A or B based on the condition boolean.
1538194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markotemplate <bool condition, typename A, typename B>
1540a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartierstruct TypeStaticIf {
1558194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  typedef A type;
1560a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier};
1570a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier
1580a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier// Specialization to handle the false case.
1590a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartiertemplate <typename A, typename B>
1600a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartierstruct TypeStaticIf<false, A,  B> {
1618194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  typedef B type;
1628194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko};
1638194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko
1648194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko// Type identity.
1658194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markotemplate <typename T>
1668194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostruct TypeIdentity {
1678194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  typedef T type;
1680a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier};
1690a9dc05e704bfd033bac2aa38a4fc6f6b8e6cf93Mathieu Chartier
1703f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin// Like sizeof, but count how many bits a type takes. Pass type explicitly.
1713f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkintemplate <typename T>
1723f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkinstatic constexpr size_t BitSizeOf() {
1733f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin  return sizeof(T) * CHAR_BIT;
1743f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin}
1753f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin
1763f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin// Like sizeof, but count how many bits a type takes. Infers type from parameter.
1773f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkintemplate <typename T>
1783f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkinstatic constexpr size_t BitSizeOf(T x) {
1793f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin  return sizeof(T) * CHAR_BIT;
1803f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin}
1813f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin
1820941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi// For rounding integers.
18361e019d291583029c01b61b93bea750f2b663c37Carl Shapirotemplate<typename T>
184b76cac637691c29daa9c44e493b5bc26346ed116Mathieu Chartierstatic constexpr T RoundDown(T x, typename TypeIdentity<T>::type n) WARN_UNUSED;
18598d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang
18698d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yangtemplate<typename T>
1878194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr T RoundDown(T x, typename TypeIdentity<T>::type n) {
1888194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return
189836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko      DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))
190836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko      (x & -n);
19161e019d291583029c01b61b93bea750f2b663c37Carl Shapiro}
19261e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
19361e019d291583029c01b61b93bea750f2b663c37Carl Shapirotemplate<typename T>
194b76cac637691c29daa9c44e493b5bc26346ed116Mathieu Chartierstatic constexpr T RoundUp(T x, typename TypeIdentity<T>::type n) WARN_UNUSED;
19598d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang
19698d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yangtemplate<typename T>
1978194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr T RoundUp(T x, typename TypeIdentity<T>::type n) {
19861e019d291583029c01b61b93bea750f2b663c37Carl Shapiro  return RoundDown(x + n - 1, n);
19961e019d291583029c01b61b93bea750f2b663c37Carl Shapiro}
20061e019d291583029c01b61b93bea750f2b663c37Carl Shapiro
2010941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi// For aligning pointers.
2020941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchitemplate<typename T>
203b76cac637691c29daa9c44e493b5bc26346ed116Mathieu Chartierstatic inline T* AlignDown(T* x, uintptr_t n) WARN_UNUSED;
20498d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang
20598d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yangtemplate<typename T>
2068194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic inline T* AlignDown(T* x, uintptr_t n) {
2078194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return reinterpret_cast<T*>(RoundDown(reinterpret_cast<uintptr_t>(x), n));
2080941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi}
2090941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi
2100941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchitemplate<typename T>
211b76cac637691c29daa9c44e493b5bc26346ed116Mathieu Chartierstatic inline T* AlignUp(T* x, uintptr_t n) WARN_UNUSED;
21298d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yang
21398d1cc8033251c93786e2fa8c59a2e555a9493beMingyao Yangtemplate<typename T>
2148194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic inline T* AlignUp(T* x, uintptr_t n) {
2158194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return reinterpret_cast<T*>(RoundUp(reinterpret_cast<uintptr_t>(x), n));
2160941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi}
2170941b0423537a6a5d7c1df6dd23e9864ea8f319cHiroshi Yamauchi
2183f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkinnamespace utils {
2193f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkinnamespace detail {  // Private, implementation-specific namespace. Do not poke outside of this file.
2203f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkintemplate <typename T>
2213f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkinstatic constexpr inline T RoundUpToPowerOfTwoRecursive(T x, size_t bit) {
2223f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin  return bit == (BitSizeOf<T>()) ? x: RoundUpToPowerOfTwoRecursive(x | x >> bit, bit << 1);
2233f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin}
2243f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin}  // namespace detail
2253f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin}  // namespace utils
2263f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin
2273f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin// Recursive implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
2283f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin// figure 3-3, page 48, where the function is called clp2.
2293f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkintemplate <typename T>
2303f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkinstatic constexpr inline T RoundUpToPowerOfTwo(T x) {
2313f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin  return art::utils::detail::RoundUpToPowerOfTwoRecursive(x - 1, 1) + 1;
2323f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin}
2333f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin
234a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
2351fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro// figure 3-3, page 48, where the function is called clp2.
2361fb8620309a4e94d11879aabc33364acfa733904Carl Shapirostatic inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
2371fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x - 1;
2381fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 1);
2391fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 2);
2401fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 4);
2411fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 8);
2421fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  x = x | (x >> 16);
2431fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro  return x + 1;
2441fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro}
2451fb8620309a4e94d11879aabc33364acfa733904Carl Shapiro
2463f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin// Find the bit position of the most significant bit (0-based), or -1 if there were no bits set.
2473f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkintemplate <typename T>
2483f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkinstatic constexpr ssize_t MostSignificantBit(T value) {
2493f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin  return (value == 0) ? -1 : (MostSignificantBit(value >> 1) + 1);
2503f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin}
2513f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin
2523f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin// How many bits (minimally) does it take to store the constant 'value'? i.e. 1 for 1, 3 for 5, etc.
2533f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkintemplate <typename T>
2543f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkinstatic constexpr size_t MinimumBitsToStore(T value) {
2553f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin  return static_cast<size_t>(MostSignificantBit(value) + 1);
2563f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin}
2573f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin
2588194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markotemplate<typename T>
2598194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr int CLZ(T x) {
2603f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin  static_assert(sizeof(T) <= sizeof(long long), "T too large, must be smaller than long long");  // NOLINT [runtime/int] [4]
2618194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return (sizeof(T) == sizeof(uint32_t))
2623f735bd4f9d09a0f9b2b01321e4c6917879dcae6Igor Murashkin      ? __builtin_clz(x)  // TODO: __builtin_clz[ll] has undefined behavior for x=0
2638194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      : __builtin_clzll(x);
264a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro}
265a2e18e1e77fc25c8260aad5daa267ababfcb65f6Carl Shapiro
266ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate<typename T>
2678194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr int CTZ(T x) {
2688194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return (sizeof(T) == sizeof(uint32_t))
2698194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      ? __builtin_ctz(x)
2708194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      : __builtin_ctzll(x);
271ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}
272ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
273ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate<typename T>
2748194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Markostatic constexpr int POPCOUNT(T x) {
2758194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko  return (sizeof(T) == sizeof(uint32_t))
2768194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      ? __builtin_popcount(x)
2778194963098247be6bca9cc4a54dbfa65c73e8cccVladimir Marko      : __builtin_popcountll(x);
278ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}
279ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
280ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic inline uint32_t PointerToLowMemUInt32(const void* p) {
281ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uintptr_t intp = reinterpret_cast<uintptr_t>(p);
282ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  DCHECK_LE(intp, 0xFFFFFFFFU);
283ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  return intp & 0xFFFFFFFFU;
284ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers}
285db4d54081f09abcbe97ffdf615874f2809a9e777Brian Carlstrom
28646b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughesstatic inline bool NeedsEscaping(uint16_t ch) {
28746b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes  return (ch < ' ' || ch > '~');
28846b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes}
28946b92ba72247e10884714d0b683bdb5e9d9ce59dElliott Hughes
290c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe// Interpret the bit pattern of input (type U) as type V. Requires the size
291c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe// of V >= size of U (compile-time checked).
292c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampetemplate<typename U, typename V>
293c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampestatic inline V bit_cast(U in) {
294c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  COMPILE_ASSERT(sizeof(U) <= sizeof(V), size_of_u_not_le_size_of_v);
295c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  union {
296c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe    U u;
297c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe    V v;
298c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  } tmp;
299c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  tmp.u = in;
300c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe  return tmp.v;
301c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe}
302c200a4abeca91e19969f5b35543f17f812ba32b9Andreas Gampe
303576ca0cd692c0b6ae70e776de91015b8ff000a08Ian Rogersstd::string PrintableChar(uint16_t ch);
304c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes
30582914b6164fd0109531391975389e4f0ff6832c8Elliott Hughes// Returns an ASCII string corresponding to the given UTF-8 string.
30682914b6164fd0109531391975389e4f0ff6832c8Elliott Hughes// Java escapes are used for non-ASCII characters.
307cc2f2393e69a9b1425bad1a89f41aaaf8c38f9e2Ian Rogersstd::string PrintableString(const char* utf8);
308c7ac37f0b8b64cfb53d8b9cc8dddbb34be3dd5eeElliott Hughes
309f1a5adc87760f938b01df26d906295063546b259Elliott Hughes// Tests whether 's' starts with 'prefix'.
310f1a5adc87760f938b01df26d906295063546b259Elliott Hughesbool StartsWith(const std::string& s, const char* prefix);
311f1a5adc87760f938b01df26d906295063546b259Elliott Hughes
3127a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrom// Tests whether 's' starts with 'suffix'.
3137a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrombool EndsWith(const std::string& s, const char* suffix);
3147a967b3d4468ab56bf1b75ebd4d7bf9e6798761bBrian Carlstrom
31554e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf,
31654e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// one of which is probably more useful to you.
317a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
318a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
319a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "java.lang.String[]", and so forth.
320ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyDescriptor(mirror::String* descriptor)
321ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
322cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogersstd::string PrettyDescriptor(const char* descriptor);
323ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyDescriptor(mirror::Class* klass)
324b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3259ee0f776095c0a93f388a29f2c37689892baa2dfMathieu Chartierstd::string PrettyDescriptor(Primitive::Type type);
32611e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes
32754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Returns a human-readable signature for 'f'. Something like "a.b.C.f" or
32854e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// "int a.b.C.f" (depending on the value of 'with_type').
329ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyField(mirror::ArtField* f, bool with_type = true)
330b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3316f29d0e6d5444ff84157c922c23c221567dcc6c5Brian Carlstromstd::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type = true);
332a2501990dd0f68baf38ce19251949d7bb3ecfe5aElliott Hughes
333a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
334a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// "a.b.C.m(II)V" (depending on the value of 'with_signature').
335ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyMethod(mirror::ArtMethod* m, bool with_signature = true)
336b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3370571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogersstd::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature = true);
338a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes
339a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// Returns a human-readable form of the name of the *class* of the given object.
340a0b8feb34a8492c6b8d430f6ca0716e7ff4f4c57Elliott Hughes// So given an instance of java.lang.String, the output would
34111e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes// be "java.lang.String". Given an array of int, the output would be "int[]".
34211e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes// Given String.class, the output would be "java.lang.Class<java.lang.String>".
343ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyTypeOf(mirror::Object* obj)
344b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
3454c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier
3464c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier// Returns a human-readable form of the type at an index in the specified dex file.
3474c70d77decc1217c8b1ebf8126b8d9d024c75d82Mathieu Chartier// Example outputs: char[], java.lang.String.
34818c24b6e05e7591069f1a2ac9c614b28fc0853acMathieu Chartierstd::string PrettyType(uint32_t type_idx, const DexFile& dex_file);
34954e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes
35054e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Returns a human-readable form of the name of the given class.
35154e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes// Given String.class, the output would be "java.lang.Class<java.lang.String>".
352ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyClass(mirror::Class* c)
353b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
35411e45077acba2e757799a00b3be9d63fec36a7ccElliott Hughes
355d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers// Returns a human-readable form of the name of the given class with its class loader.
356ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string PrettyClassAndClassLoader(mirror::Class* c)
357b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
358d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers
359c967f78cd29b6019f7cfca40a02e9b677112da70Elliott Hughes// Returns a human-readable size string such as "1MB".
360e6da9af8dfe0a3e3fbc2be700554f6478380e7b9Mathieu Chartierstd::string PrettySize(int64_t size_in_bytes);
3613bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers
3623bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// Returns a human-readable time string which prints every nanosecond while trying to limit the
3633bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// number of trailing zeros. Prints using the largest human readable unit up to a second.
3643bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// e.g. "1ms", "1.000000001s", "1.001us"
365f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartierstd::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3);
3663bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers
3670325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier// Format a nanosecond time to specified units.
368f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartierstd::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit,
369f5997b4d3f889569d5a2b724d83d764bfbb8d106Mathieu Chartier                           size_t max_fraction_digits);
3700325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier
3710325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier// Get the appropriate unit for a nanosecond duration.
3720325e6296d2370c42e7be80d846bfc7f8b28423bMathieu ChartierTimeUnit GetAppropriateTimeUnit(uint64_t nano_duration);
3730325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier
3747020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Get the divisor to convert from a nanoseconds to a time unit.
3750325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartieruint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit);
3760325e6296d2370c42e7be80d846bfc7f8b28423bMathieu Chartier
37779082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
37879082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// of the JNI spec.
37979082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughesstd::string MangleForJni(const std::string& s);
38079082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes
381f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom// Turn "java.lang.String" into "Ljava/lang/String;".
382f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstromstd::string DotToDescriptor(const char* class_name);
383f91c8c328c922ecd522e1d3508d2603e78de8a7bBrian Carlstrom
384cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers// Turn "Ljava/lang/String;" into "java.lang.String" using the conventions of
385cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers// java.lang.Class.getName().
386f1a5adc87760f938b01df26d906295063546b259Elliott Hughesstd::string DescriptorToDot(const char* descriptor);
387aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
388cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers// Turn "Ljava/lang/String;" into "java/lang/String" using the opposite conventions of
389cb6b0f31ede2275e79e6199ec391147585a37a2aIan Rogers// java.lang.Class.getName().
39091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughesstd::string DescriptorToName(const char* descriptor);
39191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
392906e685ce43092812bf403016057376d0657a671Elliott Hughes// Tests for whether 's' is a valid class name in the three common forms:
393906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidBinaryClassName(const char* s);  // "java.lang.String"
394906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidJniClassName(const char* s);     // "java/lang/String"
395906e685ce43092812bf403016057376d0657a671Elliott Hughesbool IsValidDescriptor(const char* s);       // "Ljava/lang/String;"
39664bf5a33d55aa779ef452552a466943002d39e4fElliott Hughes
39710037c866b04550fc5461058c398c2e3e509381ajeffhao// Returns whether the given string is a valid field or method name,
39810037c866b04550fc5461058c398c2e3e509381ajeffhao// additionally allowing names that begin with '<' and end with '>'.
39910037c866b04550fc5461058c398c2e3e509381ajeffhaobool IsValidMemberName(const char* s);
40010037c866b04550fc5461058c398c2e3e509381ajeffhao
40179082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Returns the JNI native function name for the non-overloaded method 'm'.
402ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string JniShortName(mirror::ArtMethod* m)
403b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
40479082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes// Returns the JNI native function name for the overloaded method 'm'.
405ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstd::string JniLongName(mirror::ArtMethod* m)
406b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
40779082e367845bbd68ec44ef2ddd1be8ef0e1550fElliott Hughes
408d92bec457dc6c506c80e9da6b8e0c958266b5cdcElliott Hughesbool ReadFileToString(const std::string& file_name, std::string* result);
409c143c55718342519db5398e41dda31422cf16c79buzbee
410e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
411e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughesstd::string GetIsoDate();
412e27955ca3ca960928d4dbd6cb79711fce06950b3Elliott Hughes
4130512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in milliseconds.
4147162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesuint64_t MilliTime();
4157162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
4160512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in microseconds.
417a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhaouint64_t MicroTime();
418a9ef3fd82bebc6370fc3ddbb094988feb6c83022jeffhao
4190512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the monotonic time since some unspecified starting point in nanoseconds.
42083df2ac4a051ee10eafde7587de2faf3f0150fadElliott Hughesuint64_t NanoTime();
42183df2ac4a051ee10eafde7587de2faf3f0150fadElliott Hughes
4220512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes// Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable.
4230512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughesuint64_t ThreadCpuNanoTime();
4240512f02dd6623c0870c11fbf3274d7462f732136Elliott Hughes
425bb551fa68ffc57f679b8c914ac856666f0348b77Elliott Hughes// Converts the given number of nanoseconds to milliseconds.
426720ef7680573c1afd12f99f02eee3045daee5168Mathieu Chartierstatic constexpr inline uint64_t NsToMs(uint64_t ns) {
4273bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers  return ns / 1000 / 1000;
4283bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers}
4293bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers
4303bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers// Converts the given number of milliseconds to nanoseconds
431720ef7680573c1afd12f99f02eee3045daee5168Mathieu Chartierstatic constexpr inline uint64_t MsToNs(uint64_t ns) {
4323bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers  return ns * 1000 * 1000;
4333bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers}
4343bb17a644e2945c3913cfbde245d2f520d62a3ffIan Rogers
435bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#if defined(__APPLE__)
436bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom// No clocks to specify on OS/X, fake value to pass to routines that require a clock.
437bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#define CLOCK_REALTIME 0xebadf00d
438bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom#endif
439bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
44056edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers// Sleep for the given number of nanoseconds, a bad way to handle contention.
44156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogersvoid NanoSleep(uint64_t ns);
44256edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers
4432f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// Initialize a timespec to either a relative time (ms,ns), or to the absolute
4442f62cfe690257d516233b399f68d322b1cf8e4f4Hans Boehm// time corresponding to the indicated clock value plus the supplied offset.
445bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstromvoid InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts);
446bcc2926b9721f94c17ed98fae5264cc98f0e066fBrian Carlstrom
44748436bbff87aae61bba20029b4382fa41ce787feElliott Hughes// Splits a string using the given separator character into a vector of
44834023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes// strings. Empty strings will be omitted.
44948436bbff87aae61bba20029b4382fa41ce787feElliott Hughesvoid Split(const std::string& s, char separator, std::vector<std::string>& result);
45048436bbff87aae61bba20029b4382fa41ce787feElliott Hughes
4517020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Trims whitespace off both ends of the given string.
4527020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allisonstd::string Trim(std::string s);
4537020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
45448436bbff87aae61bba20029b4382fa41ce787feElliott Hughes// Joins a vector of strings into a single string, using the given separator.
45548436bbff87aae61bba20029b4382fa41ce787feElliott Hughestemplate <typename StringT> std::string Join(std::vector<StringT>& strings, char separator);
45634023801bd544e613d6e85c9a5b2e743f3710e8fElliott Hughes
45742ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes// Returns the calling thread's tid. (The C libraries don't expose this.)
45842ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughespid_t GetTid();
45942ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes
460289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes// Returns the given thread's name.
461289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughesstd::string GetThreadName(pid_t tid);
462289be85116aaf7c48413858b5d0448868b4e61f3Elliott Hughes
463120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers// Returns details of the given thread's stack.
464307091dc306c34ce9e4ee6cc3b467807b3a3bd12Elliott Hughesvoid GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size, size_t* guard_size);
465e188419b971936086a188843378375f5ced13724Elliott Hughes
466bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes// Reads data from "/proc/self/task/${tid}/stat".
4672921201dce37ba40c55b89c0deca3c34bf64168eBrian Carlstromvoid GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu);
468bfe487be25652c5456236661b9d9c3579d2296c1Elliott Hughes
4691bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes// Returns the name of the scheduler group for the given thread the current process, or the empty string.
4701bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughesstd::string GetSchedulerGroupName(pid_t tid);
4711bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes
472dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes// Sets the name of the current thread. The name may be truncated to an
473dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes// implementation-defined limit.
47422869a9026a08b544eca4cefd67386d347e30d2cElliott Hughesvoid SetThreadName(const char* thread_name);
475dcc247493fd8fb243e335c3ec08e5e625896a47cElliott Hughes
47646e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes// Dumps the native stack for thread 'tid' to 'os'.
477067d20fd2fddd947ce3a014d023e9de249082ebeKenny Rootvoid DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix = "",
478a2cee18799d7e61adb037a4219adb23fef7d022bChristopher Ferris    mirror::ArtMethod* current_method = nullptr)
479067d20fd2fddd947ce3a014d023e9de249082ebeKenny Root    NO_THREAD_SAFETY_ANALYSIS;
48046e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes
48146e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes// Dumps the kernel stack for thread 'tid' to 'os'. Note that this is only available on linux-x86.
48246e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughesvoid DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix = "", bool include_count = true);
48346e251bf7200cc06f5a9a82ee2030e650f5e1443Elliott Hughes
4847020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Find $ANDROID_ROOT, /system, or abort.
485a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstromconst char* GetAndroidRoot();
486a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom
4877020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison// Find $ANDROID_DATA, /data, or abort.
488a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstromconst char* GetAndroidData();
489a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light// Find $ANDROID_DATA, /data, or return nullptr.
490a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Lightconst char* GetAndroidDataSafe(std::string* error_msg);
491a56fcd60596ae8694da21fccde5c56832e437c56Brian Carlstrom
49211d9f06a96a6909905c248ed684366190140095cNarayan Kamath// Returns the dalvik-cache location, or dies trying. subdir will be
49311d9f06a96a6909905c248ed684366190140095cNarayan Kamath// appended to the cache location.
49411d9f06a96a6909905c248ed684366190140095cNarayan Kamathstd::string GetDalvikCacheOrDie(const char* subdir, bool create_if_absent = true);
495a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light// Return true if we found the dalvik cache and stored it in the dalvik_cache argument.
496a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light// have_android_data will be set to true if we have an ANDROID_DATA that exists,
497a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light// dalvik_cache_exists will be true if there is a dalvik-cache directory that is present.
49833c36d4f22ab6a5e61eb47b654deaf647c34e49cAndreas Gampe// The flag is_global_cache tells whether this cache is /data/dalvik-cache.
499a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Lightvoid GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache,
50033c36d4f22ab6a5e61eb47b654deaf647c34e49cAndreas Gampe                    bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache);
501a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
502a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light// Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be
503a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light// rooted at cache_location.
504a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Lightbool GetDalvikCacheFilename(const char* file_location, const char* cache_location,
505a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light                            std::string* filename, std::string* error_msg);
50611d9f06a96a6909905c248ed684366190140095cNarayan Kamath// Returns the absolute dalvik-cache path for a DexFile or OatFile, or
50711d9f06a96a6909905c248ed684366190140095cNarayan Kamath// dies trying. The path returned will be rooted at cache_location.
50811d9f06a96a6909905c248ed684366190140095cNarayan Kamathstd::string GetDalvikCacheFilenameOrDie(const char* file_location,
50911d9f06a96a6909905c248ed684366190140095cNarayan Kamath                                        const char* cache_location);
510262bf46ddc91e5b4fbd367127ff21a1877d939f2jeffhao
5110e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom// Returns the system location for an image
5122afe49450f2e018f18b5de45428b9174bfd6f196Brian Carlstromstd::string GetSystemImageFilename(const char* location, InstructionSet isa);
5130e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom
5140e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom// Returns an .odex file name next adjacent to the dex location.
5150e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom// For example, for "/foo/bar/baz.jar", return "/foo/bar/<isa>/baz.odex".
516833a48501d560c9fa7fc78ef619888138c2d374fAndreas Gampe// Note: does not support multidex location strings.
5172afe49450f2e018f18b5de45428b9174bfd6f196Brian Carlstromstd::string DexFilenameToOdexFilename(const std::string& location, InstructionSet isa);
5180e12bdc49744eb6d5c29b9611a8dbe10bac4cd53Brian Carlstrom
5197c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrom// Check whether the given magic matches a known file type.
5207c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrombool IsZipMagic(uint32_t magic);
5217c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrombool IsDexMagic(uint32_t magic);
5227c3d13aebdd8611cae58a1048bffb13cbdc465cbBrian Carlstrombool IsOatMagic(uint32_t magic);
523b7bbba49d88eae58223d9878da4069bf6d7140bfBrian Carlstrom
5246449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom// Wrapper on fork/execv to run a command in a subprocess.
5256449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrombool Exec(std::vector<std::string>& arg_vector, std::string* error_msg);
5266449c62e40ef3a9bb75f664f922555affb532ee4Brian Carlstrom
527d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartierclass VoidFunctor {
528357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier public:
529d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  template <typename A>
530df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  inline void operator() (A a) const {
531d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(a);
532d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  }
533d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier
534d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  template <typename A, typename B>
535df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  inline void operator() (A a, B b) const {
536d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(a);
537d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(b);
538d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  }
539d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier
540d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  template <typename A, typename B, typename C>
541df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom  inline void operator() (A a, B b, C c) const {
542d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(a);
543d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(b);
544d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier    UNUSED(c);
545d22d54849c96760aa1efa259d6dcfbace54da2afMathieu Chartier  }
546357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier};
547357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier
548be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko// Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below.
549be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Markostruct FreeDelete {
550be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko  // NOTE: Deleting a const object is valid but free() takes a non-const pointer.
551be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko  void operator()(const void* ptr) const {
552be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko    free(const_cast<void*>(ptr));
553be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko  }
554be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko};
555be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko
556be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko// Alias for std::unique_ptr<> that uses the C function free() to delete objects.
557be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Markotemplate <typename T>
558be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Markousing UniqueCPtr = std::unique_ptr<T, FreeDelete>;
559be4e64303cc66bda0a12eaab835caa0bcfda3cd9Vladimir Marko
5606b6b5f0e67ce03f38223a525612955663bc1799bCarl Shapiro}  // namespace art
561a5d5cfda6239d8876937e75eba43222f639d2447Carl Shapiro
562fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_UTILS_H_
563