Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: base/process/memory_linux.cc

Issue 55333002: Make possible to check memory allocations inside chromium (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix component and windows build Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 #if defined(USE_TCMALLOC)
133 static tc_malloc_skip_new_handler_function tc_malloc_skip_new_handler = NULL;
134
135 void SetTCMallocSkipNewHandlerFunction(
136 tc_malloc_skip_new_handler_function function) {
willchan no longer on Chromium 2014/01/16 01:30:05 Please CHECK(!tc_malloc_skip_new_handler); It shou
137 tc_malloc_skip_new_handler = function;
138 }
139 #endif
140
141 bool UncheckedMalloc(size_t size, void** result) {
willchan no longer on Chromium 2014/01/16 01:30:05 I'm confused in this function. Why don't we always
kbalazs 2014/01/16 02:24:50 In case of LIBC_GLIBC && !USE_TCMALLOC we override
willchan no longer on Chromium 2014/01/16 02:54:44 I see. That's a good point...I'm trying to think u
142 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
143 defined(THREAD_SANITIZER) || defined(LEAK_SANITIZER) || \
144 (!defined(LIBC_GLIBC) && !defined(USE_TCMALLOC))
145 *result = malloc(size);
146 #elif defined(LIBC_GLIBC) && !defined(USE_TCMALLOC)
147 *result = __libc_malloc(size);
148 #elif defined(USE_TCMALLOC)
149 *result = tc_malloc_skip_new_handler ?
150 (*tc_malloc_skip_new_handler)(size) : malloc(size);
151 #else
152 #error Not implemented
willchan no longer on Chromium 2014/01/16 01:30:05 I don't believe it's possible to ever hit this #el
153 #endif
154 return *result;
155 }
156
157 bool UncheckedCalloc(size_t num_items, size_t size, void** result) {
158 const size_t alloc_size = num_items * size;
159
160 // Overflow check
161 if (size && ((alloc_size / size) != num_items)) {
162 result = NULL;
163 return false;
164 }
165
166 if (!UncheckedMalloc(alloc_size, result))
167 return false;
168
169 memset(result, 0, alloc_size);
170 return true;
171 }
172
128 void EnableTerminationOnHeapCorruption() { 173 void EnableTerminationOnHeapCorruption() {
129 // On Linux, there nothing to do AFAIK. 174 // On Linux, there nothing to do AFAIK.
130 } 175 }
131 176
132 void EnableTerminationOnOutOfMemory() { 177 void EnableTerminationOnOutOfMemory() {
133 #if defined(OS_ANDROID) 178 #if defined(OS_ANDROID)
134 // Android doesn't support setting a new handler. 179 // Android doesn't support setting a new handler.
135 DLOG(WARNING) << "Not feasible."; 180 DLOG(WARNING) << "Not feasible.";
136 #else 181 #else
137 // Set the new-out of memory handler. 182 // Set the new-out of memory handler.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 int score_len = static_cast<int>(score_str.length()); 221 int score_len = static_cast<int>(score_str.length());
177 return (score_len == file_util::WriteFile(oom_file, 222 return (score_len == file_util::WriteFile(oom_file,
178 score_str.c_str(), 223 score_str.c_str(),
179 score_len)); 224 score_len));
180 } 225 }
181 226
182 return false; 227 return false;
183 } 228 }
184 229
185 } // namespace base 230 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698