epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 8 | #include "SkTypes.h" |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame^] | 12 | static inline void* throwOnFailure(size_t size, void* p) { |
| 13 | if (size > 0 && p == NULL) { |
| 14 | // If we've got a NULL here, the only reason we should have failed is running out of RAM. |
| 15 | sk_out_of_memory(); |
| 16 | } |
| 17 | return p; |
| 18 | } |
| 19 | |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 20 | void sk_throw() { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 21 | SkDEBUGFAIL("sk_throw"); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 22 | abort(); |
| 23 | } |
| 24 | |
| 25 | void sk_out_of_memory(void) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 26 | SkDEBUGFAIL("sk_out_of_memory"); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 27 | abort(); |
| 28 | } |
| 29 | |
| 30 | void* sk_malloc_throw(size_t size) { |
| 31 | return sk_malloc_flags(size, SK_MALLOC_THROW); |
| 32 | } |
| 33 | |
| 34 | void* sk_realloc_throw(void* addr, size_t size) { |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame^] | 35 | return throwOnFailure(size, realloc(addr, size)); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void sk_free(void* p) { |
| 39 | if (p) { |
| 40 | free(p); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void* sk_malloc_flags(size_t size, unsigned flags) { |
| 45 | void* p = malloc(size); |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame^] | 46 | if (flags & SK_MALLOC_THROW) { |
| 47 | return throwOnFailure(size, p); |
| 48 | } else { |
| 49 | return p; |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 50 | } |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame^] | 51 | } |
| 52 | |
| 53 | void* sk_calloc(size_t size) { |
| 54 | return calloc(size, 1); |
| 55 | } |
| 56 | |
| 57 | void* sk_calloc_throw(size_t size) { |
| 58 | return throwOnFailure(size, sk_calloc(size)); |
reed@android.com | 7a56108 | 2009-05-04 19:24:56 +0000 | [diff] [blame] | 59 | } |