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" | |
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 void OnNoMemorySize(size_t size) { | 22 void OnNoMemorySize(size_t size) { |
22 g_oom_size = size; | 23 g_oom_size = size; |
23 | 24 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 } // extern C | 117 } // extern C |
117 | 118 |
118 #else | 119 #else |
119 | 120 |
120 // TODO(mostynb@opera.com): dlsym dance | 121 // TODO(mostynb@opera.com): dlsym dance |
121 | 122 |
122 #endif // LIBC_GLIBC && !USE_TCMALLOC | 123 #endif // LIBC_GLIBC && !USE_TCMALLOC |
123 | 124 |
124 #endif // !*_SANITIZER | 125 #endif // !*_SANITIZER |
125 | 126 |
127 bool UncheckedMalloc(size_t size, void** result) { | |
128 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \ | |
129 defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \ | |
130 (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)) | |
131 *result = malloc(size); | |
132 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC) | |
133 *result = __libc_malloc(size); | |
134 #else // tcmalloc | |
135 *result = tc_malloc_skip_new_handler(size); | |
136 #endif | |
137 return *result; | |
138 } | |
139 | |
140 bool UncheckedCalloc(size_t num_items, size_t size, void** result) { | |
141 const size_t alloc_size = num_items * size; | |
142 | |
143 // Overflow check | |
144 if (alloc_size && (alloc_size / size) != num_items) | |
willchan no longer on Chromium
2013/11/12 16:04:19
Please add another set of parentheses around the !
kbalazs
2013/11/12 18:40:46
I made a mistake, the first part of the condition
| |
145 return NULL; | |
willchan no longer on Chromium
2013/11/12 16:04:19
Shouldn't this be return false?
| |
146 | |
147 if (!UncheckedMalloc(alloc_size, result)) | |
148 return false; | |
149 | |
150 memset(result, 0, alloc_size); | |
151 return true; | |
152 } | |
153 | |
126 void EnableTerminationOnHeapCorruption() { | 154 void EnableTerminationOnHeapCorruption() { |
127 // On Linux, there nothing to do AFAIK. | 155 // On Linux, there nothing to do AFAIK. |
128 } | 156 } |
129 | 157 |
130 void EnableTerminationOnOutOfMemory() { | 158 void EnableTerminationOnOutOfMemory() { |
131 #if defined(OS_ANDROID) | 159 #if defined(OS_ANDROID) |
132 // Android doesn't support setting a new handler. | 160 // Android doesn't support setting a new handler. |
133 DLOG(WARNING) << "Not feasible."; | 161 DLOG(WARNING) << "Not feasible."; |
134 #else | 162 #else |
135 // Set the new-out of memory handler. | 163 // Set the new-out of memory handler. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 int score_len = static_cast<int>(score_str.length()); | 202 int score_len = static_cast<int>(score_str.length()); |
175 return (score_len == file_util::WriteFile(oom_file, | 203 return (score_len == file_util::WriteFile(oom_file, |
176 score_str.c_str(), | 204 score_str.c_str(), |
177 score_len)); | 205 score_len)); |
178 } | 206 } |
179 | 207 |
180 return false; | 208 return false; |
181 } | 209 } |
182 | 210 |
183 } // namespace base | 211 } // namespace base |
OLD | NEW |