OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/process/memory.h" | 5 #include "base/process/memory.h" |
6 | 6 |
7 #include <new> | 7 #include <new> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/process/internal_linux.h" | 12 #include "base/process/internal_linux.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 | 14 |
| 15 #if defined(USE_TCMALLOC) |
| 16 #include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h" |
| 17 #endif |
| 18 |
15 namespace base { | 19 namespace base { |
16 | 20 |
17 size_t g_oom_size = 0U; | 21 size_t g_oom_size = 0U; |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
21 #if !defined(OS_ANDROID) | 25 #if !defined(OS_ANDROID) |
22 void OnNoMemorySize(size_t size) { | 26 void OnNoMemorySize(size_t size) { |
23 g_oom_size = size; | 27 g_oom_size = size; |
24 | 28 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } // extern C | 122 } // extern C |
119 | 123 |
120 #else | 124 #else |
121 | 125 |
122 // TODO(mostynb@opera.com): dlsym dance | 126 // TODO(mostynb@opera.com): dlsym dance |
123 | 127 |
124 #endif // LIBC_GLIBC && !USE_TCMALLOC | 128 #endif // LIBC_GLIBC && !USE_TCMALLOC |
125 | 129 |
126 #endif // !*_SANITIZER | 130 #endif // !*_SANITIZER |
127 | 131 |
| 132 bool UncheckedMalloc(size_t size, void** result) { |
| 133 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ |
| 134 defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \ |
| 135 (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)) |
| 136 *result = malloc(size); |
| 137 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) |
| 138 *result = __libc_malloc(size); |
| 139 #elif defined(USE_TCMALLOC) |
| 140 *result = tc_malloc_skip_new_handler(size); |
| 141 #else |
| 142 #error Not implemented |
| 143 #endif |
| 144 return *result; |
| 145 } |
| 146 |
| 147 bool UncheckedCalloc(size_t num_items, size_t size, void** result) { |
| 148 const size_t alloc_size = num_items * size; |
| 149 |
| 150 // Overflow check |
| 151 if (size && ((alloc_size / size) != num_items)) { |
| 152 result = NULL; |
| 153 return false; |
| 154 } |
| 155 |
| 156 if (!UncheckedMalloc(alloc_size, result)) |
| 157 return false; |
| 158 |
| 159 memset(result, 0, alloc_size); |
| 160 return true; |
| 161 } |
| 162 |
128 void EnableTerminationOnHeapCorruption() { | 163 void EnableTerminationOnHeapCorruption() { |
129 // On Linux, there nothing to do AFAIK. | 164 // On Linux, there nothing to do AFAIK. |
130 } | 165 } |
131 | 166 |
132 void EnableTerminationOnOutOfMemory() { | 167 void EnableTerminationOnOutOfMemory() { |
133 #if defined(OS_ANDROID) | 168 #if defined(OS_ANDROID) |
134 // Android doesn't support setting a new handler. | 169 // Android doesn't support setting a new handler. |
135 DLOG(WARNING) << "Not feasible."; | 170 DLOG(WARNING) << "Not feasible."; |
136 #else | 171 #else |
137 // Set the new-out of memory handler. | 172 // Set the new-out of memory handler. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 int score_len = static_cast<int>(score_str.length()); | 211 int score_len = static_cast<int>(score_str.length()); |
177 return (score_len == file_util::WriteFile(oom_file, | 212 return (score_len == file_util::WriteFile(oom_file, |
178 score_str.c_str(), | 213 score_str.c_str(), |
179 score_len)); | 214 score_len)); |
180 } | 215 } |
181 | 216 |
182 return false; | 217 return false; |
183 } | 218 } |
184 | 219 |
185 } // namespace base | 220 } // namespace base |
OLD | NEW |