1 // Copyright 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.
5 #if defined(TYPE_PROFILING)
13 #include <gperftools/type_profiler_map.h>
15 #include "addressmap-inl.h"
16 #include "base/logging.h"
17 #include "base/low_level_alloc.h"
18 #include "base/spinlock.h"
19 #include "tcmalloc_guard.h"
23 const TCMallocGuard tcmalloc_initializer;
25 //----------------------------------------------------------------------
26 // A struct to store size and type_info of an object
27 //----------------------------------------------------------------------
31 ObjectInfo(): size(0), type(NULL) {}
32 ObjectInfo(size_t size_arg, const std::type_info* type_arg)
38 const std::type_info* type;
41 //----------------------------------------------------------------------
43 //----------------------------------------------------------------------
45 SpinLock g_type_profiler_lock(SpinLock::LINKER_INITIALIZED);
47 //----------------------------------------------------------------------
48 // Simple allocator for type_info map's internal memory
49 //----------------------------------------------------------------------
51 LowLevelAlloc::Arena* g_type_profiler_map_memory = NULL;
53 void* TypeProfilerMalloc(size_t bytes) {
54 return LowLevelAlloc::AllocWithArena(bytes, g_type_profiler_map_memory);
57 void TypeProfilerFree(void* p) {
58 LowLevelAlloc::Free(p);
61 //----------------------------------------------------------------------
62 // Profiling control/state data
63 //----------------------------------------------------------------------
65 AddressMap<ObjectInfo>* g_type_profiler_map = NULL;
67 //----------------------------------------------------------------------
68 // Manage type_info map
69 //----------------------------------------------------------------------
71 void InitializeTypeProfilerMemory() {
72 if (g_type_profiler_map_memory != NULL) {
73 RAW_DCHECK(g_type_profiler_map != NULL, "TypeProfilerMap is NULL");
77 g_type_profiler_map_memory =
78 LowLevelAlloc::NewArena(0, LowLevelAlloc::DefaultArena());
81 new(TypeProfilerMalloc(sizeof(*g_type_profiler_map)))
82 AddressMap<ObjectInfo>(TypeProfilerMalloc, TypeProfilerFree);
87 void InsertType(void* address, size_t size, const std::type_info& type) {
88 SpinLockHolder lock(&g_type_profiler_lock);
89 InitializeTypeProfilerMemory();
91 g_type_profiler_map->Insert(address, ObjectInfo(size, &type));
94 void EraseType(void* address) {
95 SpinLockHolder lock(&g_type_profiler_lock);
96 InitializeTypeProfilerMemory();
99 g_type_profiler_map->FindAndRemove(address, &obj);
102 const std::type_info* LookupType(const void* address) {
103 SpinLockHolder lock(&g_type_profiler_lock);
104 InitializeTypeProfilerMemory();
106 const ObjectInfo* found = g_type_profiler_map->Find(address);
112 #endif // defined(TYPE_PROFILING)