| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #if defined(DART_USE_TCMALLOC) | 5 #if defined(DART_USE_TCMALLOC) |
| 6 | 6 |
| 7 #include "vm/malloc_hooks.h" | 7 #include "vm/malloc_hooks.h" |
| 8 | 8 |
| 9 #include "platform/assert.h" |
| 10 |
| 11 #include "base/spinlock.h" |
| 12 #include "gperftools/malloc_hook.h" |
| 13 |
| 9 namespace dart { | 14 namespace dart { |
| 10 | 15 |
| 16 // Use a simple spinlock to avoid race conditions while also avoiding use of |
| 17 // malloc/new. |
| 18 static SpinLock malloc_hooks_spinlock_(base::LINKER_INITIALIZED); |
| 19 |
| 20 // MallocHooks static member variables. |
| 21 bool MallocHooks::initialized_ = false; |
| 22 LowLevelAlloc::Arena* MallocHooks::malloc_hooks_memory_ = NULL; |
| 23 uintptr_t MallocHooks::heap_allocated_memory_in_bytes_ = 0; |
| 24 AddressMap<uintptr_t>* MallocHooks::allocation_map_ = NULL; |
| 25 |
| 26 |
| 11 void MallocHooks::Init() { | 27 void MallocHooks::Init() { |
| 12 // TODO(bkonyi): Implement | 28 SpinLockHolder l(&malloc_hooks_spinlock_); |
| 29 |
| 30 if (initialized_) { |
| 31 return; |
| 32 } |
| 33 initialized_ = true; |
| 34 |
| 35 // Allocate any memory needed for the MallocHooks datastructures |
| 36 // before we connect the hooks to avoid dependencies on malloc/free. |
| 37 malloc_hooks_memory_ = |
| 38 LowLevelAlloc::NewArena(0, LowLevelAlloc::DefaultArena()); |
| 39 allocation_map_ = |
| 40 new AddressMap<uintptr_t>(MallocHooksMalloc, MallocHooksFree); |
| 41 |
| 42 heap_allocated_memory_in_bytes_ = 0; |
| 43 |
| 44 // Register malloc hooks. |
| 45 ASSERT(MallocHook::AddNewHook(&RecordAllocHook)); |
| 46 ASSERT(MallocHook::AddDeleteHook(&RecordFreeHook)); |
| 47 } |
| 48 |
| 49 |
| 50 void MallocHooks::TearDown() { |
| 51 SpinLockHolder l(&malloc_hooks_spinlock_); |
| 52 ASSERT(MallocHook::RemoveNewHook(&RecordAllocHook)); |
| 53 ASSERT(MallocHook::RemoveDeleteHook(&RecordFreeHook)); |
| 54 initialized_ = false; |
| 55 } |
| 56 |
| 57 |
| 58 void MallocHooks::RecordAllocHook(const void* ptr, size_t size) { |
| 59 SpinLockHolder l(&malloc_hooks_spinlock_); |
| 60 if (ptr != NULL) { |
| 61 heap_allocated_memory_in_bytes_ += size; |
| 62 allocation_map_->Insert(ptr, size); |
| 63 } |
| 64 } |
| 65 |
| 66 |
| 67 void MallocHooks::RecordFreeHook(const void* ptr) { |
| 68 SpinLockHolder l(&malloc_hooks_spinlock_); |
| 69 if (ptr != NULL) { |
| 70 uintptr_t value = 0; |
| 71 ASSERT(allocation_map_->FindAndRemove(ptr, &value)); |
| 72 ASSERT(heap_allocated_memory_in_bytes_ >= value); |
| 73 heap_allocated_memory_in_bytes_ -= value; |
| 74 } |
| 13 } | 75 } |
| 14 | 76 |
| 15 } // namespace dart | 77 } // namespace dart |
| 16 | 78 |
| 17 #endif // defined(DART_USE_TCMALLOC) | 79 #endif // defined(DART_USE_TCMALLOC) |
| OLD | NEW |