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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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(LIBC_GLIBC) || defined(USE_TCMALLOC) || \ |
57 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) | 57 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
58 void* result; | 58 void* result; |
59 // It's the responsibility of the caller to check the return value. | 59 // It's the responsibility of the caller to check the return value. |
60 ignore_result(base::UncheckedMalloc(size, &result)); | 60 ignore_result(base::UncheckedMalloc(size, &result)); |
61 return result; | 61 return result; |
62 #else | 62 #else |
63 // This is not really thread safe. It only won't collide with itself, but w e're totally | 63 return malloc(size); |
cpu_(ooo_6.6-7.5)
2015/01/10 05:37:28
this code is wrong. libraries shall not to touch g
grt (UTC plus 2)
2015/01/12 14:29:14
Should this use base::UncheckedMalloc on Win as we
| |
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 | 64 #endif |
71 } | 65 } |
72 | 66 |
73 void* sk_malloc_flags(size_t size, unsigned flags) { | 67 void* sk_malloc_flags(size_t size, unsigned flags) { |
74 if (flags & SK_MALLOC_THROW) { | 68 if (flags & SK_MALLOC_THROW) { |
75 return sk_malloc_throw(size); | 69 return sk_malloc_throw(size); |
76 } | 70 } |
77 return sk_malloc_nothrow(size); | 71 return sk_malloc_nothrow(size); |
78 } | 72 } |
79 | 73 |
80 void* sk_calloc_throw(size_t size) { | 74 void* sk_calloc_throw(size_t size) { |
81 return throw_on_failure(size, calloc(size, 1)); | 75 return throw_on_failure(size, calloc(size, 1)); |
82 } | 76 } |
83 | 77 |
84 void* sk_calloc(size_t size) { | 78 void* sk_calloc(size_t size) { |
85 // TODO(b.kelemen): we should always use UncheckedCalloc but currently it | 79 // TODO(b.kelemen): we should always use UncheckedCalloc but currently it |
86 // doesn't work as intended everywhere. | 80 // doesn't work as intended everywhere. |
87 #if defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \ | 81 #if defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \ |
88 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) | 82 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
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 | 87 #else |
94 SkAutoMutexAcquire lock(gSkNewHandlerMutex); | 88 return calloc(size, 1); |
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 | 89 #endif |
100 } | 90 } |
OLD | NEW |