| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <new> | 9 #include <new> |
| 10 | 10 |
| 11 #include "base/allocator/allocator_shim.h" |
| 12 #include "base/allocator/features.h" |
| 11 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 13 #include "base/logging.h" | 15 #include "base/logging.h" |
| 14 #include "base/process/internal_linux.h" | 16 #include "base/process/internal_linux.h" |
| 15 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 16 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 17 | 19 |
| 18 #if defined(USE_TCMALLOC) | 20 #if defined(USE_TCMALLOC) |
| 19 #include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h" | 21 #include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h" |
| 20 #endif | 22 #endif |
| (...skipping 13 matching lines...) Expand all Loading... |
| 34 LOG(FATAL) << "Out of memory."; | 36 LOG(FATAL) << "Out of memory."; |
| 35 } | 37 } |
| 36 | 38 |
| 37 void OnNoMemory() { | 39 void OnNoMemory() { |
| 38 OnNoMemorySize(0); | 40 OnNoMemorySize(0); |
| 39 } | 41 } |
| 40 #endif // !defined(OS_ANDROID) | 42 #endif // !defined(OS_ANDROID) |
| 41 | 43 |
| 42 } // namespace | 44 } // namespace |
| 43 | 45 |
| 46 // TODO(primiano): Once the unified shim is on by default (crbug.com/550886) |
| 47 // get rid of the code in this entire #if section. The whole termination-on-OOM |
| 48 // logic is implemented in the shim. |
| 44 #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \ | 49 #if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER) && \ |
| 45 !defined(THREAD_SANITIZER) && !defined(LEAK_SANITIZER) | 50 !defined(THREAD_SANITIZER) && !defined(LEAK_SANITIZER) && \ |
| 51 !BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 46 | 52 |
| 47 #if defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) | 53 #if defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) |
| 48 | 54 |
| 49 extern "C" { | 55 extern "C" { |
| 50 void* __libc_malloc(size_t size); | 56 void* __libc_malloc(size_t size); |
| 51 void* __libc_realloc(void* ptr, size_t size); | 57 void* __libc_realloc(void* ptr, size_t size); |
| 52 void* __libc_calloc(size_t nmemb, size_t size); | 58 void* __libc_calloc(size_t nmemb, size_t size); |
| 53 void* __libc_valloc(size_t size); | 59 void* __libc_valloc(size_t size); |
| 54 #if PVALLOC_AVAILABLE == 1 | 60 #if PVALLOC_AVAILABLE == 1 |
| 55 void* __libc_pvalloc(size_t size); | 61 void* __libc_pvalloc(size_t size); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 void EnableTerminationOnOutOfMemory() { | 149 void EnableTerminationOnOutOfMemory() { |
| 144 #if defined(OS_ANDROID) | 150 #if defined(OS_ANDROID) |
| 145 // Android doesn't support setting a new handler. | 151 // Android doesn't support setting a new handler. |
| 146 DLOG(WARNING) << "Not feasible."; | 152 DLOG(WARNING) << "Not feasible."; |
| 147 #else | 153 #else |
| 148 // Set the new-out of memory handler. | 154 // Set the new-out of memory handler. |
| 149 std::set_new_handler(&OnNoMemory); | 155 std::set_new_handler(&OnNoMemory); |
| 150 // If we're using glibc's allocator, the above functions will override | 156 // If we're using glibc's allocator, the above functions will override |
| 151 // malloc and friends and make them die on out of memory. | 157 // malloc and friends and make them die on out of memory. |
| 152 #endif | 158 #endif |
| 153 #if defined(USE_TCMALLOC) | 159 |
| 160 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 161 allocator::SetCallNewHandlerOnMallocFailure(true); |
| 162 #elif defined(USE_TCMALLOC) |
| 154 // For tcmalloc, we need to tell it to behave like new. | 163 // For tcmalloc, we need to tell it to behave like new. |
| 155 tc_set_new_mode(1); | 164 tc_set_new_mode(1); |
| 156 #endif | 165 #endif |
| 157 } | 166 } |
| 158 | 167 |
| 159 // NOTE: This is not the only version of this function in the source: | 168 // NOTE: This is not the only version of this function in the source: |
| 160 // the setuid sandbox (in process_util_linux.c, in the sandbox source) | 169 // the setuid sandbox (in process_util_linux.c, in the sandbox source) |
| 161 // also has its own C version. | 170 // also has its own C version. |
| 162 bool AdjustOOMScore(ProcessId process, int score) { | 171 bool AdjustOOMScore(ProcessId process, int score) { |
| 163 if (score < 0 || score > kMaxOomScore) | 172 if (score < 0 || score > kMaxOomScore) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 187 std::string score_str = IntToString(converted_score); | 196 std::string score_str = IntToString(converted_score); |
| 188 DVLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str; | 197 DVLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str; |
| 189 int score_len = static_cast<int>(score_str.length()); | 198 int score_len = static_cast<int>(score_str.length()); |
| 190 return (score_len == WriteFile(oom_file, score_str.c_str(), score_len)); | 199 return (score_len == WriteFile(oom_file, score_str.c_str(), score_len)); |
| 191 } | 200 } |
| 192 | 201 |
| 193 return false; | 202 return false; |
| 194 } | 203 } |
| 195 | 204 |
| 196 bool UncheckedMalloc(size_t size, void** result) { | 205 bool UncheckedMalloc(size_t size, void** result) { |
| 197 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || \ | 206 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 207 *result = allocator::UncheckedAlloc(size); |
| 208 #elif defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || \ |
| 198 (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)) | 209 (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)) |
| 199 *result = malloc(size); | 210 *result = malloc(size); |
| 200 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) | 211 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) |
| 201 *result = __libc_malloc(size); | 212 *result = __libc_malloc(size); |
| 202 #elif defined(USE_TCMALLOC) | 213 #elif defined(USE_TCMALLOC) |
| 203 *result = tc_malloc_skip_new_handler(size); | 214 *result = tc_malloc_skip_new_handler(size); |
| 204 #endif | 215 #endif |
| 205 return *result != NULL; | 216 return *result != NULL; |
| 206 } | 217 } |
| 207 | 218 |
| 208 } // namespace base | 219 } // namespace base |
| OLD | NEW |