| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 const size_t extractNameFunctionPostfixLength = sizeof("]") - 1; | 45 const size_t extractNameFunctionPostfixLength = sizeof("]") - 1; |
| 46 #elif COMPILER(MSVC) | 46 #elif COMPILER(MSVC) |
| 47 const size_t extractNameFunctionPrefixLength = sizeof("const char *__cdecl WTF::
extractNameFunction<class ") - 1; | 47 const size_t extractNameFunctionPrefixLength = sizeof("const char *__cdecl WTF::
extractNameFunction<class ") - 1; |
| 48 const size_t extractNameFunctionPostfixLength = sizeof(">(void)") - 1; | 48 const size_t extractNameFunctionPostfixLength = sizeof(">(void)") - 1; |
| 49 #else | 49 #else |
| 50 #warning "Extracting typename is supported only in compiler GCC, CLANG and MSVC
at this moment" | 50 #warning "Extracting typename is supported only in compiler GCC, CLANG and MSVC
at this moment" |
| 51 #endif | 51 #endif |
| 52 | 52 |
| 53 // This function is used to stringify a typename T without using RTTI. | 53 // This function is used to stringify a typename T without using RTTI. |
| 54 // The result of extractNameFunction<T>() is given as |funcName|. |extractTypeNa
meFromFunctionName| then extracts a typename string from |funcName|. | 54 // The result of extractNameFunction<T>() is given as |funcName|. |extractTypeNa
meFromFunctionName| then extracts a typename string from |funcName|. |
| 55 String extractTypeNameFromFunctionName(const char* funcName) | 55 String extractTypeNameFromFunctionName(const char* funcName) { |
| 56 { | |
| 57 #if COMPILER(CLANG) || COMPILER(GCC) || COMPILER(MSVC) | 56 #if COMPILER(CLANG) || COMPILER(GCC) || COMPILER(MSVC) |
| 58 size_t funcNameLength = strlen(funcName); | 57 size_t funcNameLength = strlen(funcName); |
| 59 ASSERT(funcNameLength > extractNameFunctionPrefixLength + extractNameFunctio
nPostfixLength); | 58 ASSERT(funcNameLength > extractNameFunctionPrefixLength + extractNameFunctionP
ostfixLength); |
| 60 | 59 |
| 61 const char* funcNameWithoutPrefix = funcName + extractNameFunctionPrefixLeng
th; | 60 const char* funcNameWithoutPrefix = funcName + extractNameFunctionPrefixLength
; |
| 62 return String(funcNameWithoutPrefix, funcNameLength - extractNameFunctionPre
fixLength - extractNameFunctionPostfixLength); | 61 return String(funcNameWithoutPrefix, funcNameLength - extractNameFunctionPrefi
xLength - extractNameFunctionPostfixLength); |
| 63 #else | 62 #else |
| 64 return String("unknown"); | 63 return String("unknown"); |
| 65 #endif | 64 #endif |
| 66 } | 65 } |
| 67 | 66 |
| 68 class InstanceCounter { | 67 class InstanceCounter { |
| 69 public: | 68 public: |
| 70 void incrementInstanceCount(const String& instanceName, void* ptr); | 69 void incrementInstanceCount(const String& instanceName, void* ptr); |
| 71 void decrementInstanceCount(const String& instanceName, void* ptr); | 70 void decrementInstanceCount(const String& instanceName, void* ptr); |
| 72 String dump(); | 71 String dump(); |
| 73 | 72 |
| 74 static InstanceCounter* instance() | 73 static InstanceCounter* instance() { |
| 75 { | 74 DEFINE_STATIC_LOCAL(InstanceCounter, self, ()); |
| 76 DEFINE_STATIC_LOCAL(InstanceCounter, self, ()); | 75 return &self; |
| 77 return &self; | 76 } |
| 78 } | |
| 79 | 77 |
| 80 private: | 78 private: |
| 81 InstanceCounter() { } | 79 InstanceCounter() {} |
| 82 | 80 |
| 83 Mutex m_mutex; | 81 Mutex m_mutex; |
| 84 HashMap<String, int> m_counterMap; | 82 HashMap<String, int> m_counterMap; |
| 85 }; | 83 }; |
| 86 | 84 |
| 87 void incrementInstanceCount(const char* extractNameFunctionName, void* ptr) | 85 void incrementInstanceCount(const char* extractNameFunctionName, void* ptr) { |
| 88 { | 86 String instanceName = extractTypeNameFromFunctionName(extractNameFunctionName)
; |
| 89 String instanceName = extractTypeNameFromFunctionName(extractNameFunctionNam
e); | 87 InstanceCounter::instance()->incrementInstanceCount(instanceName, ptr); |
| 90 InstanceCounter::instance()->incrementInstanceCount(instanceName, ptr); | |
| 91 } | 88 } |
| 92 | 89 |
| 93 void decrementInstanceCount(const char* extractNameFunctionName, void* ptr) | 90 void decrementInstanceCount(const char* extractNameFunctionName, void* ptr) { |
| 94 { | 91 String instanceName = extractTypeNameFromFunctionName(extractNameFunctionName)
; |
| 95 String instanceName = extractTypeNameFromFunctionName(extractNameFunctionNam
e); | 92 InstanceCounter::instance()->decrementInstanceCount(instanceName, ptr); |
| 96 InstanceCounter::instance()->decrementInstanceCount(instanceName, ptr); | |
| 97 } | 93 } |
| 98 | 94 |
| 99 String dumpRefCountedInstanceCounts() | 95 String dumpRefCountedInstanceCounts() { |
| 100 { | 96 return InstanceCounter::instance()->dump(); |
| 101 return InstanceCounter::instance()->dump(); | |
| 102 } | 97 } |
| 103 | 98 |
| 104 void InstanceCounter::incrementInstanceCount(const String& instanceName, void* p
tr) | 99 void InstanceCounter::incrementInstanceCount(const String& instanceName, void* p
tr) { |
| 105 { | 100 MutexLocker locker(m_mutex); |
| 106 MutexLocker locker(m_mutex); | 101 HashMap<String, int>::AddResult result = m_counterMap.add(instanceName, 1); |
| 107 HashMap<String, int>::AddResult result = m_counterMap.add(instanceName, 1); | 102 if (!result.isNewEntry) |
| 108 if (!result.isNewEntry) | 103 ++(result.storedValue->value); |
| 109 ++(result.storedValue->value); | |
| 110 } | 104 } |
| 111 | 105 |
| 112 void InstanceCounter::decrementInstanceCount(const String& instanceName, void* p
tr) | 106 void InstanceCounter::decrementInstanceCount(const String& instanceName, void* p
tr) { |
| 113 { | 107 MutexLocker locker(m_mutex); |
| 114 MutexLocker locker(m_mutex); | 108 HashMap<String, int>::iterator it = m_counterMap.find(instanceName); |
| 115 HashMap<String, int>::iterator it = m_counterMap.find(instanceName); | 109 ASSERT(it != m_counterMap.end()); |
| 116 ASSERT(it != m_counterMap.end()); | |
| 117 | 110 |
| 118 --(it->value); | 111 --(it->value); |
| 119 if (!it->value) | 112 if (!it->value) |
| 120 m_counterMap.remove(it); | 113 m_counterMap.remove(it); |
| 121 } | 114 } |
| 122 | 115 |
| 123 String InstanceCounter::dump() | 116 String InstanceCounter::dump() { |
| 124 { | 117 MutexLocker locker(m_mutex); |
| 125 MutexLocker locker(m_mutex); | |
| 126 | 118 |
| 127 StringBuilder builder; | 119 StringBuilder builder; |
| 128 | 120 |
| 129 builder.append('{'); | 121 builder.append('{'); |
| 130 HashMap<String, int>::iterator it = m_counterMap.begin(); | 122 HashMap<String, int>::iterator it = m_counterMap.begin(); |
| 131 HashMap<String, int>::iterator itEnd = m_counterMap.end(); | 123 HashMap<String, int>::iterator itEnd = m_counterMap.end(); |
| 132 for (; it != itEnd; ++it) { | 124 for (; it != itEnd; ++it) { |
| 133 if (it != m_counterMap.begin()) | 125 if (it != m_counterMap.begin()) |
| 134 builder.append(','); | 126 builder.append(','); |
| 135 builder.append('"'); | 127 builder.append('"'); |
| 136 builder.append(it->key); | 128 builder.append(it->key); |
| 137 builder.appendLiteral("\": "); | 129 builder.appendLiteral("\": "); |
| 138 builder.appendNumber(it->value); | 130 builder.appendNumber(it->value); |
| 139 } | 131 } |
| 140 builder.append('}'); | 132 builder.append('}'); |
| 141 | 133 |
| 142 return builder.toString(); | 134 return builder.toString(); |
| 143 } | 135 } |
| 144 | 136 |
| 145 #else | 137 #else |
| 146 | 138 |
| 147 String dumpRefCountedInstanceCounts() | 139 String dumpRefCountedInstanceCounts() { |
| 148 { | 140 return String("{}"); |
| 149 return String("{}"); | |
| 150 } | 141 } |
| 151 | 142 |
| 152 #endif // ENABLE(INSTANCE_COUNTER) || ENABLE(DETAILED_MEMORY_INFRA) | 143 #endif // ENABLE(INSTANCE_COUNTER) || ENABLE(DETAILED_MEMORY_INFRA) |
| 153 | 144 |
| 154 } // namespace WTF | 145 } // namespace WTF |
| OLD | NEW |