Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame^] | 17 | #ifndef ART_LIBARTBASE_BASE_UTILS_H_ |
| 18 | #define ART_LIBARTBASE_BASE_UTILS_H_ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 20 | #include <pthread.h> |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 22 | |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 23 | #include <random> |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 24 | #include <string> |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 25 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 26 | #include <android-base/logging.h> |
| 27 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 28 | #include "base/casts.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame^] | 29 | #include "base/enums.h" |
| 30 | #include "base/globals.h" |
| 31 | #include "base/macros.h" |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 32 | #include "base/stringpiece.h" |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 33 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 34 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 35 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 36 | template <typename T> |
| 37 | bool ParseUint(const char *in, T* out) { |
| 38 | char* end; |
| 39 | unsigned long long int result = strtoull(in, &end, 0); // NOLINT(runtime/int) |
| 40 | if (in == end || *end != '\0') { |
| 41 | return false; |
| 42 | } |
| 43 | if (std::numeric_limits<T>::max() < result) { |
| 44 | return false; |
| 45 | } |
| 46 | *out = static_cast<T>(result); |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | template <typename T> |
| 51 | bool ParseInt(const char* in, T* out) { |
| 52 | char* end; |
| 53 | long long int result = strtoll(in, &end, 0); // NOLINT(runtime/int) |
| 54 | if (in == end || *end != '\0') { |
| 55 | return false; |
| 56 | } |
| 57 | if (result < std::numeric_limits<T>::min() || std::numeric_limits<T>::max() < result) { |
| 58 | return false; |
| 59 | } |
| 60 | *out = static_cast<T>(result); |
| 61 | return true; |
| 62 | } |
| 63 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 64 | static inline uint32_t PointerToLowMemUInt32(const void* p) { |
| 65 | uintptr_t intp = reinterpret_cast<uintptr_t>(p); |
| 66 | DCHECK_LE(intp, 0xFFFFFFFFU); |
| 67 | return intp & 0xFFFFFFFFU; |
| 68 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 69 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 70 | // Returns a human-readable size string such as "1MB". |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 71 | std::string PrettySize(int64_t size_in_bytes); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 72 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 73 | // Splits a string using the given separator character into a vector of |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 74 | // strings. Empty strings will be omitted. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 75 | void Split(const std::string& s, char separator, std::vector<std::string>* result); |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 76 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 77 | // Returns the calling thread's tid. (The C libraries don't expose this.) |
| 78 | pid_t GetTid(); |
| 79 | |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 80 | // Returns the given thread's name. |
| 81 | std::string GetThreadName(pid_t tid); |
| 82 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 83 | // Sets the name of the current thread. The name may be truncated to an |
| 84 | // implementation-defined limit. |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 85 | void SetThreadName(const char* thread_name); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 86 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 87 | // Reads data from "/proc/self/task/${tid}/stat". |
| 88 | void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 89 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 90 | class VoidFunctor { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 91 | public: |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 92 | template <typename A> |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 93 | inline void operator() (A a ATTRIBUTE_UNUSED) const { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | template <typename A, typename B> |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 97 | inline void operator() (A a ATTRIBUTE_UNUSED, B b ATTRIBUTE_UNUSED) const { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | template <typename A, typename B, typename C> |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 101 | inline void operator() (A a ATTRIBUTE_UNUSED, B b ATTRIBUTE_UNUSED, C c ATTRIBUTE_UNUSED) const { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 102 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
Mathieu Chartier | 50030ef | 2015-05-08 14:19:26 -0700 | [diff] [blame] | 105 | inline bool TestBitmap(size_t idx, const uint8_t* bitmap) { |
| 106 | return ((bitmap[idx / kBitsPerByte] >> (idx % kBitsPerByte)) & 0x01) != 0; |
| 107 | } |
| 108 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 109 | static inline constexpr bool ValidPointerSize(size_t pointer_size) { |
| 110 | return pointer_size == 4 || pointer_size == 8; |
| 111 | } |
Mathieu Chartier | 50030ef | 2015-05-08 14:19:26 -0700 | [diff] [blame] | 112 | |
Nicolas Geoffray | 6bc4374 | 2015-10-12 18:11:10 +0100 | [diff] [blame] | 113 | static inline const void* EntryPointToCodePointer(const void* entry_point) { |
| 114 | uintptr_t code = reinterpret_cast<uintptr_t>(entry_point); |
| 115 | // TODO: Make this Thumb2 specific. It is benign on other architectures as code is always at |
| 116 | // least 2 byte aligned. |
| 117 | code &= ~0x1; |
| 118 | return reinterpret_cast<const void*>(code); |
| 119 | } |
| 120 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 121 | using UsageFn = void (*)(const char*, ...); |
| 122 | |
| 123 | template <typename T> |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 124 | static void ParseIntOption(const StringPiece& option, |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 125 | const std::string& option_name, |
| 126 | T* out, |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 127 | UsageFn usage, |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 128 | bool is_long_option = true) { |
| 129 | std::string option_prefix = option_name + (is_long_option ? "=" : ""); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 130 | DCHECK(option.starts_with(option_prefix)) << option << " " << option_prefix; |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 131 | const char* value_string = option.substr(option_prefix.size()).data(); |
Nicolas Geoffray | fca90a1 | 2015-10-30 12:05:41 +0000 | [diff] [blame] | 132 | int64_t parsed_integer_value = 0; |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 133 | if (!ParseInt(value_string, &parsed_integer_value)) { |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 134 | usage("Failed to parse %s '%s' as an integer", option_name.c_str(), value_string); |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 135 | } |
| 136 | *out = dchecked_integral_cast<T>(parsed_integer_value); |
| 137 | } |
| 138 | |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 139 | template <typename T> |
| 140 | static void ParseUintOption(const StringPiece& option, |
| 141 | const std::string& option_name, |
| 142 | T* out, |
| 143 | UsageFn usage, |
| 144 | bool is_long_option = true) { |
| 145 | ParseIntOption(option, option_name, out, usage, is_long_option); |
| 146 | if (*out < 0) { |
| 147 | usage("%s passed a negative value %d", option_name.c_str(), *out); |
| 148 | *out = 0; |
| 149 | } |
| 150 | } |
| 151 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 152 | void ParseDouble(const std::string& option, |
| 153 | char after_char, |
| 154 | double min, |
| 155 | double max, |
| 156 | double* parsed_value, |
| 157 | UsageFn Usage); |
| 158 | |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 159 | #if defined(__BIONIC__) |
| 160 | struct Arc4RandomGenerator { |
| 161 | typedef uint32_t result_type; |
| 162 | static constexpr uint32_t min() { return std::numeric_limits<uint32_t>::min(); } |
| 163 | static constexpr uint32_t max() { return std::numeric_limits<uint32_t>::max(); } |
| 164 | uint32_t operator() () { return arc4random(); } |
| 165 | }; |
| 166 | using RNG = Arc4RandomGenerator; |
| 167 | #else |
| 168 | using RNG = std::random_device; |
| 169 | #endif |
| 170 | |
| 171 | template <typename T> |
Mathieu Chartier | dc00f18 | 2016-07-14 10:10:44 -0700 | [diff] [blame] | 172 | static T GetRandomNumber(T min, T max) { |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 173 | CHECK_LT(min, max); |
| 174 | std::uniform_int_distribution<T> dist(min, max); |
| 175 | RNG rng; |
| 176 | return dist(rng); |
| 177 | } |
| 178 | |
Mathieu Chartier | 4d87df6 | 2016-01-07 15:14:19 -0800 | [diff] [blame] | 179 | // Sleep forever and never come back. |
| 180 | NO_RETURN void SleepForever(); |
| 181 | |
Roland Levillain | 3243026 | 2016-02-01 15:23:20 +0000 | [diff] [blame] | 182 | inline void FlushInstructionCache(char* begin, char* end) { |
Roland Levillain | 3243026 | 2016-02-01 15:23:20 +0000 | [diff] [blame] | 183 | __builtin___clear_cache(begin, end); |
Roland Levillain | 3243026 | 2016-02-01 15:23:20 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Nicolas Geoffray | ed015ac | 2016-12-15 17:58:48 +0000 | [diff] [blame] | 186 | inline void FlushDataCache(char* begin, char* end) { |
| 187 | // Same as FlushInstructionCache for lack of other builtin. __builtin___clear_cache |
| 188 | // flushes both caches. |
| 189 | __builtin___clear_cache(begin, end); |
| 190 | } |
| 191 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 192 | template <typename T> |
| 193 | constexpr PointerSize ConvertToPointerSize(T any) { |
| 194 | if (any == 4 || any == 8) { |
| 195 | return static_cast<PointerSize>(any); |
| 196 | } else { |
| 197 | LOG(FATAL); |
| 198 | UNREACHABLE(); |
| 199 | } |
| 200 | } |
| 201 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 202 | // Returns a type cast pointer if object pointed to is within the provided bounds. |
| 203 | // Otherwise returns nullptr. |
| 204 | template <typename T> |
| 205 | inline static T BoundsCheckedCast(const void* pointer, |
| 206 | const void* lower, |
| 207 | const void* upper) { |
| 208 | const uint8_t* bound_begin = static_cast<const uint8_t*>(lower); |
| 209 | const uint8_t* bound_end = static_cast<const uint8_t*>(upper); |
| 210 | DCHECK(bound_begin <= bound_end); |
| 211 | |
| 212 | T result = reinterpret_cast<T>(pointer); |
| 213 | const uint8_t* begin = static_cast<const uint8_t*>(pointer); |
| 214 | const uint8_t* end = begin + sizeof(*result); |
| 215 | if (begin < bound_begin || end > bound_end || begin > end) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | return result; |
| 219 | } |
| 220 | |
| 221 | template <typename T, size_t size> |
| 222 | constexpr size_t ArrayCount(const T (&)[size]) { |
| 223 | return size; |
| 224 | } |
| 225 | |
buzbee | 31afbec | 2017-03-14 15:30:19 -0700 | [diff] [blame] | 226 | // Return -1 if <, 0 if ==, 1 if >. |
| 227 | template <typename T> |
| 228 | inline static int32_t Compare(T lhs, T rhs) { |
| 229 | return (lhs < rhs) ? -1 : ((lhs == rhs) ? 0 : 1); |
| 230 | } |
| 231 | |
| 232 | // Return -1 if < 0, 0 if == 0, 1 if > 0. |
| 233 | template <typename T> |
| 234 | inline static int32_t Signum(T opnd) { |
| 235 | return (opnd < 0) ? -1 : ((opnd == 0) ? 0 : 1); |
| 236 | } |
| 237 | |
Mathieu Chartier | 3425d02 | 2017-10-03 16:22:05 -0700 | [diff] [blame] | 238 | template <typename Func, typename... Args> |
| 239 | static inline void CheckedCall(const Func& function, const char* what, Args... args) { |
| 240 | int rc = function(args...); |
| 241 | if (UNLIKELY(rc != 0)) { |
| 242 | errno = rc; |
| 243 | PLOG(FATAL) << "Checked call failed for " << what; |
| 244 | } |
| 245 | } |
| 246 | |
Mathieu Chartier | 66c9df1 | 2018-01-16 14:45:20 -0800 | [diff] [blame] | 247 | // Hash bytes using a relatively fast hash. |
| 248 | static inline size_t HashBytes(const uint8_t* data, size_t len) { |
| 249 | size_t hash = 0x811c9dc5; |
| 250 | for (uint32_t i = 0; i < len; ++i) { |
| 251 | hash = (hash * 16777619) ^ data[i]; |
| 252 | } |
| 253 | hash += hash << 13; |
| 254 | hash ^= hash >> 7; |
| 255 | hash += hash << 3; |
| 256 | hash ^= hash >> 17; |
| 257 | hash += hash << 5; |
| 258 | return hash; |
| 259 | } |
| 260 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 261 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 262 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame^] | 263 | #endif // ART_LIBARTBASE_BASE_UTILS_H_ |