Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Types | Static Public Member Functions | List of all members
dart::Utils Class Reference

#include <utils.h>

Classes

class  BitsIterator
 
class  BitsRange
 

Public Types

typedef std::unique_ptr< char, decltype(std::free) * > CStringUniquePtr
 

Static Public Member Functions

template<typename T >
static T Minimum (T x, T y)
 
template<typename T >
static constexpr T Maximum (T x, T y)
 
template<typename T >
static T Abs (T x)
 
template<typename T >
static T AbsWithSaturation (T x)
 
template<typename T >
static constexpr bool IsPowerOfTwo (T x)
 
template<typename T >
static constexpr int ShiftForPowerOfTwo (T x)
 
template<typename T >
static constexpr bool IsAligned (T x, uintptr_t alignment, uintptr_t offset=0)
 
template<typename T >
static constexpr bool IsAligned (T *x, uintptr_t alignment, uintptr_t offset=0)
 
template<typename T >
static constexpr T RoundDown (T x, intptr_t alignment)
 
template<typename T >
static TRoundDown (T *x, intptr_t alignment)
 
template<typename T >
static constexpr T RoundUp (T x, uintptr_t alignment, uintptr_t offset=0)
 
template<typename T >
static TRoundUp (T *x, uintptr_t alignment, uintptr_t offset=0)
 
static constexpr uintptr_t RoundUpToPowerOfTwo (uintptr_t x)
 
static constexpr int CountOneBits64 (uint64_t x)
 
static constexpr int CountOneBits32 (uint32_t x)
 
static constexpr int CountOneBitsWord (uword x)
 
static constexpr size_t HighestBit (int64_t v)
 
static constexpr size_t BitLength (int64_t value)
 
static int CountLeadingZeros32 (uint32_t x)
 
static int CountLeadingZeros64 (uint64_t x)
 
static int CountLeadingZerosWord (uword x)
 
static int CountTrailingZeros32 (uint32_t x)
 
static int CountTrailingZeros64 (uint64_t x)
 
static int CountTrailingZerosWord (uword x)
 
static uint64_t ReverseBits64 (uint64_t x)
 
static uint32_t ReverseBits32 (uint32_t x)
 
static uword ReverseBitsWord (uword x)
 
static void CalculateMagicAndShiftForDivRem (int64_t divisor, int64_t *magic, int64_t *shift)
 
static uint32_t StringHash (const void *data, int length)
 
static uint32_t WordHash (intptr_t key)
 
template<typename T >
static bool IsInt (intptr_t N, T value)
 
template<typename T >
static bool IsUint (intptr_t N, T value)
 
template<typename T >
static bool MagnitudeIsUint (intptr_t N, T value)
 
static int32_t Low16Bits (int32_t value)
 
static int32_t High16Bits (int32_t value)
 
static int32_t Low32Bits (int64_t value)
 
static int32_t High32Bits (int64_t value)
 
static int64_t LowHighTo64Bits (uint32_t low, int32_t high)
 
static constexpr bool IsAlphaNumeric (uint32_t c)
 
static constexpr bool IsDecimalDigit (uint32_t c)
 
static bool IsHexDigit (char c)
 
static int HexDigitToInt (char c)
 
static char IntToHexDigit (int i)
 
static bool RangeCheck (intptr_t offset, intptr_t count, intptr_t length)
 
static bool WillAddOverflow (int64_t a, int64_t b)
 
static bool WillSubOverflow (int64_t a, int64_t b)
 
template<typename T = int64_t>
static T AddWithWrapAround (T a, T b)
 
template<typename T = int64_t>
static T SubWithWrapAround (T a, T b)
 
template<typename T = int64_t>
static T MulWithWrapAround (T a, T b)
 
template<typename T = int64_t>
static T NegWithWrapAround (T a)
 
static int64_t ShiftLeftWithTruncation (int64_t a, int64_t b)
 
template<typename T >
static T RotateLeft (T value, uint8_t rotate)
 
template<typename T >
static T RotateRight (T value, uint8_t rotate)
 
static float DivideAllowZero (float a, float b)
 
static double DivideAllowZero (double a, double b)
 
static uint16_t HostToBigEndian16 (uint16_t host_value)
 
static uint32_t HostToBigEndian32 (uint32_t host_value)
 
static uint64_t HostToBigEndian64 (uint64_t host_value)
 
static uint16_t HostToLittleEndian16 (uint16_t host_value)
 
static uint32_t HostToLittleEndian32 (uint32_t host_value)
 
static uint64_t HostToLittleEndian64 (uint64_t host_value)
 
static uint32_t BigEndianToHost32 (uint32_t be_value)
 
static uint64_t LittleEndianToHost64 (uint64_t le_value)
 
static bool DoublesBitEqual (const double a, const double b)
 
template<typename T >
static T SafeDoubleToInt (double v)
 
static bool IsJavaScriptInt (int64_t value)
 
template<typename T = uword>
static constexpr T NBitMask (size_t n)
 
template<typename T = uword>
static constexpr T Bit (size_t n)
 
template<typename T >
static constexpr bool TestBit (T mask, size_t position)
 
static char * StrError (int err, char *buffer, size_t bufsize)
 
static char * StrNDup (const char *s, intptr_t n)
 
static char * StrDup (const char *s)
 
static intptr_t StrNLen (const char *s, intptr_t n)
 
static bool StrStartsWith (const char *s, const char *prefix)
 
static int Close (int fildes)
 
static size_t Read (int filedes, void *buf, size_t nbyte)
 
static int Unlink (const char *path)
 
static int SNPrint (char *str, size_t size, const char *format,...) PRINTF_ATTRIBUTE(3
 
static int static int VSNPrint (char *str, size_t size, const char *format, va_list args)
 
static char * SCreate (const char *format,...) PRINTF_ATTRIBUTE(1
 
static char static char * VSCreate (const char *format, va_list args)
 
static CStringUniquePtr CreateCStringUniquePtr (char *str)
 
static void * LoadDynamicLibrary (const char *library_path, char **error=nullptr)
 
static void * ResolveSymbolInDynamicLibrary (void *library_handle, const char *symbol, char **error=nullptr)
 
static void UnloadDynamicLibrary (void *library_handle, char **error=nullptr)
 

Detailed Description

Definition at line 18 of file utils.h.

Member Typedef Documentation

◆ CStringUniquePtr

typedef std::unique_ptr<char, decltype(std::free)*> dart::Utils::CStringUniquePtr

Definition at line 644 of file utils.h.

Member Function Documentation

◆ Abs()

template<typename T >
static T dart::Utils::Abs ( T  x)
inlinestatic

Definition at line 34 of file utils.h.

34 {
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 }
#define ASSERT(E)
double x

◆ AbsWithSaturation()

template<typename T >
static T dart::Utils::AbsWithSaturation ( T  x)
inlinestatic

Definition at line 47 of file utils.h.

47 {
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 }

◆ AddWithWrapAround()

template<typename T = int64_t>
static T dart::Utils::AddWithWrapAround ( T  a,
T  b 
)
inlinestatic

Definition at line 416 of file utils.h.

416 {
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 }
static bool b
struct MyStruct a[10]
#define T

◆ BigEndianToHost32()

static uint32_t dart::Utils::BigEndianToHost32 ( uint32_t  be_value)
inlinestatic

Definition at line 503 of file utils.h.

503 {
504 return HostToBigEndian32(be_value);
505 }
static uint32_t HostToBigEndian32(uint32_t host_value)

◆ Bit()

template<typename T = uword>
static constexpr T dart::Utils::Bit ( size_t  n)
inlinestaticconstexpr

Definition at line 542 of file utils.h.

542 {
543 ASSERT(n < sizeof(T) * kBitsPerByte);
544 T bit = 1;
545 return bit << n;
546 }
constexpr intptr_t kBitsPerByte
Definition globals.h:463

◆ BitLength()

static constexpr size_t dart::Utils::BitLength ( int64_t  value)
inlinestaticconstexpr

Definition at line 198 of file utils.h.

198 {
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 }
static constexpr size_t HighestBit(int64_t v)
Definition utils.h:170
uint8_t value

◆ CalculateMagicAndShiftForDivRem()

void dart::Utils::CalculateMagicAndShiftForDivRem ( int64_t  divisor,
int64_t *  magic,
int64_t *  shift 
)
static

Definition at line 39 of file utils.cc.

41 {
42 ASSERT(divisor <= -2 || divisor >= 2);
43 /* The magic number M and shift S can be calculated in the following way:
44 * Let nc be the most positive value of numerator(n) such that nc = kd - 1,
45 * where divisor(d) >= 2.
46 * Let nc be the most negative value of numerator(n) such that nc = kd + 1,
47 * where divisor(d) <= -2.
48 * Thus nc can be calculated like:
49 * nc = exp + exp % d - 1, where d >= 2 and exp = 2^63.
50 * nc = -exp + (exp + 1) % d, where d >= 2 and exp = 2^63.
51 *
52 * So the shift p is the smallest p satisfying
53 * 2^p > nc * (d - 2^p % d), where d >= 2
54 * 2^p > nc * (d + 2^p % d), where d <= -2.
55 *
56 * The magic number M is calculated by
57 * M = (2^p + d - 2^p % d) / d, where d >= 2
58 * M = (2^p - d - 2^p % d) / d, where d <= -2.
59 */
60 int64_t p = 63;
61 const uint64_t exp = 1LL << 63;
62
63 // Initialize the computations.
64 uint64_t abs_d = (divisor >= 0) ? divisor : -static_cast<uint64_t>(divisor);
65 uint64_t sign_bit = static_cast<uint64_t>(divisor) >> 63;
66 uint64_t tmp = exp + sign_bit;
67 uint64_t abs_nc = tmp - 1 - (tmp % abs_d);
68 uint64_t quotient1 = exp / abs_nc;
69 uint64_t remainder1 = exp % abs_nc;
70 uint64_t quotient2 = exp / abs_d;
71 uint64_t remainder2 = exp % abs_d;
72
73 // To avoid handling both positive and negative divisor,
74 // "Hacker's Delight" introduces a method to handle these
75 // two cases together to avoid duplication.
76 uint64_t delta;
77 do {
78 p++;
79 quotient1 = 2 * quotient1;
80 remainder1 = 2 * remainder1;
81 if (remainder1 >= abs_nc) {
82 quotient1++;
83 remainder1 = remainder1 - abs_nc;
84 }
85 quotient2 = 2 * quotient2;
86 remainder2 = 2 * remainder2;
87 if (remainder2 >= abs_d) {
88 quotient2++;
89 remainder2 = remainder2 - abs_d;
90 }
91 delta = abs_d - remainder2;
92 } while (quotient1 < delta || (quotient1 == delta && remainder1 == 0));
93
94 *magic = (divisor > 0) ? (quotient2 + 1) : (-quotient2 - 1);
95 *shift = p - 64;
96}

◆ Close()

static int dart::Utils::Close ( int  fildes)
static

◆ CountLeadingZeros32()

static int dart::Utils::CountLeadingZeros32 ( uint32_t  x)
inlinestatic

Definition at line 204 of file utils.h.

204 {
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 }

◆ CountLeadingZeros64()

static int dart::Utils::CountLeadingZeros64 ( uint64_t  x)
inlinestatic

Definition at line 214 of file utils.h.

214 {
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 }
static int CountLeadingZeros32(uint32_t x)
Definition utils.h:204

◆ CountLeadingZerosWord()

static int dart::Utils::CountLeadingZerosWord ( uword  x)
inlinestatic

Definition at line 232 of file utils.h.

232 {
233#ifdef ARCH_IS_64_BIT
234 return CountLeadingZeros64(x);
235#else
236 return CountLeadingZeros32(x);
237#endif
238 }
static int CountLeadingZeros64(uint64_t x)
Definition utils.h:214

◆ CountOneBits32()

static constexpr int dart::Utils::CountOneBits32 ( uint32_t  x)
inlinestaticconstexpr

Definition at line 145 of file utils.h.

145 {
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 }

◆ CountOneBits64()

static constexpr int dart::Utils::CountOneBits64 ( uint64_t  x)
inlinestaticconstexpr

Definition at line 133 of file utils.h.

133 {
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 }

◆ CountOneBitsWord()

static constexpr int dart::Utils::CountOneBitsWord ( uword  x)
inlinestaticconstexpr

Definition at line 161 of file utils.h.

161 {
162#ifdef ARCH_IS_64_BIT
163 return CountOneBits64(x);
164#else
165 return CountOneBits32(x);
166#endif
167 }
static constexpr int CountOneBits32(uint32_t x)
Definition utils.h:145
static constexpr int CountOneBits64(uint64_t x)
Definition utils.h:133

◆ CountTrailingZeros32()

static int dart::Utils::CountTrailingZeros32 ( uint32_t  x)
inlinestatic

Definition at line 240 of file utils.h.

240 {
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 }

◆ CountTrailingZeros64()

static int dart::Utils::CountTrailingZeros64 ( uint64_t  x)
inlinestatic

Definition at line 249 of file utils.h.

249 {
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 }
static int CountTrailingZeros32(uint32_t x)
Definition utils.h:240

◆ CountTrailingZerosWord()

static int dart::Utils::CountTrailingZerosWord ( uword  x)
inlinestatic

Definition at line 266 of file utils.h.

266 {
267#ifdef ARCH_IS_64_BIT
268 return CountTrailingZeros64(x);
269#else
270 return CountTrailingZeros32(x);
271#endif
272 }
static int CountTrailingZeros64(uint64_t x)
Definition utils.h:249

◆ CreateCStringUniquePtr()

Utils::CStringUniquePtr dart::Utils::CreateCStringUniquePtr ( char *  str)
static

Definition at line 257 of file utils.cc.

257 {
258 return std::unique_ptr<char, decltype(std::free)*>{str, std::free};
259}

◆ DivideAllowZero() [1/2]

static double dart::Utils::DivideAllowZero ( double  a,
double  b 
)
inlinestatic

Definition at line 488 of file utils.h.

488 {
489 return a / b;
490 }

◆ DivideAllowZero() [2/2]

static float dart::Utils::DivideAllowZero ( float  a,
float  b 
)
inlinestatic

Definition at line 481 of file utils.h.

481 {
482 return a / b;
483 }

◆ DoublesBitEqual()

static bool dart::Utils::DoublesBitEqual ( const double  a,
const double  b 
)
inlinestatic

Definition at line 510 of file utils.h.

510 {
511 return bit_cast<int64_t, double>(a) == bit_cast<int64_t, double>(b);
512 }

◆ HexDigitToInt()

static int dart::Utils::HexDigitToInt ( char  c)
inlinestatic

Definition at line 380 of file utils.h.

380 {
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 }
static constexpr bool IsDecimalDigit(uint32_t c)
Definition utils.h:371
static bool IsHexDigit(char c)
Definition utils.h:375

◆ High16Bits()

static int32_t dart::Utils::High16Bits ( int32_t  value)
inlinestatic

Definition at line 350 of file utils.h.

350 {
351 return static_cast<int32_t>(value >> 16);
352 }

◆ High32Bits()

static int32_t dart::Utils::High32Bits ( int64_t  value)
inlinestatic

Definition at line 358 of file utils.h.

358 {
359 return static_cast<int32_t>(value >> 32);
360 }

◆ HighestBit()

static constexpr size_t dart::Utils::HighestBit ( int64_t  v)
inlinestaticconstexpr

Definition at line 170 of file utils.h.

170 {
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 }

◆ HostToBigEndian16()

uint16_t dart::Utils::HostToBigEndian16 ( uint16_t  host_value)
inlinestatic

Definition at line 16 of file utils_android.h.

16 {
17 return htobe16(value);
18}

◆ HostToBigEndian32()

uint32_t dart::Utils::HostToBigEndian32 ( uint32_t  host_value)
inlinestatic

Definition at line 20 of file utils_android.h.

20 {
21 return htobe32(value);
22}

◆ HostToBigEndian64()

uint64_t dart::Utils::HostToBigEndian64 ( uint64_t  host_value)
inlinestatic

Definition at line 24 of file utils_android.h.

24 {
25 return htobe64(value);
26}

◆ HostToLittleEndian16()

uint16_t dart::Utils::HostToLittleEndian16 ( uint16_t  host_value)
inlinestatic

Definition at line 28 of file utils_android.h.

28 {
29 return htole16(value);
30}

◆ HostToLittleEndian32()

uint32_t dart::Utils::HostToLittleEndian32 ( uint32_t  host_value)
inlinestatic

Definition at line 32 of file utils_android.h.

32 {
33 return htole32(value);
34}

◆ HostToLittleEndian64()

uint64_t dart::Utils::HostToLittleEndian64 ( uint64_t  host_value)
inlinestatic

Definition at line 36 of file utils_android.h.

36 {
37 return htole64(value);
38}

◆ IntToHexDigit()

static char dart::Utils::IntToHexDigit ( int  i)
inlinestatic

Definition at line 387 of file utils.h.

387 {
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 }

◆ IsAligned() [1/2]

template<typename T >
static constexpr bool dart::Utils::IsAligned ( T x,
uintptr_t  alignment,
uintptr_t  offset = 0 
)
inlinestaticconstexpr

Definition at line 86 of file utils.h.

88 {
89 return IsAligned(reinterpret_cast<uword>(x), alignment, offset);
90 }
static constexpr bool IsAligned(T x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:77
uintptr_t uword
Definition globals.h:501
Point offset

◆ IsAligned() [2/2]

template<typename T >
static constexpr bool dart::Utils::IsAligned ( T  x,
uintptr_t  alignment,
uintptr_t  offset = 0 
)
inlinestaticconstexpr

Definition at line 77 of file utils.h.

79 {
80 ASSERT(IsPowerOfTwo(alignment));
81 ASSERT(offset < alignment);
82 return (x & (alignment - 1)) == offset;
83 }
static constexpr bool IsPowerOfTwo(T x)
Definition utils.h:61

◆ IsAlphaNumeric()

static constexpr bool dart::Utils::IsAlphaNumeric ( uint32_t  c)
inlinestaticconstexpr

Definition at line 366 of file utils.h.

366 {
367 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
369 }

◆ IsDecimalDigit()

static constexpr bool dart::Utils::IsDecimalDigit ( uint32_t  c)
inlinestaticconstexpr

Definition at line 371 of file utils.h.

371 {
372 return ('0' <= c) && (c <= '9');
373 }

◆ IsHexDigit()

static bool dart::Utils::IsHexDigit ( char  c)
inlinestatic

Definition at line 375 of file utils.h.

375 {
376 return IsDecimalDigit(c) || (('A' <= c) && (c <= 'F')) ||
377 (('a' <= c) && (c <= 'f'));
378 }

◆ IsInt()

template<typename T >
static bool dart::Utils::IsInt ( intptr_t  N,
T  value 
)
inlinestatic

Definition at line 298 of file utils.h.

298 {
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 }
#define N
Definition beziers.cpp:19

◆ IsJavaScriptInt()

static bool dart::Utils::IsJavaScriptInt ( int64_t  value)
inlinestatic

Definition at line 527 of file utils.h.

527 {
528 return ((-0x20000000000000LL <= value) && (value <= 0x20000000000000LL));
529 }

◆ IsPowerOfTwo()

template<typename T >
static constexpr bool dart::Utils::IsPowerOfTwo ( T  x)
inlinestaticconstexpr

Definition at line 61 of file utils.h.

61 {
62 return ((x & (x - 1)) == 0) && (x != 0);
63 }

◆ IsUint()

template<typename T >
static bool dart::Utils::IsUint ( intptr_t  N,
T  value 
)
inlinestatic

Definition at line 313 of file utils.h.

313 {
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 }

◆ LittleEndianToHost64()

static uint64_t dart::Utils::LittleEndianToHost64 ( uint64_t  le_value)
inlinestatic

Definition at line 506 of file utils.h.

506 {
507 return HostToLittleEndian64(le_value);
508 }
static uint64_t HostToLittleEndian64(uint64_t host_value)

◆ LoadDynamicLibrary()

void * dart::Utils::LoadDynamicLibrary ( const char *  library_path,
char **  error = nullptr 
)
static

Definition at line 293 of file utils.cc.

293 {
294 void* handle = nullptr;
295
296#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
297 defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA)
298 handle = dlopen(library_path, RTLD_LAZY);
299#elif defined(DART_HOST_OS_WINDOWS)
300 SetLastError(0); // Clear any errors.
301
302 if (library_path == nullptr) {
303 handle = GetModuleHandle(nullptr);
304 } else {
305 // Convert to wchar_t string.
306 const int name_len = MultiByteToWideChar(
307 CP_UTF8, /*dwFlags=*/0, library_path, /*cbMultiByte=*/-1, nullptr, 0);
308 if (name_len != 0) {
309 std::unique_ptr<wchar_t[]> name(new wchar_t[name_len]);
310 const int written_len =
311 MultiByteToWideChar(CP_UTF8, /*dwFlags=*/0, library_path,
312 /*cbMultiByte=*/-1, name.get(), name_len);
313 RELEASE_ASSERT(written_len == name_len);
314 handle = LoadLibraryW(name.get());
315 }
316 }
317#endif
318
319 if (handle == nullptr) {
321 }
322
323 return handle;
324}
#define RELEASE_ASSERT(cond)
Definition assert.h:327
const uint8_t uint32_t uint32_t GError ** error
const char *const name
static void GetLastErrorAsString(char **error)
Definition utils.cc:261
WINBASEAPI VOID WINAPI SetLastError(_In_ DWORD dwErrCode)

◆ Low16Bits()

static int32_t dart::Utils::Low16Bits ( int32_t  value)
inlinestatic

Definition at line 346 of file utils.h.

346 {
347 return static_cast<int32_t>(value & 0xffff);
348 }

◆ Low32Bits()

static int32_t dart::Utils::Low32Bits ( int64_t  value)
inlinestatic

Definition at line 354 of file utils.h.

354 {
355 return static_cast<int32_t>(value);
356 }

◆ LowHighTo64Bits()

static int64_t dart::Utils::LowHighTo64Bits ( uint32_t  low,
int32_t  high 
)
inlinestatic

Definition at line 362 of file utils.h.

362 {
363 return (static_cast<uint64_t>(high) << 32) | (low & 0x0ffffffffLL);
364 }

◆ MagnitudeIsUint()

template<typename T >
static bool dart::Utils::MagnitudeIsUint ( intptr_t  N,
T  value 
)
inlinestatic

Definition at line 337 of file utils.h.

337 {
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 }
static bool IsUint(intptr_t N, T value)
Definition utils.h:313

◆ Maximum()

template<typename T >
static constexpr T dart::Utils::Maximum ( T  x,
T  y 
)
inlinestaticconstexpr

Definition at line 26 of file utils.h.

26 {
27 return x > y ? x : y;
28 }
double y

◆ Minimum()

template<typename T >
static T dart::Utils::Minimum ( T  x,
T  y 
)
inlinestatic

Definition at line 21 of file utils.h.

21 {
22 return x < y ? x : y;
23 }

◆ MulWithWrapAround()

template<typename T = int64_t>
static T dart::Utils::MulWithWrapAround ( T  a,
T  b 
)
inlinestatic

Definition at line 434 of file utils.h.

434 {
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 }

◆ NBitMask()

template<typename T = uword>
static constexpr T dart::Utils::NBitMask ( size_t  n)
inlinestaticconstexpr

Definition at line 533 of file utils.h.

533 {
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 }

◆ NegWithWrapAround()

template<typename T = int64_t>
static T dart::Utils::NegWithWrapAround ( T  a)
inlinestatic

Definition at line 441 of file utils.h.

441 {
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 }

◆ RangeCheck()

static bool dart::Utils::RangeCheck ( intptr_t  offset,
intptr_t  count,
intptr_t  length 
)
inlinestatic

Definition at line 396 of file utils.h.

398 {
399 return offset >= 0 && count >= 0 && length >= 0 &&
400 count <= (length - offset);
401 }
int count
size_t length

◆ Read()

static size_t dart::Utils::Read ( int  filedes,
void *  buf,
size_t  nbyte 
)
static

◆ ResolveSymbolInDynamicLibrary()

void * dart::Utils::ResolveSymbolInDynamicLibrary ( void *  library_handle,
const char *  symbol,
char **  error = nullptr 
)
static

Definition at line 326 of file utils.cc.

328 {
329 void* result = nullptr;
330
331#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
332 defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA)
333 dlerror(); // Clear any errors.
334 result = dlsym(library_handle, symbol);
335 // Note: nullptr might be a valid return from dlsym. Must call dlerror
336 // to differentiate.
338 return result;
339#elif defined(DART_HOST_OS_WINDOWS)
340 SetLastError(0);
341 result = reinterpret_cast<void*>(
342 GetProcAddress(reinterpret_cast<HMODULE>(library_handle), symbol));
343#endif
344
345 if (result == nullptr) {
347 }
348
349 return result;
350}
GAsyncResult * result
HINSTANCE HMODULE

◆ ReverseBits32()

uint32_t dart::Utils::ReverseBits32 ( uint32_t  x)
static

Definition at line 27 of file utils.cc.

27 {
28 x = ((x >> 16) & 0x0000ffff) | ((x & 0x0000ffff) << 16);
29 x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
30 x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
31 x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
32 x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
33 return x;
34}

◆ ReverseBits64()

uint64_t dart::Utils::ReverseBits64 ( uint64_t  x)
static

Definition at line 17 of file utils.cc.

17 {
18 x = ((x >> 32) & 0x00000000ffffffff) | (x << 32);
19 x = ((x >> 16) & 0x0000ffff0000ffff) | ((x & 0x0000ffff0000ffff) << 16);
20 x = ((x >> 8) & 0x00ff00ff00ff00ff) | ((x & 0x00ff00ff00ff00ff) << 8);
21 x = ((x >> 4) & 0x0f0f0f0f0f0f0f0f) | ((x & 0x0f0f0f0f0f0f0f0f) << 4);
22 x = ((x >> 2) & 0x3333333333333333) | ((x & 0x3333333333333333) << 2);
23 x = ((x >> 1) & 0x5555555555555555) | ((x & 0x5555555555555555) << 1);
24 return x;
25}

◆ ReverseBitsWord()

static uword dart::Utils::ReverseBitsWord ( uword  x)
inlinestatic

Definition at line 277 of file utils.h.

277 {
278#ifdef ARCH_IS_64_BIT
279 return ReverseBits64(x);
280#else
281 return ReverseBits32(x);
282#endif
283 }
static uint32_t ReverseBits32(uint32_t x)
Definition utils.cc:27
static uint64_t ReverseBits64(uint64_t x)
Definition utils.cc:17

◆ RotateLeft()

template<typename T >
static T dart::Utils::RotateLeft ( T  value,
uint8_t  rotate 
)
inlinestatic

Definition at line 459 of file utils.h.

459 {
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 }
static bool rotate(const SkDCubic &cubic, int zero, int index, SkDCubic &rotPath)
int32_t width

◆ RotateRight()

template<typename T >
static T dart::Utils::RotateRight ( T  value,
uint8_t  rotate 
)
inlinestatic

Definition at line 468 of file utils.h.

468 {
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 }

◆ RoundDown() [1/2]

template<typename T >
static T * dart::Utils::RoundDown ( T x,
intptr_t  alignment 
)
inlinestatic

Definition at line 99 of file utils.h.

99 {
100 return reinterpret_cast<T*>(
101 RoundDown(reinterpret_cast<uword>(x), alignment));
102 }
static constexpr T RoundDown(T x, intptr_t alignment)
Definition utils.h:93

◆ RoundDown() [2/2]

template<typename T >
static constexpr T dart::Utils::RoundDown ( T  x,
intptr_t  alignment 
)
inlinestaticconstexpr

Definition at line 93 of file utils.h.

93 {
94 ASSERT(IsPowerOfTwo(alignment));
95 return (x & -alignment);
96 }

◆ RoundUp() [1/2]

template<typename T >
static T * dart::Utils::RoundUp ( T x,
uintptr_t  alignment,
uintptr_t  offset = 0 
)
inlinestatic

Definition at line 113 of file utils.h.

113 {
114 return reinterpret_cast<T*>(
115 RoundUp(reinterpret_cast<uword>(x), alignment, offset));
116 }
static constexpr T RoundUp(T x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:105

◆ RoundUp() [2/2]

template<typename T >
static constexpr T dart::Utils::RoundUp ( T  x,
uintptr_t  alignment,
uintptr_t  offset = 0 
)
inlinestaticconstexpr

Definition at line 105 of file utils.h.

107 {
108 ASSERT(offset < alignment);
109 return RoundDown(x + alignment - 1 + offset, alignment) - offset;
110 }

◆ RoundUpToPowerOfTwo()

static constexpr uintptr_t dart::Utils::RoundUpToPowerOfTwo ( uintptr_t  x)
inlinestaticconstexpr

Definition at line 120 of file utils.h.

120 {
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 }

◆ SafeDoubleToInt()

template<typename T >
static T dart::Utils::SafeDoubleToInt ( double  v)
inlinestatic

Definition at line 518 of file utils.h.

518 {
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 }
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
Definition ref_ptr.h:256

◆ SCreate()

char * dart::Utils::SCreate ( const char *  format,
  ... 
)
static

Definition at line 231 of file utils.cc.

231 {
232 va_list args;
234 char* buffer = VSCreate(format, args);
235 va_end(args);
236 return buffer;
237}
static char static char * VSCreate(const char *format, va_list args)
Definition utils.cc:239
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static const uint8_t buffer[]
uint32_t uint32_t * format
va_start(args, format)
va_end(args)

◆ ShiftForPowerOfTwo()

template<typename T >
static constexpr int dart::Utils::ShiftForPowerOfTwo ( T  x)
inlinestaticconstexpr

Definition at line 66 of file utils.h.

66 {
68 int num_shifts = 0;
69 while (x > 1) {
70 num_shifts++;
71 x = x >> 1;
72 }
73 return num_shifts;
74 }

◆ ShiftLeftWithTruncation()

static int64_t dart::Utils::ShiftLeftWithTruncation ( int64_t  a,
int64_t  b 
)
inlinestatic

Definition at line 449 of file utils.h.

449 {
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 }
constexpr intptr_t kBitsPerInt64
Definition globals.h:467

◆ SNPrint()

static int dart::Utils::SNPrint ( char *  str,
size_t  size,
const char *  format,
  ... 
)
static

◆ StrDup()

static char * dart::Utils::StrDup ( const char *  s)
static

◆ StrError()

char * dart::Utils::StrError ( int  err,
char *  buffer,
size_t  bufsize 
)
inlinestatic

Definition at line 40 of file utils_android.h.

40 {
41 if (strerror_r(err, buffer, bufsize) != 0) {
42 snprintf(buffer, bufsize, "%s", "strerror_r failed");
43 }
44 return buffer;
45}

◆ StringHash()

uint32_t dart::Utils::StringHash ( const void *  data,
int  length 
)
static

Definition at line 114 of file utils.cc.

114 {
115 int size = length;
116 uint32_t hash = size;
117
118 auto cursor = reinterpret_cast<const uint8_t*>(data);
119
120 if (size >= kInt32Size) {
121 const intptr_t misalignment =
122 reinterpret_cast<intptr_t>(cursor) % kInt32Size;
123 if (misalignment > 0) {
124 // Stores 4-byte values starting from the start of the string to mimic
125 // the algorithm on aligned data.
126 uint32_t data_window = 0;
127
128 // Shift sizes for adjusting the data window when adding the next aligned
129 // piece of data.
130 const uint32_t sr = misalignment * kBitsPerByte;
131 const uint32_t sl = kBitsPerInt32 - sr;
132
133 const intptr_t pre_alignment_length = kInt32Size - misalignment;
134 switch (pre_alignment_length) {
135 case 3:
136 data_window |= cursor[2] << 16;
138 case 2:
139 data_window |= cursor[1] << 8;
141 case 1:
142 data_window |= cursor[0];
143 }
144 cursor += pre_alignment_length;
145 size -= pre_alignment_length;
146
147 // Mix four bytes at a time now that we're at an aligned spot.
148 for (; size >= kInt32Size; cursor += kInt32Size, size -= kInt32Size) {
149 uint32_t aligned_part = *reinterpret_cast<const uint32_t*>(cursor);
150 data_window |= (aligned_part << sl);
151 MIX(hash, data_window);
152 data_window = aligned_part >> sr;
153 }
154
155 if (size >= misalignment) {
156 // There's one more full window in the data. We'll let the normal tail
157 // code handle any partial window.
158 switch (misalignment) {
159 case 3:
160 data_window |= cursor[2] << (16 + sl);
162 case 2:
163 data_window |= cursor[1] << (8 + sl);
165 case 1:
166 data_window |= cursor[0] << sl;
167 }
168 MIX(hash, data_window);
169 cursor += misalignment;
170 size -= misalignment;
171 } else {
172 // This is a partial window, so just xor and multiply by M.
173 switch (size) {
174 case 2:
175 data_window |= cursor[1] << (8 + sl);
177 case 1:
178 data_window |= cursor[0] << sl;
179 }
180 hash ^= data_window;
182 cursor += size;
183 size = 0;
184 }
185 } else {
186 // Mix four bytes at a time into the hash.
187 for (; size >= kInt32Size; size -= kInt32Size, cursor += kInt32Size) {
188 uint32_t part = *reinterpret_cast<const uint32_t*>(cursor);
189 MIX(hash, part);
190 }
191 }
192 }
193
194 // Handle the last few bytes of the string if any.
195 switch (size) {
196 case 3:
197 hash ^= cursor[2] << 16;
199 case 2:
200 hash ^= cursor[1] << 8;
202 case 1:
203 hash ^= cursor[0];
205 }
206
207 // Do a few final mixes of the hash to ensure the last few bytes are
208 // well-incorporated.
209 hash ^= hash >> 13;
211 hash ^= hash >> 15;
212 return hash;
213}
static uint32_t hash(const SkShaderBase::GradientInfo &v)
constexpr intptr_t kInt32Size
Definition globals.h:450
static constexpr uint32_t kStringHashM
Definition utils.cc:101
constexpr intptr_t kBitsPerInt32
Definition globals.h:466
static int8_t data[kExtLength]
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
#define FALL_THROUGH
Definition globals.h:15
#define MIX(hash, part)
Definition utils.cc:105

◆ StrNDup()

static char * dart::Utils::StrNDup ( const char *  s,
intptr_t  n 
)
static

◆ StrNLen()

static intptr_t dart::Utils::StrNLen ( const char *  s,
intptr_t  n 
)
static

◆ StrStartsWith()

static bool dart::Utils::StrStartsWith ( const char *  s,
const char *  prefix 
)
inlinestatic

Definition at line 612 of file utils.h.

612 {
613 return strncmp(s, prefix, strlen(prefix)) == 0;
614 }
struct MyStruct s

◆ SubWithWrapAround()

template<typename T = int64_t>
static T dart::Utils::SubWithWrapAround ( T  a,
T  b 
)
inlinestatic

Definition at line 425 of file utils.h.

425 {
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 }

◆ TestBit()

template<typename T >
static constexpr bool dart::Utils::TestBit ( T  mask,
size_t  position 
)
inlinestaticconstexpr

Definition at line 549 of file utils.h.

549 {
550 ASSERT(position < sizeof(T) * kBitsPerByte);
551 return ((mask >> position) & 1) != 0;
552 }

◆ Unlink()

static int dart::Utils::Unlink ( const char *  path)
static

◆ UnloadDynamicLibrary()

void dart::Utils::UnloadDynamicLibrary ( void *  library_handle,
char **  error = nullptr 
)
static

Definition at line 352 of file utils.cc.

352 {
353 bool ok = false;
354
355#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) || \
356 defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA)
357 ok = dlclose(library_handle) == 0;
358#elif defined(DART_HOST_OS_WINDOWS)
359 SetLastError(0); // Clear any errors.
360
361 ok = FreeLibrary(reinterpret_cast<HMODULE>(library_handle));
362#endif
363
364 if (!ok) {
366 }
367}
static bool ok(int result)

◆ VSCreate()

char * dart::Utils::VSCreate ( const char *  format,
va_list  args 
)
static

Definition at line 239 of file utils.cc.

239 {
240 // Measure.
241 va_list measure_args;
242 va_copy(measure_args, args);
243 intptr_t len = VSNPrint(nullptr, 0, format, measure_args);
244 va_end(measure_args);
245
246 char* buffer = reinterpret_cast<char*>(malloc(len + 1));
247 ASSERT(buffer != nullptr);
248
249 // Print.
250 va_list print_args;
251 va_copy(print_args, args);
252 VSNPrint(buffer, len + 1, format, print_args);
253 va_end(print_args);
254 return buffer;
255}
static int static int VSNPrint(char *str, size_t size, const char *format, va_list args)
void * malloc(size_t size)
Definition allocation.cc:19

◆ VSNPrint()

static int static int dart::Utils::VSNPrint ( char *  str,
size_t  size,
const char *  format,
va_list  args 
)
static

◆ WillAddOverflow()

static bool dart::Utils::WillAddOverflow ( int64_t  a,
int64_t  b 
)
inlinestatic

Definition at line 403 of file utils.h.

403 {
404 return ((b > 0) && (a > (kMaxInt64 - b))) ||
405 ((b < 0) && (a < (kMinInt64 - b)));
406 }
constexpr int64_t kMaxInt64
Definition globals.h:486
constexpr int64_t kMinInt64
Definition globals.h:485

◆ WillSubOverflow()

static bool dart::Utils::WillSubOverflow ( int64_t  a,
int64_t  b 
)
inlinestatic

Definition at line 408 of file utils.h.

408 {
409 return ((b > 0) && (a < (kMinInt64 + b))) ||
410 ((b < 0) && (a > (kMaxInt64 + b)));
411 }

◆ WordHash()

uint32_t dart::Utils::WordHash ( intptr_t  key)
static

Definition at line 217 of file utils.cc.

217 {
218 // TODO(iposva): Need to check hash spreading.
219 // This example is from http://www.concentric.net/~Ttwang/tech/inthash.htm
220 // via. http://web.archive.org/web/20071223173210/http://www.concentric.net/~Ttwang/tech/inthash.htm
221 uword a = static_cast<uword>(key);
222 a = (a + 0x7ed55d16) + (a << 12);
223 a = (a ^ 0xc761c23c) ^ (a >> 19);
224 a = (a + 0x165667b1) + (a << 5);
225 a = (a + 0xd3a2646c) ^ (a << 9);
226 a = (a + 0xfd7046c5) + (a << 3);
227 a = (a ^ 0xb55a4f09) ^ (a >> 16);
228 return static_cast<uint32_t>(a);
229}

The documentation for this class was generated from the following files: