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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 | 7 |
8 #include "base/process/memory.h" | 8 #include "base/process/memory.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "third_party/skia/include/core/SkTypes.h" | 10 #include "third_party/skia/include/core/SkTypes.h" |
11 | 11 |
| 12 #if defined(OS_WIN) |
| 13 #include <windows.h> |
| 14 #endif |
| 15 |
12 // This implementation of sk_malloc_flags() and friends is similar to | 16 // This implementation of sk_malloc_flags() and friends is similar to |
13 // SkMemory_malloc.cpp, except it uses base::UncheckedMalloc and friends | 17 // SkMemory_malloc.cpp, except it uses base::UncheckedMalloc and friends |
14 // for non-SK_MALLOC_THROW calls. | 18 // for non-SK_MALLOC_THROW calls. |
15 // | 19 // |
16 // The name of this file is historic: a previous implementation tried to | 20 // The name of this file is historic: a previous implementation tried to |
17 // use std::set_new_handler() for the same effect, but it didn't actually work. | 21 // use std::set_new_handler() for the same effect, but it didn't actually work. |
18 | 22 |
19 static inline void* throw_on_failure(size_t size, void* p) { | 23 static inline void* throw_on_failure(size_t size, void* p) { |
20 if (size > 0 && p == NULL) { | 24 if (size > 0 && p == NULL) { |
21 // If we've got a NULL here, the only reason we should have failed is ru
nning out of RAM. | 25 // If we've got a NULL here, the only reason we should have failed is ru
nning out of RAM. |
22 sk_out_of_memory(); | 26 sk_out_of_memory(); |
23 } | 27 } |
24 return p; | 28 return p; |
25 } | 29 } |
26 | 30 |
27 void sk_abort_no_print() { | 31 void sk_abort_no_print() { |
28 abort(); | 32 abort(); |
29 } | 33 } |
30 | 34 |
31 void sk_out_of_memory(void) { | 35 void sk_out_of_memory(void) { |
32 SkASSERT(!"sk_out_of_memory"); | 36 SkASSERT(!"sk_out_of_memory"); |
| 37 #if defined(OS_WIN) |
| 38 // Kill the process. This is important for security since most of code |
| 39 // does not check the result of memory allocation. |
| 40 // https://msdn.microsoft.com/en-us/library/het71c37.aspx |
| 41 ::RaiseException(base::win::kOomExceptionCode, EXCEPTION_NONCONTINUABLE, 0, |
| 42 nullptr); |
| 43 #endif |
33 abort(); | 44 abort(); |
34 } | 45 } |
35 | 46 |
36 void* sk_realloc_throw(void* addr, size_t size) { | 47 void* sk_realloc_throw(void* addr, size_t size) { |
37 return throw_on_failure(size, realloc(addr, size)); | 48 return throw_on_failure(size, realloc(addr, size)); |
38 } | 49 } |
39 | 50 |
40 void sk_free(void* p) { | 51 void sk_free(void* p) { |
41 if (p) { | 52 if (p) { |
42 free(p); | 53 free(p); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 result = calloc(1, size); | 104 result = calloc(1, size); |
94 #else | 105 #else |
95 // It's the responsibility of the caller to check the return value. | 106 // It's the responsibility of the caller to check the return value. |
96 ignore_result(base::UncheckedCalloc(size, 1, &result)); | 107 ignore_result(base::UncheckedCalloc(size, 1, &result)); |
97 #endif | 108 #endif |
98 if (result) { | 109 if (result) { |
99 prevent_overcommit(0, size, result); | 110 prevent_overcommit(0, size, result); |
100 } | 111 } |
101 return result; | 112 return result; |
102 } | 113 } |
OLD | NEW |