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 8#include "SkMalloc.h" 9 10#include <cstdlib> 11 12#define SK_DEBUGFAILF(fmt, ...) \ 13 SkASSERT((SkDebugf(fmt"\n", __VA_ARGS__), false)) 14 15static inline void sk_out_of_memory(size_t size) { 16 SK_DEBUGFAILF("sk_out_of_memory (asked for " SK_SIZE_T_SPECIFIER " bytes)", 17 size); 18#if defined(IS_FUZZING) 19 exit(1); 20#else 21 abort(); 22#endif 23} 24 25static inline void* throw_on_failure(size_t size, void* p) { 26 if (size > 0 && p == nullptr) { 27 // If we've got a nullptr here, the only reason we should have failed is running out of RAM. 28 sk_out_of_memory(size); 29 } 30 return p; 31} 32 33void sk_abort_no_print() { 34#if defined(SK_BUILD_FOR_WIN) && defined(SK_IS_BOT) 35 // do not display a system dialog before aborting the process 36 _set_abort_behavior(0, _WRITE_ABORT_MSG); 37#endif 38#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_WIN) 39 __debugbreak(); 40#endif 41#if defined(IS_FUZZING) 42 exit(1); 43#else 44 abort(); 45#endif 46} 47 48void sk_out_of_memory(void) { 49 SkDEBUGFAIL("sk_out_of_memory"); 50#if defined(IS_FUZZING_WITH_AFL) 51 exit(1); 52#else 53 abort(); 54#endif 55} 56 57void* sk_realloc_throw(void* addr, size_t size) { 58 return throw_on_failure(size, realloc(addr, size)); 59} 60 61void sk_free(void* p) { 62 if (p) { 63 free(p); 64 } 65} 66 67void* sk_malloc_flags(size_t size, unsigned flags) { 68 void* p; 69 if (flags & SK_MALLOC_ZERO_INITIALIZE) { 70 p = calloc(size, 1); 71 } else { 72 p = malloc(size); 73 } 74 if (flags & SK_MALLOC_THROW) { 75 return throw_on_failure(size, p); 76 } else { 77 return p; 78 } 79} 80