Flutter Engine
The Flutter Engine
Macros | Functions
SkMemory_malloc.cpp File Reference
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkDebug.h"
#include "include/private/base/SkFeatures.h"
#include "include/private/base/SkMalloc.h"
#include <algorithm>
#include <cstdlib>

Go to the source code of this file.

Macros

#define SK_DEBUGFAILF(fmt, ...)   SkASSERT((SkDebugf(fmt"\n", __VA_ARGS__), false))
 

Functions

static void sk_out_of_memory (size_t size)
 
static void * throw_on_failure (size_t size, void *p)
 
void sk_abort_no_print ()
 
void sk_out_of_memory (void)
 
void * sk_realloc_throw (void *addr, size_t size)
 
void sk_free (void *p)
 
void * sk_malloc_flags (size_t size, unsigned flags)
 
size_t sk_malloc_size (void *addr, size_t size)
 

Macro Definition Documentation

◆ SK_DEBUGFAILF

#define SK_DEBUGFAILF (   fmt,
  ... 
)    SkASSERT((SkDebugf(fmt"\n", __VA_ARGS__), false))

Definition at line 35 of file SkMemory_malloc.cpp.

Function Documentation

◆ sk_abort_no_print()

void sk_abort_no_print ( void  )

Called internally if we hit an unrecoverable error. The platform implementation must not return, but should either throw an exception or otherwise exit.

Definition at line 56 of file SkMemory_malloc.cpp.

56 {
57#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_WIN)
58 __fastfail(FAST_FAIL_FATAL_APP_EXIT);
59#elif defined(__clang__)
60 __builtin_trap();
61#else
62 abort();
63#endif
64}

◆ sk_free()

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_flags()

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}
@ SK_MALLOC_ZERO_INITIALIZE
Definition: SkMalloc.h:34
@ SK_MALLOC_THROW
Definition: SkMalloc.h:40
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
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_malloc_size()

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_out_of_memory() [1/2]

static void sk_out_of_memory ( size_t  size)
inlinestatic

Definition at line 38 of file SkMemory_malloc.cpp.

38 {
39 SK_DEBUGFAILF("sk_out_of_memory (asked for %zu bytes)",
40 size);
41#if defined(SK_BUILD_FOR_AFL_FUZZ)
42 exit(1);
43#else
44 abort();
45#endif
46}
#define SK_DEBUGFAILF(fmt,...)
exit(kErrorExitCode)

◆ sk_out_of_memory() [2/2]

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

◆ sk_realloc_throw()

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

◆ throw_on_failure()

static void * throw_on_failure ( size_t  size,
void *  p 
)
inlinestatic

Definition at line 48 of file SkMemory_malloc.cpp.

48 {
49 if (size > 0 && p == nullptr) {
50 // If we've got a nullptr here, the only reason we should have failed is running out of RAM.
52 }
53 return p;
54}
static void sk_out_of_memory(size_t size)