Flutter Engine
The Flutter Engine
SkMalloc.h
Go to the documentation of this file.
1/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkMalloc_DEFINED
9#define SkMalloc_DEFINED
10
11#include <cstring>
12
14
15/*
16 memory wrappers to be implemented by the porting layer (platform)
17*/
18
19
20/** Free memory returned by sk_malloc(). It is safe to pass null. */
21SK_API extern void sk_free(void*);
22
23/**
24 * Called internally if we run out of memory. The platform implementation must
25 * not return, but should either throw an exception or otherwise exit.
26 */
27SK_API extern void sk_out_of_memory(void);
28
29enum {
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 */
41};
42/**
43 * Return a block of memory (at least 4-byte aligned) of at least the specified size.
44 * If the requested memory cannot be returned, either return nullptr or throw/exit, depending
45 * on the SK_MALLOC_THROW bit. If the allocation succeeds, the memory will be zero-initialized
46 * if the SK_MALLOC_ZERO_INITIALIZE bit was set.
47 *
48 * To free the memory, call sk_free()
49 */
50SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
51
52/** Same as standard realloc(), but this one never returns null on failure. It will throw
53 * if it fails.
54 * If size is 0, it will call sk_free on buffer and return null. (This behavior is implementation-
55 * defined for normal realloc. We follow what glibc does.)
56 */
57SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
58
59/**
60 * Return the size of the block of memory allocated in reality for a given pointer. The pointer
61 * passed must have been allocated using the sk_malloc_* or sk_realloc_* functions. The "size"
62 * parameter indicates the size originally requested when the memory block was allocated, and
63 * the value returned by this function must be bigger or equal to it.
64 */
65SK_API extern size_t sk_malloc_size(void* addr, size_t size);
66
67static inline void* sk_malloc_throw(size_t size) {
69}
70
71static inline void* sk_calloc_throw(size_t size) {
73}
74
75static inline void* sk_calloc_canfail(size_t size) {
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}
84
85// Performs a safe multiply count * elemSize, checking for overflow
86SK_API extern void* sk_calloc_throw(size_t count, size_t elemSize);
87SK_API extern void* sk_malloc_throw(size_t count, size_t elemSize);
88SK_API extern void* sk_realloc_throw(void* buffer, size_t count, size_t elemSize);
89
90/**
91 * These variants return nullptr on failure
92 */
93static inline void* sk_malloc_canfail(size_t size) {
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}
102SK_API extern void* sk_malloc_canfail(size_t count, size_t elemSize);
103
104// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
105static inline void sk_bzero(void* buffer, size_t size) {
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}
111
112/**
113 * sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
114 *
115 * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
116 * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
117 * memcpy(dst, src, 0);
118 * if (src) {
119 * printf("%x\n", *src);
120 * }
121 * In this code the compiler can assume src is not null and omit the if (src) {...} check,
122 * unconditionally running the printf, crashing the program if src really is null.
123 * Of the compilers we pay attention to only GCC performs this optimization in practice.
124 */
125static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
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}
133
134static inline void* sk_careful_memmove(void* dst, const void* src, size_t len) {
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}
142
143static inline int sk_careful_memcmp(const void* a, const void* b, size_t len) {
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}
151
152#endif // SkMalloc_DEFINED
int count
Definition: FontMgrTest.cpp:50
#define SK_API
Definition: SkAPI.h:35
SK_API void sk_out_of_memory(void)
static void * sk_careful_memmove(void *dst, const void *src, size_t len)
Definition: SkMalloc.h:134
SK_API void * sk_malloc_flags(size_t size, unsigned flags)
static void * sk_calloc_throw(size_t size)
Definition: SkMalloc.h:71
@ SK_MALLOC_ZERO_INITIALIZE
Definition: SkMalloc.h:34
@ SK_MALLOC_THROW
Definition: SkMalloc.h:40
static void * sk_calloc_canfail(size_t size)
Definition: SkMalloc.h:75
static void sk_bzero(void *buffer, size_t size)
Definition: SkMalloc.h:105
SK_API void sk_free(void *)
static void * sk_careful_memcpy(void *dst, const void *src, size_t len)
Definition: SkMalloc.h:125
static void * sk_malloc_canfail(size_t size)
Definition: SkMalloc.h:93
SK_API size_t sk_malloc_size(void *addr, size_t size)
static void * sk_malloc_throw(size_t size)
Definition: SkMalloc.h:67
SK_API void * sk_realloc_throw(void *buffer, size_t size)
static int sk_careful_memcmp(const void *a, const void *b, size_t len)
Definition: SkMalloc.h:143
static bool b
struct MyStruct a[10]
FlutterSemanticsFlag flags
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
dst
Definition: cp.py:12