Chromium Code Reviews| 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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 | 6 |
| 7 #if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | 7 #if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |
| 8 | 8 |
| 9 #include "vm/malloc_hooks.h" | 9 #include "vm/malloc_hooks.h" |
| 10 | 10 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 static intptr_t original_pid_; | 166 static intptr_t original_pid_; |
| 167 static Mutex* malloc_hook_mutex_; | 167 static Mutex* malloc_hook_mutex_; |
| 168 static intptr_t allocation_count_; | 168 static intptr_t allocation_count_; |
| 169 static intptr_t heap_allocated_memory_in_bytes_; | 169 static intptr_t heap_allocated_memory_in_bytes_; |
| 170 static AddressMap* address_map_; | 170 static AddressMap* address_map_; |
| 171 | 171 |
| 172 static const intptr_t kInvalidPid = -1; | 172 static const intptr_t kInvalidPid = -1; |
| 173 }; | 173 }; |
| 174 | 174 |
| 175 | 175 |
| 176 // MallocHookScope state. | |
| 177 ThreadLocalKey MallocHookScope::in_malloc_hook_flag_ = kUnsetThreadLocalKey; | |
| 178 | |
| 176 // MallocHooks state / locks. | 179 // MallocHooks state / locks. |
| 177 ThreadLocalKey MallocHookScope::in_malloc_hook_flag_ = kUnsetThreadLocalKey; | |
| 178 bool MallocHooksState::active_ = false; | 180 bool MallocHooksState::active_ = false; |
| 179 intptr_t MallocHooksState::original_pid_ = MallocHooksState::kInvalidPid; | 181 intptr_t MallocHooksState::original_pid_ = MallocHooksState::kInvalidPid; |
| 180 Mutex* MallocHooksState::malloc_hook_mutex_ = new Mutex(); | 182 Mutex* MallocHooksState::malloc_hook_mutex_ = new Mutex(); |
| 181 | 183 |
| 182 // Memory allocation state information. | 184 // Memory allocation state information. |
| 183 intptr_t MallocHooksState::allocation_count_ = 0; | 185 intptr_t MallocHooksState::allocation_count_ = 0; |
| 184 intptr_t MallocHooksState::heap_allocated_memory_in_bytes_ = 0; | 186 intptr_t MallocHooksState::heap_allocated_memory_in_bytes_ = 0; |
| 185 AddressMap* MallocHooksState::address_map_ = NULL; | 187 AddressMap* MallocHooksState::address_map_ = NULL; |
| 186 | 188 |
| 187 | 189 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 263 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); | 265 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); |
| 264 return MallocHooksState::heap_allocated_memory_in_bytes(); | 266 return MallocHooksState::heap_allocated_memory_in_bytes(); |
| 265 } | 267 } |
| 266 | 268 |
| 267 | 269 |
| 268 void MallocHooksState::RecordAllocHook(const void* ptr, size_t size) { | 270 void MallocHooksState::RecordAllocHook(const void* ptr, size_t size) { |
| 269 if (MallocHookScope::IsInHook() || !MallocHooksState::IsOriginalProcess()) { | 271 if (MallocHookScope::IsInHook() || !MallocHooksState::IsOriginalProcess()) { |
| 270 return; | 272 return; |
| 271 } | 273 } |
| 272 | 274 |
| 273 // Set the malloc hook flag before grabbing the mutex to avoid calling hooks | |
| 274 // again. | |
| 275 MallocHookScope mhs; | |
| 276 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); | 275 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); |
|
zra
2017/02/17 18:41:02
I think taking the log and checking Active() needs
bkonyi
2017/02/17 21:15:11
We can't take the lock before anything else, or el
| |
| 277 if ((ptr != NULL) && MallocHooksState::Active()) { | 276 if ((ptr != NULL) && MallocHooksState::Active()) { |
| 277 // Set the malloc hook flag before to avoid calling hooks again if memory is | |
| 278 // allocated/freed below. | |
| 279 MallocHookScope mhs; | |
| 278 MallocHooksState::IncrementHeapAllocatedMemoryInBytes(size); | 280 MallocHooksState::IncrementHeapAllocatedMemoryInBytes(size); |
| 279 MallocHooksState::address_map()->Insert(ptr, size); | 281 MallocHooksState::address_map()->Insert(ptr, size); |
| 280 } | 282 } |
| 281 } | 283 } |
| 282 | 284 |
| 283 | 285 |
| 284 void MallocHooksState::RecordFreeHook(const void* ptr) { | 286 void MallocHooksState::RecordFreeHook(const void* ptr) { |
| 285 if (MallocHookScope::IsInHook() || !MallocHooksState::IsOriginalProcess()) { | 287 if (MallocHookScope::IsInHook() || !MallocHooksState::IsOriginalProcess()) { |
| 286 return; | 288 return; |
| 287 } | 289 } |
| 288 | 290 |
| 289 // Set the malloc hook flag before grabbing the mutex to avoid calling hooks | |
| 290 // again. | |
| 291 MallocHookScope mhs; | |
| 292 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); | 291 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); |
|
zra
2017/02/17 18:41:02
ditto
bkonyi
2017/02/17 21:15:11
See above.
| |
| 293 if ((ptr != NULL) && MallocHooksState::Active()) { | 292 if ((ptr != NULL) && MallocHooksState::Active()) { |
| 293 // Set the malloc hook flag before to avoid calling hooks again if memory is | |
| 294 // allocated/freed below. | |
| 295 MallocHookScope mhs; | |
| 294 intptr_t size = 0; | 296 intptr_t size = 0; |
| 295 if (MallocHooksState::address_map()->Lookup(ptr, &size)) { | 297 if (MallocHooksState::address_map()->Lookup(ptr, &size)) { |
| 296 MallocHooksState::DecrementHeapAllocatedMemoryInBytes(size); | 298 MallocHooksState::DecrementHeapAllocatedMemoryInBytes(size); |
| 297 MallocHooksState::address_map()->Remove(ptr); | 299 MallocHooksState::address_map()->Remove(ptr); |
| 298 } | 300 } |
| 299 } | 301 } |
| 300 } | 302 } |
| 301 | 303 |
| 302 } // namespace dart | 304 } // namespace dart |
| 303 | 305 |
| 304 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | 306 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |
| OLD | NEW |