Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_PLATFORM_UTILS_H_
6#define RUNTIME_PLATFORM_UTILS_H_
7
8#include <cstdlib>
9#include <limits>
10#include <memory>
11#include <type_traits>
12
13#include "platform/assert.h"
14#include "platform/globals.h"
15
16namespace dart {
17
18class Utils {
19 public:
20 template <typename T>
21 static inline T Minimum(T x, T y) {
22 return x < y ? x : y;
23 }
24
25 template <typename T>
26 static constexpr inline T Maximum(T x, T y) {
27 return x > y ? x : y;
28 }
29
30 // Calculates absolute value of a given signed integer.
31 // `x` must not be equal to minimum value representable by `T`
32 // as its absolute value is out of range.
33 template <typename T>
34 static inline T Abs(T x) {
35 // Note: as a general rule, it is not OK to use STL in Dart VM.
36 // However, std::numeric_limits<T>::min() and max() are harmless
37 // and worthwhile exception from this rule.
38 ASSERT(x != std::numeric_limits<T>::min());
39 if (x < 0) return -x;
40 return x;
41 }
42
43 // Calculates absolute value of a given signed integer with saturation.
44 // If `x` equals to minimum value representable by `T`, then
45 // absolute value is saturated to the maximum value representable by `T`.
46 template <typename T>
47 static inline T AbsWithSaturation(T x) {
48 if (x < 0) {
49 // Note: as a general rule, it is not OK to use STL in Dart VM.
50 // However, std::numeric_limits<T>::min() and max() are harmless
51 // and worthwhile exception from this rule.
52 if (x == std::numeric_limits<T>::min()) {
53 return std::numeric_limits<T>::max();
54 }
55 return -x;
56 }
57 return x;
58 }
59
60 template <typename T>
61 static constexpr bool IsPowerOfTwo(T x) {
62 return ((x & (x - 1)) == 0) && (x != 0);
63 }
64
65 template <typename T>
66 static constexpr int ShiftForPowerOfTwo(T x) {
68 int num_shifts = 0;
69 while (x > 1) {
70 num_shifts++;
71 x = x >> 1;
72 }
73 return num_shifts;
74 }
75
76 template <typename T>
77 static constexpr bool IsAligned(T x,
78 uintptr_t alignment,
79 uintptr_t offset = 0) {
80 ASSERT(IsPowerOfTwo(alignment));
81 ASSERT(offset < alignment);
82 return (x & (alignment - 1)) == offset;
83 }
84
85 template <typename T>
86 static constexpr bool IsAligned(T* x,
87 uintptr_t alignment,
88 uintptr_t offset = 0) {
89 return IsAligned(reinterpret_cast<uword>(x), alignment, offset);
90 }
91
92 template <typename T>
93 static constexpr inline T RoundDown(T x, intptr_t alignment) {
94 ASSERT(IsPowerOfTwo(alignment));
95 return (x & -alignment);
96 }
97
98 template <typename T>
99 static inline T* RoundDown(T* x, intptr_t alignment) {
100 return reinterpret_cast<T*>(
101 RoundDown(reinterpret_cast<uword>(x), alignment));
102 }
103
104 template <typename T>
105 static constexpr inline T RoundUp(T x,
106 uintptr_t alignment,
107 uintptr_t offset = 0) {
108 ASSERT(offset < alignment);
109 return RoundDown(x + alignment - 1 + offset, alignment) - offset;
110 }
111
112 template <typename T>
113 static inline T* RoundUp(T* x, uintptr_t alignment, uintptr_t offset = 0) {
114 return reinterpret_cast<T*>(
115 RoundUp(reinterpret_cast<uword>(x), alignment, offset));
116 }
117
118 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
119 // figure 3-3, page 48, where the function is called clp2.
120 static constexpr uintptr_t RoundUpToPowerOfTwo(uintptr_t x) {
121 x = x - 1;
122 x = x | (x >> 1);
123 x = x | (x >> 2);
124 x = x | (x >> 4);
125 x = x | (x >> 8);
126 x = x | (x >> 16);
127#if defined(ARCH_IS_64_BIT)
128 x = x | (x >> 32);
129#endif // defined(ARCH_IS_64_BIT)
130 return x + 1;
131 }
132
133 static constexpr int CountOneBits64(uint64_t x) {
134 // Apparently there are x64 chips without popcount.
135#if __GNUC__ && !defined(HOST_ARCH_IA32) && !defined(HOST_ARCH_X64)
136 return __builtin_popcountll(x);
137#else
138 x = x - ((x >> 1) & 0x5555555555555555);
139 x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
140 x = (((x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f) * 0x0101010101010101) >> 56;
141 return x;
142#endif
143 }
144
145 static constexpr int CountOneBits32(uint32_t x) {
146 // Apparently there are x64 chips without popcount.
147#if __GNUC__ && !defined(HOST_ARCH_IA32) && !defined(HOST_ARCH_X64)
148 return __builtin_popcount(x);
149#else
150 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
151 // figure 5-2, page 66, where the function is called pop.
152 x = x - ((x >> 1) & 0x55555555);
153 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
154 x = (x + (x >> 4)) & 0x0F0F0F0F;
155 x = x + (x >> 8);
156 x = x + (x >> 16);
157 return static_cast<int>(x & 0x0000003F);
158#endif
159 }
160
161 static constexpr int CountOneBitsWord(uword x) {
162#ifdef ARCH_IS_64_BIT
163 return CountOneBits64(x);
164#else
165 return CountOneBits32(x);
166#endif
167 }
168
169 // TODO(koda): Compare to flsll call/intrinsic.
170 static constexpr size_t HighestBit(int64_t v) {
171 uint64_t x = static_cast<uint64_t>((v > 0) ? v : -v);
172 uint64_t t = 0;
173 size_t r = 0;
174 if ((t = x >> 32) != 0) {
175 x = t;
176 r += 32;
177 }
178 if ((t = x >> 16) != 0) {
179 x = t;
180 r += 16;
181 }
182 if ((t = x >> 8) != 0) {
183 x = t;
184 r += 8;
185 }
186 if ((t = x >> 4) != 0) {
187 x = t;
188 r += 4;
189 }
190 if ((t = x >> 2) != 0) {
191 x = t;
192 r += 2;
193 }
194 if (x > 1) r += 1;
195 return r;
196 }
197
198 static constexpr size_t BitLength(int64_t value) {
199 // Flip bits if negative (-1 becomes 0).
200 value ^= value >> (8 * sizeof(value) - 1);
201 return (value == 0) ? 0 : (Utils::HighestBit(value) + 1);
202 }
203
204 static int CountLeadingZeros32(uint32_t x) {
205#if defined(DART_HOST_OS_WINDOWS)
206 unsigned long position; // NOLINT
207 return (_BitScanReverse(&position, x) == 0)
208 ? 32
209 : 31 - static_cast<int>(position);
210#else
211 return x == 0 ? 32 : __builtin_clz(x);
212#endif
213 }
214 static int CountLeadingZeros64(uint64_t x) {
215#if defined(DART_HOST_OS_WINDOWS)
216#if defined(ARCH_IS_32_BIT)
217 const uint32_t x_hi = static_cast<uint32_t>(x >> 32);
218 if (x_hi != 0) {
219 return CountLeadingZeros32(x_hi);
220 }
221 return 32 + CountLeadingZeros32(static_cast<uint32_t>(x));
222#else
223 unsigned long position; // NOLINT
224 return (_BitScanReverse64(&position, x) == 0)
225 ? 64
226 : 63 - static_cast<int>(position);
227#endif
228#else
229 return x == 0 ? 64 : __builtin_clzll(x);
230#endif
231 }
233#ifdef ARCH_IS_64_BIT
234 return CountLeadingZeros64(x);
235#else
236 return CountLeadingZeros32(x);
237#endif
238 }
239
240 static int CountTrailingZeros32(uint32_t x) {
241#if defined(DART_HOST_OS_WINDOWS)
242 unsigned long position; // NOLINT
243 return (_BitScanForward(&position, x) == 0) ? 32
244 : static_cast<int>(position);
245#else
246 return x == 0 ? 32 : __builtin_ctz(x);
247#endif
248 }
249 static int CountTrailingZeros64(uint64_t x) {
250#if defined(DART_HOST_OS_WINDOWS)
251#if defined(ARCH_IS_32_BIT)
252 const uint32_t x_lo = static_cast<uint32_t>(x);
253 if (x_lo != 0) {
254 return CountTrailingZeros32(x_lo);
255 }
256 return 32 + CountTrailingZeros32(static_cast<uint32_t>(x >> 32));
257#else
258 unsigned long position; // NOLINT
259 return (_BitScanForward64(&position, x) == 0) ? 64
260 : static_cast<int>(position);
261#endif
262#else
263 return x == 0 ? 64 : __builtin_ctzll(x);
264#endif
265 }
267#ifdef ARCH_IS_64_BIT
268 return CountTrailingZeros64(x);
269#else
270 return CountTrailingZeros32(x);
271#endif
272 }
273
274 static uint64_t ReverseBits64(uint64_t x);
275 static uint32_t ReverseBits32(uint32_t x);
276
278#ifdef ARCH_IS_64_BIT
279 return ReverseBits64(x);
280#else
281 return ReverseBits32(x);
282#endif
283 }
284
285 // Computes magic numbers to implement DIV or MOD operator.
286 static void CalculateMagicAndShiftForDivRem(int64_t divisor,
287 int64_t* magic,
288 int64_t* shift);
289
290 // Computes a hash value for the given series of bytes.
291 static uint32_t StringHash(const void* data, int length);
292
293 // Computes a hash value for the given word.
294 static uint32_t WordHash(intptr_t key);
295
296 // Check whether an N-bit two's-complement representation can hold value.
297 template <typename T>
298 static inline bool IsInt(intptr_t N, T value) {
299 ASSERT(N >= 1);
300 constexpr intptr_t value_size_in_bits = kBitsPerByte * sizeof(T);
301 if constexpr (std::is_signed<T>::value) {
302 if (N >= value_size_in_bits) return true; // Trivially fits.
303 const T limit = static_cast<T>(1) << (N - 1);
304 return (-limit <= value) && (value < limit);
305 } else {
306 if (N > value_size_in_bits) return true; // Trivially fits.
307 const T limit = static_cast<T>(1) << (N - 1);
308 return value < limit;
309 }
310 }
311
312 template <typename T>
313 static inline bool IsUint(intptr_t N, T value) {
314 ASSERT(N >= 1);
315 constexpr intptr_t value_size_in_bits = kBitsPerByte * sizeof(T);
316 if constexpr (std::is_signed<T>::value) {
317 if (value < 0) return false; // Not an unsigned value.
318 if (N >= value_size_in_bits - 1) {
319 return true; // N can fit the magnitude bits.
320 }
321 } else {
322 if (N >= value_size_in_bits) return true; // Trivially fits.
323 }
324 const T limit = (static_cast<T>(1) << N) - 1;
325 return value <= limit;
326 }
327
328 // Check whether the magnitude of value fits in N bits. This differs from
329 // IsInt(N + 1, value) only in that this returns false for the minimum value
330 // of a N+1 bit two's complement value.
331 //
332 // Primarily used for testing whether a two's complement value can be used in
333 // a place where the sign is replaced with a marker that says whether the
334 // magnitude is added or subtracted, e.g., the U bit (bit 23) in some ARM7
335 // instructions.
336 template <typename T>
337 static inline bool MagnitudeIsUint(intptr_t N, T value) {
338 ASSERT(N >= 1);
339 if constexpr (std::is_signed<T>::value) {
340 using Unsigned = typename std::make_unsigned<T>::type;
341 if (value < 0) return IsUint<Unsigned>(N, -value);
342 }
343 return IsUint(N, value);
344 }
345
346 static inline int32_t Low16Bits(int32_t value) {
347 return static_cast<int32_t>(value & 0xffff);
348 }
349
350 static inline int32_t High16Bits(int32_t value) {
351 return static_cast<int32_t>(value >> 16);
352 }
353
354 static inline int32_t Low32Bits(int64_t value) {
355 return static_cast<int32_t>(value);
356 }
357
358 static inline int32_t High32Bits(int64_t value) {
359 return static_cast<int32_t>(value >> 32);
360 }
361
362 static inline int64_t LowHighTo64Bits(uint32_t low, int32_t high) {
363 return (static_cast<uint64_t>(high) << 32) | (low & 0x0ffffffffLL);
364 }
365
366 static inline constexpr bool IsAlphaNumeric(uint32_t c) {
367 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
369 }
370
371 static inline constexpr bool IsDecimalDigit(uint32_t c) {
372 return ('0' <= c) && (c <= '9');
373 }
374
375 static bool IsHexDigit(char c) {
376 return IsDecimalDigit(c) || (('A' <= c) && (c <= 'F')) ||
377 (('a' <= c) && (c <= 'f'));
378 }
379
380 static int HexDigitToInt(char c) {
381 ASSERT(IsHexDigit(c));
382 if (IsDecimalDigit(c)) return c - '0';
383 if (('A' <= c) && (c <= 'F')) return 10 + (c - 'A');
384 return 10 + (c - 'a');
385 }
386
387 static char IntToHexDigit(int i) {
388 ASSERT(0 <= i && i < 16);
389 if (i < 10) return static_cast<char>('0' + i);
390 return static_cast<char>('A' + (i - 10));
391 }
392
393 // Perform a range check, checking if
394 // offset + count <= length
395 // without the risk of integer overflow.
396 static inline bool RangeCheck(intptr_t offset,
397 intptr_t count,
398 intptr_t length) {
399 return offset >= 0 && count >= 0 && length >= 0 &&
400 count <= (length - offset);
401 }
402
403 static inline bool WillAddOverflow(int64_t a, int64_t b) {
404 return ((b > 0) && (a > (kMaxInt64 - b))) ||
405 ((b < 0) && (a < (kMinInt64 - b)));
406 }
407
408 static inline bool WillSubOverflow(int64_t a, int64_t b) {
409 return ((b > 0) && (a < (kMinInt64 + b))) ||
410 ((b < 0) && (a > (kMaxInt64 + b)));
411 }
412
413 // Adds two int64_t values with wrapping around
414 // (two's complement arithmetic).
415 template <typename T = int64_t>
416 static inline T AddWithWrapAround(T a, T b) {
417 // Avoid undefined behavior by doing arithmetic in the unsigned type.
418 using Unsigned = typename std::make_unsigned<T>::type;
419 return static_cast<T>(static_cast<Unsigned>(a) + static_cast<Unsigned>(b));
420 }
421
422 // Subtracts two int64_t values with wrapping around
423 // (two's complement arithmetic).
424 template <typename T = int64_t>
425 static inline T SubWithWrapAround(T a, T b) {
426 // Avoid undefined behavior by doing arithmetic in the unsigned type.
427 using Unsigned = typename std::make_unsigned<T>::type;
428 return static_cast<T>(static_cast<Unsigned>(a) - static_cast<Unsigned>(b));
429 }
430
431 // Multiplies two int64_t values with wrapping around
432 // (two's complement arithmetic).
433 template <typename T = int64_t>
434 static inline T MulWithWrapAround(T a, T b) {
435 // Avoid undefined behavior by doing arithmetic in the unsigned type.
436 using Unsigned = typename std::make_unsigned<T>::type;
437 return static_cast<T>(static_cast<Unsigned>(a) * static_cast<Unsigned>(b));
438 }
439
440 template <typename T = int64_t>
441 static inline T NegWithWrapAround(T a) {
442 // Avoid undefined behavior by doing arithmetic in the unsigned type.
443 using Unsigned = typename std::make_unsigned<T>::type;
444 return static_cast<T>(-static_cast<Unsigned>(a));
445 }
446
447 // Shifts int64_t value left. Supports any non-negative number of bits and
448 // silently discards shifted out bits.
449 static inline int64_t ShiftLeftWithTruncation(int64_t a, int64_t b) {
450 ASSERT(b >= 0);
451 if (b >= kBitsPerInt64) {
452 return 0;
453 }
454 // Avoid undefined behavior by doing arithmetic in the unsigned type.
455 return static_cast<int64_t>(static_cast<uint64_t>(a) << b);
456 }
457
458 template <typename T>
459 static inline T RotateLeft(T value, uint8_t rotate) {
460 const uint8_t width = sizeof(T) * kBitsPerByte;
461 ASSERT(0 <= rotate);
462 ASSERT(rotate <= width);
463 using Unsigned = typename std::make_unsigned<T>::type;
464 return (static_cast<Unsigned>(value) << rotate) |
465 (static_cast<T>(value) >> ((width - rotate) & (width - 1)));
466 }
467 template <typename T>
468 static inline T RotateRight(T value, uint8_t rotate) {
469 const uint8_t width = sizeof(T) * kBitsPerByte;
470 ASSERT(0 <= rotate);
471 ASSERT(rotate <= width);
472 using Unsigned = typename std::make_unsigned<T>::type;
473 return (static_cast<T>(value) >> rotate) |
474 (static_cast<Unsigned>(value) << ((width - rotate) & (width - 1)));
475 }
476
477#ifdef __GNUC__
478 __attribute__((no_sanitize("float-divide-by-zero")))
479#endif
480 static inline float
481 DivideAllowZero(float a, float b) {
482 return a / b;
483 }
484#ifdef __GNUC__
485 __attribute__((no_sanitize("float-divide-by-zero")))
486#endif
487 static inline double
488 DivideAllowZero(double a, double b) {
489 return a / b;
490 }
491
492 // Utility functions for converting values from host endianness to
493 // big or little endian values.
494 static uint16_t HostToBigEndian16(uint16_t host_value);
495 static uint32_t HostToBigEndian32(uint32_t host_value);
496 static uint64_t HostToBigEndian64(uint64_t host_value);
497 static uint16_t HostToLittleEndian16(uint16_t host_value);
498 static uint32_t HostToLittleEndian32(uint32_t host_value);
499 static uint64_t HostToLittleEndian64(uint64_t host_value);
500
501 // Going between Host <-> LE/BE is the same operation for all practical
502 // purposes.
503 static inline uint32_t BigEndianToHost32(uint32_t be_value) {
504 return HostToBigEndian32(be_value);
505 }
506 static inline uint64_t LittleEndianToHost64(uint64_t le_value) {
507 return HostToLittleEndian64(le_value);
508 }
509
510 static bool DoublesBitEqual(const double a, const double b) {
511 return bit_cast<int64_t, double>(a) == bit_cast<int64_t, double>(b);
512 }
513
514 // A double-to-integer conversion that avoids undefined behavior.
515 // Out of range values and NaNs are converted to minimum value
516 // for type T.
517 template <typename T>
518 static T SafeDoubleToInt(double v) {
519 const double min = static_cast<double>(std::numeric_limits<T>::min());
520 const double max = static_cast<double>(std::numeric_limits<T>::max());
521 return (min <= v && v <= max) ? static_cast<T>(v)
522 : std::numeric_limits<T>::min();
523 }
524
525 // dart2js represents integers as double precision floats, which can
526 // represent anything in the range -2^53 ... 2^53.
527 static bool IsJavaScriptInt(int64_t value) {
528 return ((-0x20000000000000LL <= value) && (value <= 0x20000000000000LL));
529 }
530
531 // The lowest n bits are 1, the others are 0.
532 template <typename T = uword>
533 static constexpr T NBitMask(size_t n) {
534 using Unsigned = typename std::make_unsigned<T>::type;
535 constexpr size_t kBitsPerT = sizeof(T) * kBitsPerByte;
536 assert(n <= sizeof(T) * kBitsPerT);
537 return static_cast<T>(n == kBitsPerT ? std::numeric_limits<Unsigned>::max()
538 : (static_cast<Unsigned>(1) << n) - 1);
539 }
540
541 template <typename T = uword>
542 static constexpr T Bit(size_t n) {
543 ASSERT(n < sizeof(T) * kBitsPerByte);
544 T bit = 1;
545 return bit << n;
546 }
547
548 template <typename T>
549 static constexpr bool TestBit(T mask, size_t position) {
550 ASSERT(position < sizeof(T) * kBitsPerByte);
551 return ((mask >> position) & 1) != 0;
552 }
553
554 template <typename T>
556 public:
557 explicit BitsIterator(uint32_t bits) : bits_(bits), bit_(bits & -bits) {}
558
559 DART_FORCE_INLINE T operator*() const {
560 return static_cast<T>(BitPosition(bit_));
561 }
562
563 DART_FORCE_INLINE bool operator==(const BitsIterator& other) const {
564 return bits_ == other.bits_ && bit_ == other.bit_;
565 }
566
567 DART_FORCE_INLINE bool operator!=(const BitsIterator& other) const {
568 return !(*this == other);
569 }
570
571 DART_FORCE_INLINE BitsIterator& operator++() {
572 bits_ ^= bit_;
573 bit_ = bits_ & -bits_;
574 return *this;
575 }
576
577 private:
578 // Returns position of the given bit. Unlike CountTrailingZeroes assumes
579 // that bit is not zero without checking!
580 static DART_FORCE_INLINE intptr_t BitPosition(uint32_t bit) {
581#if defined(DART_HOST_OS_WINDOWS)
582 unsigned long position; // NOLINT
583 BitScanForward(&position, bit);
584 return static_cast<int>(position);
585#else
586 return __builtin_ctz(bit);
587#endif
588 }
589
590 uint32_t bits_;
591 intptr_t bit_;
592 };
593
594 template <typename T>
595 class BitsRange {
596 public:
597 explicit BitsRange(uint32_t bits) : bits_(bits) {}
598
601
602 public:
603 const uint32_t bits_;
604 };
605
606 static char* StrError(int err, char* buffer, size_t bufsize);
607
608 // Not all platforms support strndup.
609 static char* StrNDup(const char* s, intptr_t n);
610 static char* StrDup(const char* s);
611 static intptr_t StrNLen(const char* s, intptr_t n);
612 static bool StrStartsWith(const char* s, const char* prefix) {
613 return strncmp(s, prefix, strlen(prefix)) == 0;
614 }
615
616 static int Close(int fildes);
617 static size_t Read(int filedes, void* buf, size_t nbyte);
618 static int Unlink(const char* path);
619
620 // Print formatted output info a buffer.
621 //
622 // Does not write more than size characters (including the trailing '\0').
623 //
624 // Returns the number of characters (excluding the trailing '\0')
625 // that would been written if the buffer had been big enough. If
626 // the return value is greater or equal than the given size then the
627 // output has been truncated. The return value is never negative.
628 //
629 // The buffer will always be terminated by a '\0', unless the buffer
630 // is of size 0. The buffer might be nullptr if the size is 0.
631 //
632 // This specification conforms to C99 standard which is implemented
633 // by glibc 2.1+ with one exception: the C99 standard allows a
634 // negative return value. We will terminate the vm rather than let
635 // that occur.
636 static int SNPrint(char* str, size_t size, const char* format, ...)
637 PRINTF_ATTRIBUTE(3, 4);
638 static int VSNPrint(char* str, size_t size, const char* format, va_list args);
639
640 // Allocate a string and print formatted output into a malloc'd buffer.
641 static char* SCreate(const char* format, ...) PRINTF_ATTRIBUTE(1, 2);
642 static char* VSCreate(const char* format, va_list args);
643
644 typedef std::unique_ptr<char, decltype(std::free)*> CStringUniquePtr;
645
646 // Returns str in a unique_ptr with free used as its deleter.
648
649 // Load dynamic library from the given |library_path| and return the
650 // library handle. |library_path| can be |nullptr| in which case
651 // library handle representing the executable is returned.
652 // If an error occurs returns |nullptr| and populates
653 // |error| (if provided) with an error message (caller must free this message
654 // when it is no longer needed).
655 static void* LoadDynamicLibrary(const char* library_path,
656 char** error = nullptr);
657
658 // Resolve the given |symbol| within the library referenced by the
659 // given |library_handle|.
660 // If an error occurs populates |error| (if provided) with an error message
661 // (caller must free this message when it is no longer needed).
662 // Note: on some platforms |nullptr| is a valid value for a symbol, so to
663 // check if resolution succeeded one must instead provide non-null |error|
664 // and then check if it was populated with an error message.
665 static void* ResolveSymbolInDynamicLibrary(void* library_handle,
666 const char* symbol,
667 char** error = nullptr);
668
669 // Unload the library referenced by the given |library_handle|.
670 // If an error occurs returns |nullptr| and populates
671 // |error| (if provided) with an error message (caller must free this message
672 // when it is no longer needed).
673 static void UnloadDynamicLibrary(void* library_handle,
674 char** error = nullptr);
675
676#if defined(DART_HOST_OS_LINUX)
677 static bool IsWindowsSubsystemForLinux();
678#endif
679};
680
681} // namespace dart
682
683#if defined(DART_HOST_OS_ANDROID)
685#elif defined(DART_HOST_OS_FUCHSIA)
687#elif defined(DART_HOST_OS_LINUX)
688#include "platform/utils_linux.h"
689#elif defined(DART_HOST_OS_MACOS)
690#include "platform/utils_macos.h"
691#elif defined(DART_HOST_OS_WINDOWS)
692#include "platform/utils_win.h"
693#else
694#error Unknown target os.
695#endif
696
697#endif // RUNTIME_PLATFORM_UTILS_H_
int count
static bool rotate(const SkDCubic &cubic, int zero, int index, SkDCubic &rotPath)
#define N
Definition beziers.cpp:19
DART_FORCE_INLINE BitsIterator & operator++()
Definition utils.h:571
BitsIterator(uint32_t bits)
Definition utils.h:557
DART_FORCE_INLINE T operator*() const
Definition utils.h:559
DART_FORCE_INLINE bool operator==(const BitsIterator &other) const
Definition utils.h:563
DART_FORCE_INLINE bool operator!=(const BitsIterator &other) const
Definition utils.h:567
const uint32_t bits_
Definition utils.h:603
BitsRange(uint32_t bits)
Definition utils.h:597
BitsIterator< T > end()
Definition utils.h:600
BitsIterator< T > begin()
Definition utils.h:599
static constexpr bool IsAligned(T *x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:86
static T AbsWithSaturation(T x)
Definition utils.h:47
static uint64_t HostToBigEndian64(uint64_t host_value)
static constexpr T Bit(size_t n)
Definition utils.h:542
static bool IsInt(intptr_t N, T value)
Definition utils.h:298
static int CountLeadingZeros64(uint64_t x)
Definition utils.h:214
static T * RoundUp(T *x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:113
static bool IsJavaScriptInt(int64_t value)
Definition utils.h:527
static constexpr uintptr_t RoundUpToPowerOfTwo(uintptr_t x)
Definition utils.h:120
static uint64_t HostToLittleEndian64(uint64_t host_value)
static T Abs(T x)
Definition utils.h:34
static void CalculateMagicAndShiftForDivRem(int64_t divisor, int64_t *magic, int64_t *shift)
Definition utils.cc:39
static int HexDigitToInt(char c)
Definition utils.h:380
static T MulWithWrapAround(T a, T b)
Definition utils.h:434
static void * LoadDynamicLibrary(const char *library_path, char **error=nullptr)
Definition utils.cc:293
static int32_t Low32Bits(int64_t value)
Definition utils.h:354
static int32_t Low16Bits(int32_t value)
Definition utils.h:346
static T SafeDoubleToInt(double v)
Definition utils.h:518
static uword ReverseBitsWord(uword x)
Definition utils.h:277
static constexpr int CountOneBitsWord(uword x)
Definition utils.h:161
static constexpr T Maximum(T x, T y)
Definition utils.h:26
static int SNPrint(char *str, size_t size, const char *format,...) PRINTF_ATTRIBUTE(3
static bool MagnitudeIsUint(intptr_t N, T value)
Definition utils.h:337
static constexpr bool IsDecimalDigit(uint32_t c)
Definition utils.h:371
static constexpr int ShiftForPowerOfTwo(T x)
Definition utils.h:66
static int CountLeadingZeros32(uint32_t x)
Definition utils.h:204
static char * StrDup(const char *s)
static CStringUniquePtr CreateCStringUniquePtr(char *str)
Definition utils.cc:257
static bool StrStartsWith(const char *s, const char *prefix)
Definition utils.h:612
static int CountTrailingZeros64(uint64_t x)
Definition utils.h:249
static constexpr T NBitMask(size_t n)
Definition utils.h:533
static double DivideAllowZero(double a, double b)
Definition utils.h:488
static uint64_t LittleEndianToHost64(uint64_t le_value)
Definition utils.h:506
static char static char * VSCreate(const char *format, va_list args)
Definition utils.cc:239
static int CountTrailingZeros32(uint32_t x)
Definition utils.h:240
static constexpr bool IsAlphaNumeric(uint32_t c)
Definition utils.h:366
static int static int VSNPrint(char *str, size_t size, const char *format, va_list args)
static int32_t High32Bits(int64_t value)
Definition utils.h:358
static uint32_t WordHash(intptr_t key)
Definition utils.cc:217
static bool WillSubOverflow(int64_t a, int64_t b)
Definition utils.h:408
static T Minimum(T x, T y)
Definition utils.h:21
std::unique_ptr< char, decltype(std::free) * > CStringUniquePtr
Definition utils.h:644
static T AddWithWrapAround(T a, T b)
Definition utils.h:416
static uint16_t HostToLittleEndian16(uint16_t host_value)
static T RotateLeft(T value, uint8_t rotate)
Definition utils.h:459
static T SubWithWrapAround(T a, T b)
Definition utils.h:425
static constexpr T RoundUp(T x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:105
static size_t Read(int filedes, void *buf, size_t nbyte)
static uint32_t StringHash(const void *data, int length)
Definition utils.cc:114
static int CountLeadingZerosWord(uword x)
Definition utils.h:232
static constexpr size_t BitLength(int64_t value)
Definition utils.h:198
static int64_t LowHighTo64Bits(uint32_t low, int32_t high)
Definition utils.h:362
static bool WillAddOverflow(int64_t a, int64_t b)
Definition utils.h:403
static uint32_t BigEndianToHost32(uint32_t be_value)
Definition utils.h:503
static void UnloadDynamicLibrary(void *library_handle, char **error=nullptr)
Definition utils.cc:352
static intptr_t StrNLen(const char *s, intptr_t n)
static constexpr int CountOneBits32(uint32_t x)
Definition utils.h:145
static constexpr bool TestBit(T mask, size_t position)
Definition utils.h:549
static char * StrError(int err, char *buffer, size_t bufsize)
static bool IsUint(intptr_t N, T value)
Definition utils.h:313
static int Close(int fildes)
static int CountTrailingZerosWord(uword x)
Definition utils.h:266
static uint32_t HostToLittleEndian32(uint32_t host_value)
static int Unlink(const char *path)
static T NegWithWrapAround(T a)
Definition utils.h:441
static float DivideAllowZero(float a, float b)
Definition utils.h:481
static constexpr int CountOneBits64(uint64_t x)
Definition utils.h:133
static char IntToHexDigit(int i)
Definition utils.h:387
static constexpr T RoundDown(T x, intptr_t alignment)
Definition utils.h:93
static bool RangeCheck(intptr_t offset, intptr_t count, intptr_t length)
Definition utils.h:396
static int32_t High16Bits(int32_t value)
Definition utils.h:350
static char * SCreate(const char *format,...) PRINTF_ATTRIBUTE(1
Definition utils.cc:231
static void * ResolveSymbolInDynamicLibrary(void *library_handle, const char *symbol, char **error=nullptr)
Definition utils.cc:326
static constexpr bool IsAligned(T x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:77
static bool DoublesBitEqual(const double a, const double b)
Definition utils.h:510
static T RotateRight(T value, uint8_t rotate)
Definition utils.h:468
static int64_t ShiftLeftWithTruncation(int64_t a, int64_t b)
Definition utils.h:449
static bool IsHexDigit(char c)
Definition utils.h:375
static uint16_t HostToBigEndian16(uint16_t host_value)
static uint32_t HostToBigEndian32(uint32_t host_value)
static constexpr size_t HighestBit(int64_t v)
Definition utils.h:170
static uint32_t ReverseBits32(uint32_t x)
Definition utils.cc:27
static uint64_t ReverseBits64(uint64_t x)
Definition utils.cc:17
static T * RoundDown(T *x, intptr_t alignment)
Definition utils.h:99
static constexpr bool IsPowerOfTwo(T x)
Definition utils.h:61
static char * StrNDup(const char *s, intptr_t n)
#define ASSERT(E)
static bool b
struct MyStruct s
struct MyStruct a[10]
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static const uint8_t buffer[]
const uint8_t uint32_t uint32_t GError ** error
uint8_t value
uint32_t uint32_t * format
static float max(float r, float g, float b)
Definition hsl.cpp:49
static float min(float r, float g, float b)
Definition hsl.cpp:48
size_t length
__attribute__((visibility("default"))) int RunBenchmarks(int argc
double y
double x
constexpr int64_t kMaxInt64
Definition globals.h:486
constexpr int64_t kMinInt64
Definition globals.h:485
constexpr intptr_t kBitsPerByte
Definition globals.h:463
uintptr_t uword
Definition globals.h:501
static int8_t data[kExtLength]
constexpr intptr_t kBitsPerInt64
Definition globals.h:467
Definition ref_ptr.h:256
#define PRINTF_ATTRIBUTE(string_index, first_to_check)
Definition globals.h:697
#define T
int32_t width
Point offset