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 #include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h" | |
willchan no longer on Chromium
2013/11/28 03:38:28
You should use an #if defined(USE_TCMALLOC) here.
| |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
17 size_t g_oom_size = 0U; | 18 size_t g_oom_size = 0U; |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 #if !defined(OS_ANDROID) | 22 #if !defined(OS_ANDROID) |
22 void OnNoMemorySize(size_t size) { | 23 void OnNoMemorySize(size_t size) { |
23 g_oom_size = size; | 24 g_oom_size = size; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 } // extern C | 119 } // extern C |
119 | 120 |
120 #else | 121 #else |
121 | 122 |
122 // TODO(mostynb@opera.com): dlsym dance | 123 // TODO(mostynb@opera.com): dlsym dance |
123 | 124 |
124 #endif // LIBC_GLIBC && !USE_TCMALLOC | 125 #endif // LIBC_GLIBC && !USE_TCMALLOC |
125 | 126 |
126 #endif // !*_SANITIZER | 127 #endif // !*_SANITIZER |
127 | 128 |
129 bool UncheckedMalloc(size_t size, void** result) { | |
130 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ | |
131 defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \ | |
132 (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)) | |
133 *result = malloc(size); | |
134 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) | |
135 *result = __libc_malloc(size); | |
136 #else // tcmalloc | |
willchan no longer on Chromium
2013/11/28 03:38:28
please do #elif defined(USE_TCMALLOC) and an #else
| |
137 *result = tc_malloc_skip_new_handler(size); | |
138 #endif | |
139 return *result; | |
140 } | |
141 | |
142 bool UncheckedCalloc(size_t num_items, size_t size, void** result) { | |
143 const size_t alloc_size = num_items * size; | |
144 | |
145 // Overflow check | |
146 if (size && ((alloc_size / size) != num_items)) { | |
147 result = NULL; | |
148 return false; | |
149 } | |
150 | |
151 if (!UncheckedMalloc(alloc_size, result)) | |
152 return false; | |
153 | |
154 memset(result, 0, alloc_size); | |
155 return true; | |
156 } | |
157 | |
128 void EnableTerminationOnHeapCorruption() { | 158 void EnableTerminationOnHeapCorruption() { |
129 // On Linux, there nothing to do AFAIK. | 159 // On Linux, there nothing to do AFAIK. |
130 } | 160 } |
131 | 161 |
132 void EnableTerminationOnOutOfMemory() { | 162 void EnableTerminationOnOutOfMemory() { |
133 #if defined(OS_ANDROID) | 163 #if defined(OS_ANDROID) |
134 // Android doesn't support setting a new handler. | 164 // Android doesn't support setting a new handler. |
135 DLOG(WARNING) << "Not feasible."; | 165 DLOG(WARNING) << "Not feasible."; |
136 #else | 166 #else |
137 // Set the new-out of memory handler. | 167 // 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()); | 206 int score_len = static_cast<int>(score_str.length()); |
177 return (score_len == file_util::WriteFile(oom_file, | 207 return (score_len == file_util::WriteFile(oom_file, |
178 score_str.c_str(), | 208 score_str.c_str(), |
179 score_len)); | 209 score_len)); |
180 } | 210 } |
181 | 211 |
182 return false; | 212 return false; |
183 } | 213 } |
184 | 214 |
185 } // namespace base | 215 } // namespace base |
OLD | NEW |