[email protected] | 237a1485 | 2012-04-28 02:56:38 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/allocator/allocator_extension.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | |
primiano | d3a81ab | 2016-01-25 22:21:15 | [diff] [blame] | 9 | #if defined(USE_TCMALLOC) |
| 10 | #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" |
| 11 | #include "third_party/tcmalloc/chromium/src/gperftools/malloc_extension.h" |
sque | be8dae53 | 2016-03-15 19:55:15 | [diff] [blame] | 12 | #include "third_party/tcmalloc/chromium/src/gperftools/malloc_hook.h" |
primiano | d3a81ab | 2016-01-25 22:21:15 | [diff] [blame] | 13 | #endif |
| 14 | |
[email protected] | 237a1485 | 2012-04-28 02:56:38 | [diff] [blame] | 15 | namespace base { |
| 16 | namespace allocator { |
| 17 | |
[email protected] | 237a1485 | 2012-04-28 02:56:38 | [diff] [blame] | 18 | void ReleaseFreeMemory() { |
primiano | d3a81ab | 2016-01-25 22:21:15 | [diff] [blame] | 19 | #if defined(USE_TCMALLOC) |
| 20 | ::MallocExtension::instance()->ReleaseFreeMemory(); |
| 21 | #endif |
primiano | dda6c27 | 2015-12-07 16:51:04 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | bool GetNumericProperty(const char* name, size_t* value) { |
primiano | d3a81ab | 2016-01-25 22:21:15 | [diff] [blame] | 25 | #if defined(USE_TCMALLOC) |
| 26 | return ::MallocExtension::instance()->GetNumericProperty(name, value); |
| 27 | #endif |
| 28 | return false; |
[email protected] | 237a1485 | 2012-04-28 02:56:38 | [diff] [blame] | 29 | } |
| 30 | |
primiano | d3a81ab | 2016-01-25 22:21:15 | [diff] [blame] | 31 | bool IsHeapProfilerRunning() { |
| 32 | #if defined(USE_TCMALLOC) |
| 33 | return ::IsHeapProfilerRunning(); |
| 34 | #endif |
| 35 | return false; |
ssid | 0943409 | 2015-10-26 23:05:04 | [diff] [blame] | 36 | } |
| 37 | |
sque | be8dae53 | 2016-03-15 19:55:15 | [diff] [blame] | 38 | void SetHooks(AllocHookFunc alloc_hook, FreeHookFunc free_hook) { |
| 39 | // TODO(sque): Use allocator shim layer instead. |
| 40 | #if defined(USE_TCMALLOC) |
| 41 | // Make sure no hooks get overwritten. |
| 42 | auto prev_alloc_hook = MallocHook::SetNewHook(alloc_hook); |
| 43 | if (alloc_hook) |
| 44 | DCHECK(!prev_alloc_hook); |
| 45 | |
| 46 | auto prev_free_hook = MallocHook::SetDeleteHook(free_hook); |
| 47 | if (free_hook) |
| 48 | DCHECK(!prev_free_hook); |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | int GetCallStack(void** stack, int max_stack_size) { |
| 53 | #if defined(USE_TCMALLOC) |
| 54 | return MallocHook::GetCallerStackTrace(stack, max_stack_size, 0); |
| 55 | #endif |
| 56 | return 0; |
| 57 | } |
| 58 | |
[email protected] | 237a1485 | 2012-04-28 02:56:38 | [diff] [blame] | 59 | } // namespace allocator |
| 60 | } // namespace base |