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