blob: 6db65a8524524831aaf3402ea9e49cec4e75051c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com7a561082009-05-04 19:24:56 +00008#include "SkTypes.h"
9#include <stdio.h>
10#include <stdlib.h>
11
mtklein@google.com519f9672013-09-20 14:31:45 +000012static 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.com7a561082009-05-04 19:24:56 +000020void sk_throw() {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000021 SkDEBUGFAIL("sk_throw");
reed@android.com7a561082009-05-04 19:24:56 +000022 abort();
23}
24
25void sk_out_of_memory(void) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000026 SkDEBUGFAIL("sk_out_of_memory");
reed@android.com7a561082009-05-04 19:24:56 +000027 abort();
28}
29
30void* sk_malloc_throw(size_t size) {
31 return sk_malloc_flags(size, SK_MALLOC_THROW);
32}
33
34void* sk_realloc_throw(void* addr, size_t size) {
mtklein@google.com519f9672013-09-20 14:31:45 +000035 return throwOnFailure(size, realloc(addr, size));
reed@android.com7a561082009-05-04 19:24:56 +000036}
37
38void sk_free(void* p) {
39 if (p) {
40 free(p);
41 }
42}
43
44void* sk_malloc_flags(size_t size, unsigned flags) {
45 void* p = malloc(size);
mtklein@google.com519f9672013-09-20 14:31:45 +000046 if (flags & SK_MALLOC_THROW) {
47 return throwOnFailure(size, p);
48 } else {
49 return p;
reed@android.com7a561082009-05-04 19:24:56 +000050 }
mtklein@google.com519f9672013-09-20 14:31:45 +000051}
52
53void* sk_calloc(size_t size) {
54 return calloc(size, 1);
55}
56
57void* sk_calloc_throw(size_t size) {
58 return throwOnFailure(size, sk_calloc(size));
reed@android.com7a561082009-05-04 19:24:56 +000059}