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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 static void* sk_malloc_nothrow(size_t size) { | 53 static void* sk_malloc_nothrow(size_t size) { |
54 // TODO(b.kelemen): we should always use UncheckedMalloc but currently it | 54 // TODO(b.kelemen): we should always use UncheckedMalloc but currently it |
55 // doesn't work as intended everywhere. | 55 // doesn't work as intended everywhere. |
56 #if defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \ | 56 #if defined(OS_IOS) |
57 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) | 57 return malloc(size); |
| 58 #else |
58 void* result; | 59 void* result; |
59 // It's the responsibility of the caller to check the return value. | 60 // It's the responsibility of the caller to check the return value. |
60 ignore_result(base::UncheckedMalloc(size, &result)); | 61 ignore_result(base::UncheckedMalloc(size, &result)); |
61 return result; | 62 return result; |
62 #else | |
63 // This is not really thread safe. It only won't collide with itself, but w
e're totally | |
64 // unprotected from races with other code that calls set_new_handler. | |
65 SkAutoMutexAcquire lock(gSkNewHandlerMutex); | |
66 std::new_handler old_handler = std::set_new_handler(NULL); | |
67 void* p = malloc(size); | |
68 std::set_new_handler(old_handler); | |
69 return p; | |
70 #endif | 63 #endif |
71 } | 64 } |
72 | 65 |
73 void* sk_malloc_flags(size_t size, unsigned flags) { | 66 void* sk_malloc_flags(size_t size, unsigned flags) { |
74 if (flags & SK_MALLOC_THROW) { | 67 if (flags & SK_MALLOC_THROW) { |
75 return sk_malloc_throw(size); | 68 return sk_malloc_throw(size); |
76 } | 69 } |
77 return sk_malloc_nothrow(size); | 70 return sk_malloc_nothrow(size); |
78 } | 71 } |
79 | 72 |
80 void* sk_calloc_throw(size_t size) { | 73 void* sk_calloc_throw(size_t size) { |
81 return throw_on_failure(size, calloc(size, 1)); | 74 return throw_on_failure(size, calloc(size, 1)); |
82 } | 75 } |
83 | 76 |
84 void* sk_calloc(size_t size) { | 77 void* sk_calloc(size_t size) { |
85 // TODO(b.kelemen): we should always use UncheckedCalloc but currently it | 78 // TODO(b.kelemen): we should always use UncheckedCalloc but currently it |
86 // doesn't work as intended everywhere. | 79 // doesn't work as intended everywhere. |
87 #if defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \ | 80 #if defined(OS_IOS) |
88 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) | 81 return calloc(1, size); |
| 82 #else |
89 void* result; | 83 void* result; |
90 // It's the responsibility of the caller to check the return value. | 84 // It's the responsibility of the caller to check the return value. |
91 ignore_result(base::UncheckedCalloc(size, 1, &result)); | 85 ignore_result(base::UncheckedCalloc(size, 1, &result)); |
92 return result; | 86 return result; |
93 #else | |
94 SkAutoMutexAcquire lock(gSkNewHandlerMutex); | |
95 std::new_handler old_handler = std::set_new_handler(NULL); | |
96 void* p = calloc(size, 1); | |
97 std::set_new_handler(old_handler); | |
98 return p; | |
99 #endif | 87 #endif |
100 } | 88 } |
OLD | NEW |