Flutter Engine
The Flutter Engine
SkMemory_malloc.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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
12
13#include <algorithm>
14#include <cstdlib>
15
16#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
17#include <malloc/malloc.h>
18#elif defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_UNIX)
19#include <malloc.h>
20#elif defined(SK_BUILD_FOR_WIN)
21#include <malloc.h>
22#endif
23
24#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_WIN)
25#include <intrin.h>
26// This is a super stable value and setting it here avoids pulling in all of windows.h.
27#ifndef FAST_FAIL_FATAL_APP_EXIT
28#define FAST_FAIL_FATAL_APP_EXIT 7
29#endif
30#endif
31
32#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
33 #define SK_DEBUGFAILF(fmt, ...) SK_ABORT(fmt"\n", __VA_ARGS__)
34#else
35 #define SK_DEBUGFAILF(fmt, ...) SkASSERT((SkDebugf(fmt"\n", __VA_ARGS__), false))
36#endif
37
38static inline void sk_out_of_memory(size_t size) {
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}
47
48static inline void* throw_on_failure(size_t size, void* p) {
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}
55
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}
65
66void sk_out_of_memory(void) {
67 SkDEBUGFAIL("sk_out_of_memory");
68#if defined(SK_BUILD_FOR_AFL_FUZZ)
69 exit(1);
70#else
71 abort();
72#endif
73}
74
75void* sk_realloc_throw(void* addr, size_t size) {
76 if (size == 0) {
78 return nullptr;
79 }
81}
82
83void sk_free(void* p) {
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}
90
91void* sk_malloc_flags(size_t size, unsigned flags) {
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}
118
119size_t sk_malloc_size(void* addr, size_t size) {
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 SkDEBUGFAIL(message)
Definition: SkAssert.h:118
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ SK_MALLOC_ZERO_INITIALIZE
Definition: SkMalloc.h:34
@ SK_MALLOC_THROW
Definition: SkMalloc.h:40
void * sk_malloc_flags(size_t size, unsigned flags)
void sk_free(void *p)
size_t sk_malloc_size(void *addr, size_t size)
static void * throw_on_failure(size_t size, void *p)
void * sk_realloc_throw(void *addr, size_t size)
void sk_abort_no_print()
#define SK_DEBUGFAILF(fmt,...)
static void sk_out_of_memory(size_t size)
FlutterSemanticsFlag flags
static float max(float r, float g, float b)
Definition: hsl.cpp:49
exit(kErrorExitCode)
void * malloc(size_t size)
Definition: allocation.cc:19
void * calloc(size_t n, size_t size)
Definition: allocation.cc:11
void * realloc(void *ptr, size_t size)
Definition: allocation.cc:27
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