blob: 29e75433b9e9ce2bba5058ea81d0b176bf3f7da4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
bungemanf20488b2015-07-29 11:49:40 -07007
Mike Reed7edde232018-01-04 13:52:07 -05008#include "SkMalloc.h"
bungemanf20488b2015-07-29 11:49:40 -07009
Herb Derbyd7b34a52017-03-20 11:19:23 -040010#include <cstdlib>
reed@android.com7a561082009-05-04 19:24:56 +000011
halcanary61b14362014-07-10 08:59:38 -070012#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);
Kevin Lubick2416f962018-02-12 08:26:39 -050018#if defined(IS_FUZZING_WITH_AFL)
Kevin Lubickedee1ae2017-02-20 17:47:18 -050019 exit(1);
20#else
halcanary61b14362014-07-10 08:59:38 -070021 abort();
Kevin Lubickedee1ae2017-02-20 17:47:18 -050022#endif
halcanary61b14362014-07-10 08:59:38 -070023}
24
commit-bot@chromium.orgdd6cde52013-10-07 17:59:04 +000025static inline void* throw_on_failure(size_t size, void* p) {
halcanary96fcdcc2015-08-27 07:41:13 -070026 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.
halcanary61b14362014-07-10 08:59:38 -070028 sk_out_of_memory(size);
mtklein@google.com519f9672013-09-20 14:31:45 +000029 }
30 return p;
31}
32
djsollenf2b340f2016-01-29 08:51:04 -080033void sk_abort_no_print() {
bsalomonad514d02016-03-02 14:44:15 -080034#if defined(SK_BUILD_FOR_WIN) && defined(SK_IS_BOT)
djsollen6b489842016-02-04 08:59:35 -080035 // do not display a system dialog before aborting the process
36 _set_abort_behavior(0, _WRITE_ABORT_MSG);
37#endif
bungeman1f790aa2016-07-20 09:49:10 -070038#if defined(SK_DEBUG) && defined(SK_BUILD_FOR_WIN)
39 __debugbreak();
40#else
reed@android.com7a561082009-05-04 19:24:56 +000041 abort();
bungeman1f790aa2016-07-20 09:49:10 -070042#endif
reed@android.com7a561082009-05-04 19:24:56 +000043}
44
45void sk_out_of_memory(void) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000046 SkDEBUGFAIL("sk_out_of_memory");
Kevin Lubick54f20e02018-01-11 14:50:21 -050047#if defined(IS_FUZZING_WITH_AFL)
Kevin Lubickedee1ae2017-02-20 17:47:18 -050048 exit(1);
49#else
reed@android.com7a561082009-05-04 19:24:56 +000050 abort();
Kevin Lubickedee1ae2017-02-20 17:47:18 -050051#endif
reed@android.com7a561082009-05-04 19:24:56 +000052}
53
reed@android.com7a561082009-05-04 19:24:56 +000054void* sk_realloc_throw(void* addr, size_t size) {
commit-bot@chromium.orgdd6cde52013-10-07 17:59:04 +000055 return throw_on_failure(size, realloc(addr, size));
reed@android.com7a561082009-05-04 19:24:56 +000056}
57
58void sk_free(void* p) {
59 if (p) {
60 free(p);
61 }
62}
63
64void* sk_malloc_flags(size_t size, unsigned flags) {
Mike Reed8dc8dbc2018-01-05 11:20:10 -050065 void* p;
Mike Reed8dc8dbc2018-01-05 11:20:10 -050066 if (flags & SK_MALLOC_ZERO_INITIALIZE) {
67 p = calloc(size, 1);
68 } else {
69 p = malloc(size);
70 }
mtklein@google.com519f9672013-09-20 14:31:45 +000071 if (flags & SK_MALLOC_THROW) {
commit-bot@chromium.orgdd6cde52013-10-07 17:59:04 +000072 return throw_on_failure(size, p);
mtklein@google.com519f9672013-09-20 14:31:45 +000073 } else {
74 return p;
reed@android.com7a561082009-05-04 19:24:56 +000075 }
mtklein@google.com519f9672013-09-20 14:31:45 +000076}