Flutter Engine
The Flutter Engine
Enumerations | Functions
SkMalloc.h File Reference
#include <cstring>
#include "include/private/base/SkAPI.h"

Go to the source code of this file.

Enumerations

enum  { SK_MALLOC_ZERO_INITIALIZE = 1 << 0 , SK_MALLOC_THROW = 1 << 1 }
 

Functions

SK_API void sk_free (void *)
 
SK_API void sk_out_of_memory (void)
 
SK_API void * sk_malloc_flags (size_t size, unsigned flags)
 
SK_API void * sk_realloc_throw (void *buffer, size_t size)
 
SK_API size_t sk_malloc_size (void *addr, size_t size)
 
static void * sk_malloc_throw (size_t size)
 
static void * sk_calloc_throw (size_t size)
 
static void * sk_calloc_canfail (size_t size)
 
SK_API void * sk_calloc_throw (size_t count, size_t elemSize)
 
SK_API void * sk_malloc_throw (size_t count, size_t elemSize)
 
SK_API void * sk_realloc_throw (void *buffer, size_t count, size_t elemSize)
 
static void * sk_malloc_canfail (size_t size)
 
SK_API void * sk_malloc_canfail (size_t count, size_t elemSize)
 
static void sk_bzero (void *buffer, size_t size)
 
static void * sk_careful_memcpy (void *dst, const void *src, size_t len)
 
static void * sk_careful_memmove (void *dst, const void *src, size_t len)
 
static int sk_careful_memcmp (const void *a, const void *b, size_t len)
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
SK_MALLOC_ZERO_INITIALIZE 

If this bit is set, the returned buffer must be zero-initialized. If this bit is not set the buffer can be uninitialized.

SK_MALLOC_THROW 

If this bit is set, the implementation must throw/crash/quit if the request cannot be fulfilled. If this bit is not set, then it should return nullptr on failure.

Definition at line 29 of file SkMalloc.h.

29 {
30 /**
31 * If this bit is set, the returned buffer must be zero-initialized. If this bit is not set
32 * the buffer can be uninitialized.
33 */
35
36 /**
37 * If this bit is set, the implementation must throw/crash/quit if the request cannot
38 * be fulfilled. If this bit is not set, then it should return nullptr on failure.
39 */
40 SK_MALLOC_THROW = 1 << 1,
41};
@ SK_MALLOC_ZERO_INITIALIZE
Definition: SkMalloc.h:34
@ SK_MALLOC_THROW
Definition: SkMalloc.h:40

Function Documentation

◆ sk_bzero()

static void sk_bzero ( void *  buffer,
size_t  size 
)
inlinestatic

Definition at line 105 of file SkMalloc.h.

105 {
106 // Please c.f. sk_careful_memcpy. It's undefined behavior to call memset(null, 0, 0).
107 if (size) {
108 memset(buffer, 0, size);
109 }
110}
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

◆ sk_calloc_canfail()

static void * sk_calloc_canfail ( size_t  size)
inlinestatic

Definition at line 75 of file SkMalloc.h.

75 {
76#if defined(SK_BUILD_FOR_FUZZER)
77 // To reduce the chance of OOM, pretend we can't allocate more than 200kb.
78 if (size > 200000) {
79 return nullptr;
80 }
81#endif
83}
SK_API void * sk_malloc_flags(size_t size, unsigned flags)

◆ sk_calloc_throw() [1/2]

SK_API void * sk_calloc_throw ( size_t  count,
size_t  elemSize 
)

Definition at line 8 of file SkMalloc.cpp.

8 {
9 return sk_calloc_throw(SkSafeMath::Mul(count, elemSize));
10}
int count
Definition: FontMgrTest.cpp:50
void * sk_calloc_throw(size_t count, size_t elemSize)
Definition: SkMalloc.cpp:8
static size_t Mul(size_t x, size_t y)
Definition: SkSafeMath.cpp:16

◆ sk_calloc_throw() [2/2]

static void * sk_calloc_throw ( size_t  size)
inlinestatic

Definition at line 71 of file SkMalloc.h.

◆ sk_careful_memcmp()

static int sk_careful_memcmp ( const void *  a,
const void *  b,
size_t  len 
)
inlinestatic

Definition at line 143 of file SkMalloc.h.

143 {
144 // When we pass >0 len we had better already be passing valid pointers.
145 // So we just need to skip calling memcmp when len == 0.
146 if (len == 0) {
147 return 0; // we treat zero-length buffers as "equal"
148 }
149 return memcmp(a, b, len);
150}
static bool b
struct MyStruct a[10]

◆ sk_careful_memcpy()

static void * sk_careful_memcpy ( void *  dst,
const void *  src,
size_t  len 
)
inlinestatic

sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.

It is undefined behavior to call memcpy() with null dst or src, even if len is 0. If an optimizer is "smart" enough, it can exploit this to do unexpected things. memcpy(dst, src, 0); if (src) { printf("%x\n", *src); } In this code the compiler can assume src is not null and omit the if (src) {...} check, unconditionally running the printf, crashing the program if src really is null. Of the compilers we pay attention to only GCC performs this optimization in practice.

Definition at line 125 of file SkMalloc.h.

125 {
126 // When we pass >0 len we had better already be passing valid pointers.
127 // So we just need to skip calling memcpy when len == 0.
128 if (len) {
129 memcpy(dst,src,len);
130 }
131 return dst;
132}
dst
Definition: cp.py:12

◆ sk_careful_memmove()

static void * sk_careful_memmove ( void *  dst,
const void *  src,
size_t  len 
)
inlinestatic

Definition at line 134 of file SkMalloc.h.

134 {
135 // When we pass >0 len we had better already be passing valid pointers.
136 // So we just need to skip calling memcpy when len == 0.
137 if (len) {
138 memmove(dst,src,len);
139 }
140 return dst;
141}

◆ sk_free()

SK_API void sk_free ( void *  p)

Free memory returned by sk_malloc(). It is safe to pass null.

Definition at line 83 of file SkMemory_malloc.cpp.

83 {
84 // The guard here produces a performance improvement across many tests, and many platforms.
85 // Removing the check was tried in skia cl 588037.
86 if (p != nullptr) {
87 free(p);
88 }
89}

◆ sk_malloc_canfail() [1/2]

SK_API void * sk_malloc_canfail ( size_t  count,
size_t  elemSize 
)

Definition at line 20 of file SkMalloc.cpp.

20 {
21 return sk_malloc_canfail(SkSafeMath::Mul(count, elemSize));
22}
void * sk_malloc_canfail(size_t count, size_t elemSize)
Definition: SkMalloc.cpp:20

◆ sk_malloc_canfail() [2/2]

static void * sk_malloc_canfail ( size_t  size)
inlinestatic

These variants return nullptr on failure

Definition at line 93 of file SkMalloc.h.

93 {
94#if defined(SK_BUILD_FOR_FUZZER)
95 // To reduce the chance of OOM, pretend we can't allocate more than 200kb.
96 if (size > 200000) {
97 return nullptr;
98 }
99#endif
100 return sk_malloc_flags(size, 0);
101}

◆ sk_malloc_flags()

SK_API void * sk_malloc_flags ( size_t  size,
unsigned  flags 
)

Return a block of memory (at least 4-byte aligned) of at least the specified size. If the requested memory cannot be returned, either return nullptr or throw/exit, depending on the SK_MALLOC_THROW bit. If the allocation succeeds, the memory will be zero-initialized if the SK_MALLOC_ZERO_INITIALIZE bit was set.

To free the memory, call sk_free()

Definition at line 91 of file SkMemory_malloc.cpp.

91 {
92 void* p;
94 p = calloc(size, 1);
95 } else {
96#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(__BIONIC__)
97 /* TODO: After b/169449588 is fixed, we will want to change this to restore
98 * original behavior instead of always disabling the flag.
99 * TODO: After b/158870657 is fixed and scudo is used globally, we can assert when an
100 * an error is returned.
101 */
102 // malloc() generally doesn't initialize its memory and that's a huge security hole,
103 // so Android has replaced its malloc() with one that zeros memory,
104 // but that's a huge performance hit for HWUI, so turn it back off again.
105 (void)mallopt(M_THREAD_DISABLE_MEM_INIT, 1);
106#endif
107 p = malloc(size);
108#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && defined(__BIONIC__)
109 (void)mallopt(M_THREAD_DISABLE_MEM_INIT, 0);
110#endif
111 }
112 if (flags & SK_MALLOC_THROW) {
113 return throw_on_failure(size, p);
114 } else {
115 return p;
116 }
117}
static void * throw_on_failure(size_t size, void *p)
FlutterSemanticsFlag flags
void * malloc(size_t size)
Definition: allocation.cc:19
void * calloc(size_t n, size_t size)
Definition: allocation.cc:11

◆ sk_malloc_size()

SK_API size_t sk_malloc_size ( void *  addr,
size_t  size 
)

Return the size of the block of memory allocated in reality for a given pointer. The pointer passed must have been allocated using the sk_malloc_* or sk_realloc_* functions. The "size" parameter indicates the size originally requested when the memory block was allocated, and the value returned by this function must be bigger or equal to it.

Definition at line 119 of file SkMemory_malloc.cpp.

119 {
120 size_t completeSize = size;
121
122 // Use the OS specific calls to find the actual capacity.
123 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
124 // TODO: remove the max, when the chrome implementation of malloc_size doesn't return 0.
125 completeSize = std::max(malloc_size(addr), size);
126 #elif defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 17
127 completeSize = malloc_usable_size(addr);
128 SkASSERT(completeSize >= size);
129 #elif defined(SK_BUILD_FOR_UNIX)
130 completeSize = malloc_usable_size(addr);
131 SkASSERT(completeSize >= size);
132 #elif defined(SK_BUILD_FOR_WIN)
133 completeSize = _msize(addr);
134 SkASSERT(completeSize >= size);
135 #endif
136
137 return completeSize;
138}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static float max(float r, float g, float b)
Definition: hsl.cpp:49

◆ sk_malloc_throw() [1/2]

SK_API void * sk_malloc_throw ( size_t  count,
size_t  elemSize 
)

Definition at line 12 of file SkMalloc.cpp.

12 {
13 return sk_malloc_throw(SkSafeMath::Mul(count, elemSize));
14}
void * sk_malloc_throw(size_t count, size_t elemSize)
Definition: SkMalloc.cpp:12

◆ sk_malloc_throw() [2/2]

static void * sk_malloc_throw ( size_t  size)
inlinestatic

Definition at line 67 of file SkMalloc.h.

67 {
69}

◆ sk_out_of_memory()

SK_API void sk_out_of_memory ( void  )

Called internally if we run out of memory. The platform implementation must not return, but should either throw an exception or otherwise exit.

Definition at line 66 of file SkMemory_malloc.cpp.

66 {
67 SkDEBUGFAIL("sk_out_of_memory");
68#if defined(SK_BUILD_FOR_AFL_FUZZ)
69 exit(1);
70#else
71 abort();
72#endif
73}
#define SkDEBUGFAIL(message)
Definition: SkAssert.h:118
exit(kErrorExitCode)

◆ sk_realloc_throw() [1/2]

SK_API void * sk_realloc_throw ( void *  buffer,
size_t  count,
size_t  elemSize 
)

Definition at line 16 of file SkMalloc.cpp.

16 {
17 return sk_realloc_throw(buffer, SkSafeMath::Mul(count, elemSize));
18}
void * sk_realloc_throw(void *buffer, size_t count, size_t elemSize)
Definition: SkMalloc.cpp:16

◆ sk_realloc_throw() [2/2]

SK_API void * sk_realloc_throw ( void *  buffer,
size_t  size 
)

Same as standard realloc(), but this one never returns null on failure. It will throw if it fails. If size is 0, it will call sk_free on buffer and return null. (This behavior is implementation- defined for normal realloc. We follow what glibc does.)

Definition at line 75 of file SkMemory_malloc.cpp.

75 {
76 if (size == 0) {
78 return nullptr;
79 }
81}
void sk_free(void *p)
void * realloc(void *ptr, size_t size)
Definition: allocation.cc:27