epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 7 | |
Mike Reed | 7edde23 | 2018-01-04 13:52:07 -0500 | [diff] [blame] | 8 | #include "SkMalloc.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 9 | |
Herb Derby | d7b34a5 | 2017-03-20 11:19:23 -0400 | [diff] [blame] | 10 | #include <cstdlib> |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 11 | |
halcanary | 61b1436 | 2014-07-10 08:59:38 -0700 | [diff] [blame] | 12 | #define SK_DEBUGFAILF(fmt, ...) \ |
| 13 | SkASSERT((SkDebugf(fmt"\n", __VA_ARGS__), false)) |
| 14 | |
| 15 | static 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 Lubick | 2416f96 | 2018-02-12 08:26:39 -0500 | [diff] [blame] | 18 | #if defined(IS_FUZZING_WITH_AFL) |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 19 | exit(1); |
| 20 | #else |
halcanary | 61b1436 | 2014-07-10 08:59:38 -0700 | [diff] [blame] | 21 | abort(); |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 22 | #endif |
halcanary | 61b1436 | 2014-07-10 08:59:38 -0700 | [diff] [blame] | 23 | } |
| 24 | |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 25 | static inline void* throw_on_failure(size_t size, void* p) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 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. |
halcanary | 61b1436 | 2014-07-10 08:59:38 -0700 | [diff] [blame] | 28 | sk_out_of_memory(size); |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 29 | } |
| 30 | return p; |
| 31 | } |
| 32 | |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 33 | void sk_abort_no_print() { |
bsalomon | ad514d0 | 2016-03-02 14:44:15 -0800 | [diff] [blame] | 34 | #if defined(SK_BUILD_FOR_WIN) && defined(SK_IS_BOT) |
djsollen | 6b48984 | 2016-02-04 08:59:35 -0800 | [diff] [blame] | 35 | // do not display a system dialog before aborting the process |
| 36 | _set_abort_behavior(0, _WRITE_ABORT_MSG); |
| 37 | #endif |
bungeman | 1f790aa | 2016-07-20 09:49:10 -0700 | [diff] [blame] | 38 | #if defined(SK_DEBUG) && defined(SK_BUILD_FOR_WIN) |
| 39 | __debugbreak(); |
| 40 | #else |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 41 | abort(); |
bungeman | 1f790aa | 2016-07-20 09:49:10 -0700 | [diff] [blame] | 42 | #endif |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void sk_out_of_memory(void) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 46 | SkDEBUGFAIL("sk_out_of_memory"); |
Kevin Lubick | 54f20e0 | 2018-01-11 14:50:21 -0500 | [diff] [blame] | 47 | #if defined(IS_FUZZING_WITH_AFL) |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 48 | exit(1); |
| 49 | #else |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 50 | abort(); |
Kevin Lubick | edee1ae | 2017-02-20 17:47:18 -0500 | [diff] [blame] | 51 | #endif |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 52 | } |
| 53 | |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 54 | void* sk_realloc_throw(void* addr, size_t size) { |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 55 | return throw_on_failure(size, realloc(addr, size)); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void sk_free(void* p) { |
| 59 | if (p) { |
| 60 | free(p); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void* sk_malloc_flags(size_t size, unsigned flags) { |
Mike Reed | 8dc8dbc | 2018-01-05 11:20:10 -0500 | [diff] [blame] | 65 | void* p; |
Mike Reed | 8dc8dbc | 2018-01-05 11:20:10 -0500 | [diff] [blame] | 66 | if (flags & SK_MALLOC_ZERO_INITIALIZE) { |
| 67 | p = calloc(size, 1); |
| 68 | } else { |
| 69 | p = malloc(size); |
| 70 | } |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 71 | if (flags & SK_MALLOC_THROW) { |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 72 | return throw_on_failure(size, p); |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 73 | } else { |
| 74 | return p; |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 75 | } |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 76 | } |