OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/allocator/allocator_shim.h" | 5 #include "base/allocator/allocator_shim.h" |
6 | 6 |
7 #include <config.h> | 7 #include <config.h> |
8 #include "base/allocator/allocator_extension_thunks.h" | |
8 #include "base/profiler/alternate_timer.h" | 9 #include "base/profiler/alternate_timer.h" |
9 #include "base/sysinfo.h" | 10 #include "base/sysinfo.h" |
10 | 11 |
11 // When defined, different heap allocators can be used via an environment | 12 // When defined, different heap allocators can be used via an environment |
12 // variable set before running the program. This may reduce the amount | 13 // variable set before running the program. This may reduce the amount |
13 // of inlining that we get with malloc/free/etc. Disabling makes it | 14 // of inlining that we get with malloc/free/etc. Disabling makes it |
14 // so that only tcmalloc can be used. | 15 // so that only tcmalloc can be used. |
15 #define ENABLE_DYNAMIC_ALLOCATOR_SWITCHING | 16 #define ENABLE_DYNAMIC_ALLOCATOR_SWITCHING |
16 | 17 |
17 // TODO(mbelshe): Ensure that all calls to tcmalloc have the proper call depth | 18 // TODO(mbelshe): Ensure that all calls to tcmalloc have the proper call depth |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 } | 223 } |
223 #endif | 224 #endif |
224 return MallocExtension::instance()->GetAllocatedSize(p); | 225 return MallocExtension::instance()->GetAllocatedSize(p); |
225 } | 226 } |
226 | 227 |
227 // This is included to resolve references from libcmt. | 228 // This is included to resolve references from libcmt. |
228 extern "C" intptr_t _get_heap_handle() { | 229 extern "C" intptr_t _get_heap_handle() { |
229 return 0; | 230 return 0; |
230 } | 231 } |
231 | 232 |
233 static void get_stats_thunk(char* buffer, int buffer_length) { | |
234 MallocExtension::instance()->GetStats(buffer, buffer_length); | |
willchan no longer on Chromium
2012/04/27 19:51:51
indent 2 spaces
| |
235 } | |
236 | |
237 static void release_free_memory_thunk() { | |
238 MallocExtension::instance()->ReleaseFreeMemory(); | |
willchan no longer on Chromium
2012/04/27 19:51:51
ditto
| |
239 } | |
240 | |
232 // The CRT heap initialization stub. | 241 // The CRT heap initialization stub. |
233 extern "C" int _heap_init() { | 242 extern "C" int _heap_init() { |
234 #ifdef ENABLE_DYNAMIC_ALLOCATOR_SWITCHING | 243 #ifdef ENABLE_DYNAMIC_ALLOCATOR_SWITCHING |
235 const char* environment_value = GetenvBeforeMain(primary_name); | 244 const char* environment_value = GetenvBeforeMain(primary_name); |
236 if (environment_value) { | 245 if (environment_value) { |
237 if (!stricmp(environment_value, "jemalloc")) | 246 if (!stricmp(environment_value, "jemalloc")) |
238 allocator = JEMALLOC; | 247 allocator = JEMALLOC; |
239 else if (!stricmp(environment_value, "winheap")) | 248 else if (!stricmp(environment_value, "winheap")) |
240 allocator = WINHEAP; | 249 allocator = WINHEAP; |
241 else if (!stricmp(environment_value, "winlfh")) | 250 else if (!stricmp(environment_value, "winlfh")) |
(...skipping 24 matching lines...) Expand all Loading... | |
266 // Provide optional hook for monitoring allocation quantities on a per-thread | 275 // Provide optional hook for monitoring allocation quantities on a per-thread |
267 // basis. Only set the hook if the environment indicates this needs to be | 276 // basis. Only set the hook if the environment indicates this needs to be |
268 // enabled. | 277 // enabled. |
269 const char* profiling = | 278 const char* profiling = |
270 GetenvBeforeMain(tracked_objects::kAlternateProfilerTime); | 279 GetenvBeforeMain(tracked_objects::kAlternateProfilerTime); |
271 if (profiling && *profiling == '1') { | 280 if (profiling && *profiling == '1') { |
272 tracked_objects::SetAlternateTimeSource( | 281 tracked_objects::SetAlternateTimeSource( |
273 tcmalloc::ThreadCache::GetBytesAllocatedOnCurrentThread); | 282 tcmalloc::ThreadCache::GetBytesAllocatedOnCurrentThread); |
274 } | 283 } |
275 | 284 |
285 base::allocator::thunks::SetGetStatsFunction(get_stats_thunk); | |
286 base::allocator::thunks::SetReleaseFreeMemoryFunction( | |
287 release_free_memory_thunk); | |
288 | |
276 return 1; | 289 return 1; |
277 } | 290 } |
278 | 291 |
279 // The CRT heap cleanup stub. | 292 // The CRT heap cleanup stub. |
280 extern "C" void _heap_term() {} | 293 extern "C" void _heap_term() {} |
281 | 294 |
282 // We set this to 1 because part of the CRT uses a check of _crtheap != 0 | 295 // We set this to 1 because part of the CRT uses a check of _crtheap != 0 |
283 // to test whether the CRT has been initialized. Once we've ripped out | 296 // to test whether the CRT has been initialized. Once we've ripped out |
284 // the allocators from libcmt, we need to provide this definition so that | 297 // the allocators from libcmt, we need to provide this definition so that |
285 // the rest of the CRT is still usable. | 298 // the rest of the CRT is still usable. |
(...skipping 23 matching lines...) Expand all Loading... | |
309 const char* secondary_value = secondary_length ? buffer : "TCMALLOC"; | 322 const char* secondary_value = secondary_length ? buffer : "TCMALLOC"; |
310 // Force renderer (or other subprocesses) to use secondary_value. | 323 // Force renderer (or other subprocesses) to use secondary_value. |
311 int ret_val = _putenv_s(primary_name, secondary_value); | 324 int ret_val = _putenv_s(primary_name, secondary_value); |
312 DCHECK_EQ(0, ret_val); | 325 DCHECK_EQ(0, ret_val); |
313 } | 326 } |
314 #endif // ENABLE_DYNAMIC_ALLOCATOR_SWITCHING | 327 #endif // ENABLE_DYNAMIC_ALLOCATOR_SWITCHING |
315 } | 328 } |
316 | 329 |
317 } // namespace allocator. | 330 } // namespace allocator. |
318 } // namespace base. | 331 } // namespace base. |
OLD | NEW |