OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include <new> | 7 #include <new> |
8 | 8 |
9 #include "base/process/memory.h" | 9 #include "base/process/memory.h" |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 void sk_free(void* p) { | 43 void sk_free(void* p) { |
44 if (p) { | 44 if (p) { |
45 free(p); | 45 free(p); |
46 } | 46 } |
47 } | 47 } |
48 | 48 |
49 void* sk_malloc_throw(size_t size) { | 49 void* sk_malloc_throw(size_t size) { |
50 return throw_on_failure(size, malloc(size)); | 50 return throw_on_failure(size, malloc(size)); |
51 } | 51 } |
52 | 52 |
53 // Platform specific ways to try really hard to get a malloc that won't crash on
failure. | |
54 static void* sk_malloc_nothrow(size_t size) { | 53 static void* sk_malloc_nothrow(size_t size) { |
55 #if defined(ANDROID) | 54 // TODO(b.kelemen): we should always use UncheckedMalloc but currently it |
56 // Android doesn't have std::set_new_handler, so we just call malloc. | 55 // doesn't work as intended everywhere. |
57 return malloc(size); | 56 #if defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \ |
58 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 57 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
59 return base::UncheckedMalloc(size); | 58 void* result; |
| 59 // It's the responsibility of the caller to check the return value. |
| 60 ignore_result(base::UncheckedMalloc(size, &result)); |
| 61 return result; |
60 #else | 62 #else |
61 // This is not really thread safe. It only won't collide with itself, but w
e're totally | 63 // This is not really thread safe. It only won't collide with itself, but w
e're totally |
62 // unprotected from races with other code that calls set_new_handler. | 64 // unprotected from races with other code that calls set_new_handler. |
63 SkAutoMutexAcquire lock(gSkNewHandlerMutex); | 65 SkAutoMutexAcquire lock(gSkNewHandlerMutex); |
64 std::new_handler old_handler = std::set_new_handler(NULL); | 66 std::new_handler old_handler = std::set_new_handler(NULL); |
65 void* p = malloc(size); | 67 void* p = malloc(size); |
66 std::set_new_handler(old_handler); | 68 std::set_new_handler(old_handler); |
67 return p; | 69 return p; |
68 #endif | 70 #endif |
69 } | 71 } |
70 | 72 |
71 void* sk_malloc_flags(size_t size, unsigned flags) { | 73 void* sk_malloc_flags(size_t size, unsigned flags) { |
72 if (flags & SK_MALLOC_THROW) { | 74 if (flags & SK_MALLOC_THROW) { |
73 return sk_malloc_throw(size); | 75 return sk_malloc_throw(size); |
74 } | 76 } |
75 return sk_malloc_nothrow(size); | 77 return sk_malloc_nothrow(size); |
76 } | 78 } |
77 | 79 |
78 void* sk_calloc_throw(size_t size) { | 80 void* sk_calloc_throw(size_t size) { |
79 return throw_on_failure(size, calloc(size, 1)); | 81 return throw_on_failure(size, calloc(size, 1)); |
80 } | 82 } |
81 | 83 |
82 // Jump through the same hoops as sk_malloc_nothrow to avoid a crash, but for ca
lloc. | |
83 void* sk_calloc(size_t size) { | 84 void* sk_calloc(size_t size) { |
84 #if defined(ANDROID) | 85 // TODO(b.kelemen): we should always use UncheckedCalloc but currently it |
85 return calloc(size, 1); | 86 // doesn't work as intended everywhere. |
86 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 87 #if defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \ |
87 return base::UncheckedCalloc(size, 1); | 88 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
| 89 void* result; |
| 90 // It's the responsibility of the caller to check the return value. |
| 91 ignore_result(base::UncheckedCalloc(size, 1, &result)); |
| 92 return result; |
88 #else | 93 #else |
89 SkAutoMutexAcquire lock(gSkNewHandlerMutex); | 94 SkAutoMutexAcquire lock(gSkNewHandlerMutex); |
90 std::new_handler old_handler = std::set_new_handler(NULL); | 95 std::new_handler old_handler = std::set_new_handler(NULL); |
91 void* p = calloc(size, 1); | 96 void* p = calloc(size, 1); |
92 std::set_new_handler(old_handler); | 97 std::set_new_handler(old_handler); |
93 return p; | 98 return p; |
94 #endif | 99 #endif |
95 } | 100 } |
OLD | NEW |