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/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/process/internal_linux.h" | 14 #include "base/process/internal_linux.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 | 17 |
18 #if defined(USE_TCMALLOC) | 18 #if defined(USE_TCMALLOC) |
19 // Used by UncheckedMalloc. If tcmalloc is linked to the executable | 19 #include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h" |
20 // this will be replaced by a strong symbol that actually implement | |
21 // the semantics and don't call new handler in case the allocation fails. | |
22 extern "C" { | |
23 | |
24 __attribute__((weak, visibility("default"))) | |
25 void* tc_malloc_skip_new_handler_weak(size_t size); | |
26 | |
27 void* tc_malloc_skip_new_handler_weak(size_t size) { | |
28 return malloc(size); | |
29 } | |
30 | |
31 } | |
32 #endif | 20 #endif |
33 | 21 |
34 namespace base { | 22 namespace base { |
35 | 23 |
36 size_t g_oom_size = 0U; | 24 size_t g_oom_size = 0U; |
37 | 25 |
38 namespace { | 26 namespace { |
39 | 27 |
40 #if !defined(OS_ANDROID) | 28 #if !defined(OS_ANDROID) |
41 void OnNoMemorySize(size_t size) { | 29 void OnNoMemorySize(size_t size) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 void EnableTerminationOnOutOfMemory() { | 143 void EnableTerminationOnOutOfMemory() { |
156 #if defined(OS_ANDROID) | 144 #if defined(OS_ANDROID) |
157 // Android doesn't support setting a new handler. | 145 // Android doesn't support setting a new handler. |
158 DLOG(WARNING) << "Not feasible."; | 146 DLOG(WARNING) << "Not feasible."; |
159 #else | 147 #else |
160 // Set the new-out of memory handler. | 148 // Set the new-out of memory handler. |
161 std::set_new_handler(&OnNoMemory); | 149 std::set_new_handler(&OnNoMemory); |
162 // If we're using glibc's allocator, the above functions will override | 150 // If we're using glibc's allocator, the above functions will override |
163 // malloc and friends and make them die on out of memory. | 151 // malloc and friends and make them die on out of memory. |
164 #endif | 152 #endif |
| 153 #if defined(USE_TCMALLOC) |
| 154 // For tcmalloc, we need to tell it to behave like new. |
| 155 tc_set_new_mode(1); |
| 156 #endif |
165 } | 157 } |
166 | 158 |
167 // NOTE: This is not the only version of this function in the source: | 159 // NOTE: This is not the only version of this function in the source: |
168 // the setuid sandbox (in process_util_linux.c, in the sandbox source) | 160 // the setuid sandbox (in process_util_linux.c, in the sandbox source) |
169 // also has its own C version. | 161 // also has its own C version. |
170 bool AdjustOOMScore(ProcessId process, int score) { | 162 bool AdjustOOMScore(ProcessId process, int score) { |
171 if (score < 0 || score > kMaxOomScore) | 163 if (score < 0 || score > kMaxOomScore) |
172 return false; | 164 return false; |
173 | 165 |
174 FilePath oom_path(internal::GetProcPidDir(process)); | 166 FilePath oom_path(internal::GetProcPidDir(process)); |
(...skipping 26 matching lines...) Expand all Loading... |
201 return false; | 193 return false; |
202 } | 194 } |
203 | 195 |
204 bool UncheckedMalloc(size_t size, void** result) { | 196 bool UncheckedMalloc(size_t size, void** result) { |
205 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || \ | 197 #if defined(MEMORY_TOOL_REPLACES_ALLOCATOR) || \ |
206 (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)) | 198 (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)) |
207 *result = malloc(size); | 199 *result = malloc(size); |
208 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) | 200 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) |
209 *result = __libc_malloc(size); | 201 *result = __libc_malloc(size); |
210 #elif defined(USE_TCMALLOC) | 202 #elif defined(USE_TCMALLOC) |
211 *result = tc_malloc_skip_new_handler_weak(size); | 203 *result = tc_malloc_skip_new_handler(size); |
212 #endif | 204 #endif |
213 return *result != NULL; | 205 return *result != NULL; |
214 } | 206 } |
215 | 207 |
216 } // namespace base | 208 } // namespace base |
OLD | NEW |