Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | List of all members
SkSafeMath Class Reference

#include <SkSafeMath.h>

Public Member Functions

 SkSafeMath ()=default
 
bool ok () const
 
 operator bool () const
 
size_t mul (size_t x, size_t y)
 
size_t add (size_t x, size_t y)
 
int addInt (int a, int b)
 
size_t alignUp (size_t x, size_t alignment)
 
template<typename T >
T castTo (size_t value)
 

Static Public Member Functions

static size_t Add (size_t x, size_t y)
 
static size_t Mul (size_t x, size_t y)
 
static size_t Align4 (size_t x)
 

Detailed Description

Definition at line 22 of file SkSafeMath.h.

Constructor & Destructor Documentation

◆ SkSafeMath()

SkSafeMath::SkSafeMath ( )
default

Member Function Documentation

◆ add()

size_t SkSafeMath::add ( size_t  x,
size_t  y 
)
inline

Definition at line 33 of file SkSafeMath.h.

33 {
34 size_t result = x + y;
35 fOK &= result >= x;
36 return result;
37 }
GAsyncResult * result
double y
double x

◆ Add()

size_t SkSafeMath::Add ( size_t  x,
size_t  y 
)
static

Definition at line 10 of file SkSafeMath.cpp.

10 {
11 SkSafeMath tmp;
12 size_t sum = tmp.add(x, y);
13 return tmp.ok() ? sum : SIZE_MAX;
14}
size_t add(size_t x, size_t y)
Definition: SkSafeMath.h:33
bool ok() const
Definition: SkSafeMath.h:26

◆ addInt()

int SkSafeMath::addInt ( int  a,
int  b 
)
inline

Return a + b, unless this result is an overflow/underflow. In those cases, fOK will be set to false, and it is undefined what this returns.

Definition at line 43 of file SkSafeMath.h.

43 {
44 if (b < 0 && a < std::numeric_limits<int>::min() - b) {
45 fOK = false;
46 return a;
47 } else if (b > 0 && a > std::numeric_limits<int>::max() - b) {
48 fOK = false;
49 return a;
50 }
51 return a + b;
52 }
static bool b
struct MyStruct a[10]
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

◆ Align4()

static size_t SkSafeMath::Align4 ( size_t  x)
inlinestatic

Definition at line 69 of file SkSafeMath.h.

69 {
70 SkSafeMath safe;
71 return safe.alignUp(x, 4);
72 }
size_t alignUp(size_t x, size_t alignment)
Definition: SkSafeMath.h:54

◆ alignUp()

size_t SkSafeMath::alignUp ( size_t  x,
size_t  alignment 
)
inline

Definition at line 54 of file SkSafeMath.h.

54 {
55 SkASSERT(alignment && !(alignment & (alignment - 1)));
56 return add(x, alignment - 1) & ~(alignment - 1);
57 }
#define SkASSERT(cond)
Definition: SkAssert.h:116

◆ castTo()

template<typename T >
T SkSafeMath::castTo ( size_t  value)
inline

Definition at line 59 of file SkSafeMath.h.

59 {
60 if (!SkTFitsIn<T>(value)) {
61 fOK = false;
62 }
63 return static_cast<T>(value);
64 }
uint8_t value
#define T
Definition: precompiler.cc:65

◆ mul()

size_t SkSafeMath::mul ( size_t  x,
size_t  y 
)
inline

Definition at line 29 of file SkSafeMath.h.

29 {
30 return sizeof(size_t) == sizeof(uint64_t) ? mul64(x, y) : mul32(x, y);
31 }

◆ Mul()

size_t SkSafeMath::Mul ( size_t  x,
size_t  y 
)
static

Definition at line 16 of file SkSafeMath.cpp.

16 {
17 SkSafeMath tmp;
18 size_t prod = tmp.mul(x, y);
19 return tmp.ok() ? prod : SIZE_MAX;
20}
size_t mul(size_t x, size_t y)
Definition: SkSafeMath.h:29

◆ ok()

bool SkSafeMath::ok ( ) const
inline

Definition at line 26 of file SkSafeMath.h.

26{ return fOK; }

◆ operator bool()

SkSafeMath::operator bool ( ) const
inlineexplicit

Definition at line 27 of file SkSafeMath.h.

27{ return fOK; }

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