OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkTypes.h" | 8 #include "SkTypes.h" |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
11 | 11 |
| 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 ru
nning out of RAM. |
| 15 sk_out_of_memory(); |
| 16 } |
| 17 return p; |
| 18 } |
| 19 |
12 void sk_throw() { | 20 void sk_throw() { |
13 SkDEBUGFAIL("sk_throw"); | 21 SkDEBUGFAIL("sk_throw"); |
14 abort(); | 22 abort(); |
15 } | 23 } |
16 | 24 |
17 void sk_out_of_memory(void) { | 25 void sk_out_of_memory(void) { |
18 SkDEBUGFAIL("sk_out_of_memory"); | 26 SkDEBUGFAIL("sk_out_of_memory"); |
19 abort(); | 27 abort(); |
20 } | 28 } |
21 | 29 |
22 void* sk_malloc_throw(size_t size) { | 30 void* sk_malloc_throw(size_t size) { |
23 return sk_malloc_flags(size, SK_MALLOC_THROW); | 31 return sk_malloc_flags(size, SK_MALLOC_THROW); |
24 } | 32 } |
25 | 33 |
26 void* sk_realloc_throw(void* addr, size_t size) { | 34 void* sk_realloc_throw(void* addr, size_t size) { |
27 void* p = realloc(addr, size); | 35 return throwOnFailure(size, realloc(addr, size)); |
28 if (size == 0) { | |
29 return p; | |
30 } | |
31 if (p == NULL) { | |
32 sk_throw(); | |
33 } | |
34 return p; | |
35 } | 36 } |
36 | 37 |
37 void sk_free(void* p) { | 38 void sk_free(void* p) { |
38 if (p) { | 39 if (p) { |
39 free(p); | 40 free(p); |
40 } | 41 } |
41 } | 42 } |
42 | 43 |
43 void* sk_malloc_flags(size_t size, unsigned flags) { | 44 void* sk_malloc_flags(size_t size, unsigned flags) { |
44 void* p = malloc(size); | 45 void* p = malloc(size); |
45 if (p == NULL) { | 46 if (flags & SK_MALLOC_THROW) { |
46 if (flags & SK_MALLOC_THROW) { | 47 return throwOnFailure(size, p); |
47 sk_throw(); | 48 } else { |
48 } | 49 return p; |
49 } | 50 } |
50 return p; | |
51 } | 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)); |
| 59 } |
OLD | NEW |