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 | |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 8 | #include "SkTypes.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 9 | |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 10 | #include <stdlib.h> |
| 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); |
| 18 | abort(); |
| 19 | } |
| 20 | |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 21 | static inline void* throw_on_failure(size_t size, void* p) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 22 | if (size > 0 && p == nullptr) { |
| 23 | // 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] | 24 | sk_out_of_memory(size); |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 25 | } |
| 26 | return p; |
| 27 | } |
| 28 | |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame^] | 29 | void sk_abort_no_print() { |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 30 | abort(); |
| 31 | } |
| 32 | |
| 33 | void sk_out_of_memory(void) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 34 | SkDEBUGFAIL("sk_out_of_memory"); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 35 | abort(); |
| 36 | } |
| 37 | |
| 38 | void* sk_malloc_throw(size_t size) { |
| 39 | return sk_malloc_flags(size, SK_MALLOC_THROW); |
| 40 | } |
| 41 | |
| 42 | void* sk_realloc_throw(void* addr, size_t size) { |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 43 | return throw_on_failure(size, realloc(addr, size)); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void sk_free(void* p) { |
| 47 | if (p) { |
| 48 | free(p); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void* sk_malloc_flags(size_t size, unsigned flags) { |
| 53 | void* p = malloc(size); |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 54 | if (flags & SK_MALLOC_THROW) { |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 55 | return throw_on_failure(size, p); |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 56 | } else { |
| 57 | return p; |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 58 | } |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void* sk_calloc(size_t size) { |
| 62 | return calloc(size, 1); |
| 63 | } |
| 64 | |
| 65 | void* sk_calloc_throw(size_t size) { |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 66 | return throw_on_failure(size, sk_calloc(size)); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 67 | } |