blob: 008352ec4abd5a75da7d4fc2875903cc32ac71e9 [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
reed@android.com7a561082009-05-04 19:24:56 +00008#include "SkTypes.h"
bungemanf20488b2015-07-29 11:49:40 -07009
reed@android.com7a561082009-05-04 19:24:56 +000010#include <stdlib.h>
11
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);
18 abort();
19}
20
commit-bot@chromium.orgdd6cde52013-10-07 17:59:04 +000021static inline void* throw_on_failure(size_t size, void* p) {
halcanary96fcdcc2015-08-27 07:41:13 -070022 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.
halcanary61b14362014-07-10 08:59:38 -070024 sk_out_of_memory(size);
mtklein@google.com519f9672013-09-20 14:31:45 +000025 }
26 return p;
27}
28
djsollenf2b340f2016-01-29 08:51:04 -080029void sk_abort_no_print() {
reed@android.com7a561082009-05-04 19:24:56 +000030 abort();
31}
32
33void sk_out_of_memory(void) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000034 SkDEBUGFAIL("sk_out_of_memory");
reed@android.com7a561082009-05-04 19:24:56 +000035 abort();
36}
37
38void* sk_malloc_throw(size_t size) {
39 return sk_malloc_flags(size, SK_MALLOC_THROW);
40}
41
42void* sk_realloc_throw(void* addr, size_t size) {
commit-bot@chromium.orgdd6cde52013-10-07 17:59:04 +000043 return throw_on_failure(size, realloc(addr, size));
reed@android.com7a561082009-05-04 19:24:56 +000044}
45
46void sk_free(void* p) {
47 if (p) {
48 free(p);
49 }
50}
51
52void* sk_malloc_flags(size_t size, unsigned flags) {
53 void* p = malloc(size);
mtklein@google.com519f9672013-09-20 14:31:45 +000054 if (flags & SK_MALLOC_THROW) {
commit-bot@chromium.orgdd6cde52013-10-07 17:59:04 +000055 return throw_on_failure(size, p);
mtklein@google.com519f9672013-09-20 14:31:45 +000056 } else {
57 return p;
reed@android.com7a561082009-05-04 19:24:56 +000058 }
mtklein@google.com519f9672013-09-20 14:31:45 +000059}
60
61void* sk_calloc(size_t size) {
62 return calloc(size, 1);
63}
64
65void* sk_calloc_throw(size_t size) {
commit-bot@chromium.orgdd6cde52013-10-07 17:59:04 +000066 return throw_on_failure(size, sk_calloc(size));
reed@android.com7a561082009-05-04 19:24:56 +000067}