blob: 9284950c1a100fbb52c1a87767c941e4968fde28 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070016
David Sehrc431b9d2018-03-02 12:01:51 -080017#ifndef ART_LIBARTBASE_BASE_UTILS_H_
18#define ART_LIBARTBASE_BASE_UTILS_H_
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070019
Elliott Hughes92b3b562011-09-08 16:32:26 -070020#include <pthread.h>
Alex Light15324762015-11-19 11:03:10 -080021#include <stdlib.h>
Elliott Hughese222ee02012-12-13 14:41:43 -080022
Alex Light15324762015-11-19 11:03:10 -080023#include <random>
Elliott Hughes34023802011-08-30 12:06:17 -070024#include <string>
Elliott Hughes34023802011-08-30 12:06:17 -070025
Andreas Gampe57943812017-12-06 21:39:13 -080026#include <android-base/logging.h>
Andreas Gampef9411702018-09-06 17:16:57 -070027#include <android-base/parseint.h>
Andreas Gampe57943812017-12-06 21:39:13 -080028
David Sehr1979c642018-04-26 14:41:18 -070029#include "casts.h"
30#include "enums.h"
31#include "globals.h"
32#include "macros.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010033
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070034namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070035
Ian Rogersef7d42f2014-01-06 12:55:46 -080036static inline uint32_t PointerToLowMemUInt32(const void* p) {
37 uintptr_t intp = reinterpret_cast<uintptr_t>(p);
38 DCHECK_LE(intp, 0xFFFFFFFFU);
39 return intp & 0xFFFFFFFFU;
40}
Brian Carlstromdb4d5402011-08-09 12:18:28 -070041
Elliott Hughesc967f782012-04-16 10:23:15 -070042// Returns a human-readable size string such as "1MB".
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080043std::string PrettySize(int64_t size_in_bytes);
Ian Rogers3bb17a62012-01-27 23:56:44 -080044
Elliott Hughes48436bb2012-02-07 15:23:28 -080045// Splits a string using the given separator character into a vector of
Elliott Hughes34023802011-08-30 12:06:17 -070046// strings. Empty strings will be omitted.
Ian Rogers6f3dbba2014-10-14 17:41:57 -070047void Split(const std::string& s, char separator, std::vector<std::string>* result);
Elliott Hughes48436bb2012-02-07 15:23:28 -080048
Elliott Hughes42ee1422011-09-06 12:33:32 -070049// Returns the calling thread's tid. (The C libraries don't expose this.)
50pid_t GetTid();
51
Elliott Hughes289be852012-06-12 13:57:20 -070052// Returns the given thread's name.
53std::string GetThreadName(pid_t tid);
54
Elliott Hughesdcc24742011-09-07 14:02:44 -070055// Sets the name of the current thread. The name may be truncated to an
56// implementation-defined limit.
Elliott Hughes22869a92012-03-27 14:08:24 -070057void SetThreadName(const char* thread_name);
Elliott Hughesdcc24742011-09-07 14:02:44 -070058
David Sehr891a50e2017-10-27 17:01:07 -070059// Reads data from "/proc/self/task/${tid}/stat".
60void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu);
David Brazdil7b49e6c2016-09-01 11:06:18 +010061
Mathieu Chartierd22d5482012-11-06 17:14:12 -080062class VoidFunctor {
Mathieu Chartier357e9be2012-08-01 11:00:14 -070063 public:
Mathieu Chartierd22d5482012-11-06 17:14:12 -080064 template <typename A>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010065 inline void operator() (A a ATTRIBUTE_UNUSED) const {
Mathieu Chartierd22d5482012-11-06 17:14:12 -080066 }
67
68 template <typename A, typename B>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010069 inline void operator() (A a ATTRIBUTE_UNUSED, B b ATTRIBUTE_UNUSED) const {
Mathieu Chartierd22d5482012-11-06 17:14:12 -080070 }
71
72 template <typename A, typename B, typename C>
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010073 inline void operator() (A a ATTRIBUTE_UNUSED, B b ATTRIBUTE_UNUSED, C c ATTRIBUTE_UNUSED) const {
Mathieu Chartierd22d5482012-11-06 17:14:12 -080074 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -070075};
76
Mathieu Chartier50030ef2015-05-08 14:19:26 -070077inline bool TestBitmap(size_t idx, const uint8_t* bitmap) {
78 return ((bitmap[idx / kBitsPerByte] >> (idx % kBitsPerByte)) & 0x01) != 0;
79}
80
Mathieu Chartiere401d142015-04-22 13:56:20 -070081static inline constexpr bool ValidPointerSize(size_t pointer_size) {
82 return pointer_size == 4 || pointer_size == 8;
83}
Mathieu Chartier50030ef2015-05-08 14:19:26 -070084
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010085static inline const void* EntryPointToCodePointer(const void* entry_point) {
86 uintptr_t code = reinterpret_cast<uintptr_t>(entry_point);
87 // TODO: Make this Thumb2 specific. It is benign on other architectures as code is always at
88 // least 2 byte aligned.
89 code &= ~0x1;
90 return reinterpret_cast<const void*>(code);
91}
92
Alex Light15324762015-11-19 11:03:10 -080093#if defined(__BIONIC__)
94struct Arc4RandomGenerator {
95 typedef uint32_t result_type;
96 static constexpr uint32_t min() { return std::numeric_limits<uint32_t>::min(); }
97 static constexpr uint32_t max() { return std::numeric_limits<uint32_t>::max(); }
98 uint32_t operator() () { return arc4random(); }
99};
100using RNG = Arc4RandomGenerator;
101#else
102using RNG = std::random_device;
103#endif
104
105template <typename T>
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700106static T GetRandomNumber(T min, T max) {
Alex Light15324762015-11-19 11:03:10 -0800107 CHECK_LT(min, max);
108 std::uniform_int_distribution<T> dist(min, max);
109 RNG rng;
110 return dist(rng);
111}
112
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800113// Sleep forever and never come back.
114NO_RETURN void SleepForever();
115
Orion Hodson38d29fd2018-09-07 12:58:37 +0100116inline void FlushDataCache(void* begin, void* end) {
117 __builtin___clear_cache(reinterpret_cast<char*>(begin), reinterpret_cast<char*>(end));
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +0000118}
119
Orion Hodson38d29fd2018-09-07 12:58:37 +0100120inline void FlushInstructionCache(void* begin, void* end) {
121 // Same as FlushInstructionCache for lack of other builtin. __builtin___clear_cache
122 // flushes both caches.
123 __builtin___clear_cache(reinterpret_cast<char*>(begin), reinterpret_cast<char*>(end));
Orion Hodsonf2331362018-07-11 15:14:10 +0100124}
125
Andreas Gampebda1d602016-08-29 17:43:45 -0700126template <typename T>
127constexpr PointerSize ConvertToPointerSize(T any) {
128 if (any == 4 || any == 8) {
129 return static_cast<PointerSize>(any);
130 } else {
131 LOG(FATAL);
132 UNREACHABLE();
133 }
134}
135
buzbee31afbec2017-03-14 15:30:19 -0700136// Return -1 if <, 0 if ==, 1 if >.
137template <typename T>
138inline static int32_t Compare(T lhs, T rhs) {
139 return (lhs < rhs) ? -1 : ((lhs == rhs) ? 0 : 1);
140}
141
142// Return -1 if < 0, 0 if == 0, 1 if > 0.
143template <typename T>
144inline static int32_t Signum(T opnd) {
145 return (opnd < 0) ? -1 : ((opnd == 0) ? 0 : 1);
146}
147
Mathieu Chartier3425d022017-10-03 16:22:05 -0700148template <typename Func, typename... Args>
149static inline void CheckedCall(const Func& function, const char* what, Args... args) {
150 int rc = function(args...);
151 if (UNLIKELY(rc != 0)) {
Mathieu Chartier3425d022017-10-03 16:22:05 -0700152 PLOG(FATAL) << "Checked call failed for " << what;
153 }
154}
155
Wei Li8991ad02018-09-13 16:43:39 +0800156// Lookup value for a given key in /proc/self/status. Keys and values are separated by a ':' in
157// the status file. Returns value found on success and "<unknown>" if the key is not found or
158// there is an I/O error.
159std::string GetProcessStatus(const char* key);
160
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700161} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700162
David Sehrc431b9d2018-03-02 12:01:51 -0800163#endif // ART_LIBARTBASE_BASE_UTILS_H_