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 | aa97a84 | 2016-01-22 06:50:25 -0800 | [diff] [blame] | 29 | void sk_throw() { |
| 30 | SkDEBUGFAIL("sk_throw"); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 31 | abort(); |
| 32 | } |
| 33 | |
| 34 | void sk_out_of_memory(void) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 35 | SkDEBUGFAIL("sk_out_of_memory"); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 36 | abort(); |
| 37 | } |
| 38 | |
| 39 | void* sk_malloc_throw(size_t size) { |
| 40 | return sk_malloc_flags(size, SK_MALLOC_THROW); |
| 41 | } |
| 42 | |
| 43 | void* sk_realloc_throw(void* addr, size_t size) { |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 44 | return throw_on_failure(size, realloc(addr, size)); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void sk_free(void* p) { |
| 48 | if (p) { |
| 49 | free(p); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void* sk_malloc_flags(size_t size, unsigned flags) { |
| 54 | void* p = malloc(size); |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 55 | if (flags & SK_MALLOC_THROW) { |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 56 | return throw_on_failure(size, p); |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 57 | } else { |
| 58 | return p; |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 59 | } |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void* sk_calloc(size_t size) { |
| 63 | return calloc(size, 1); |
| 64 | } |
| 65 | |
| 66 | void* sk_calloc_throw(size_t size) { |
commit-bot@chromium.org | dd6cde5 | 2013-10-07 17:59:04 +0000 | [diff] [blame] | 67 | return throw_on_failure(size, sk_calloc(size)); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 68 | } |