| 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 19 matching lines...) Expand all Loading... |
| 30 #include "wtf/ThreadingPrimitives.h" | 30 #include "wtf/ThreadingPrimitives.h" |
| 31 #include "wtf/text/StringBuilder.h" | 31 #include "wtf/text/StringBuilder.h" |
| 32 #include "wtf/text/StringHash.h" | 32 #include "wtf/text/StringHash.h" |
| 33 #include "wtf/text/WTFString.h" | 33 #include "wtf/text/WTFString.h" |
| 34 | 34 |
| 35 namespace WTF { | 35 namespace WTF { |
| 36 | 36 |
| 37 #if ENABLE(INSTANCE_COUNTER) || ENABLE(DETAILED_MEMORY_INFRA) | 37 #if ENABLE(INSTANCE_COUNTER) || ENABLE(DETAILED_MEMORY_INFRA) |
| 38 | 38 |
| 39 #if COMPILER(CLANG) | 39 #if COMPILER(CLANG) |
| 40 const size_t stringWithTypeNamePrefixLength = sizeof("const char *WTF::getString
WithTypeName() [T = ") - 1; | 40 const size_t stringWithTypeNamePrefixLength = |
| 41 sizeof("const char *WTF::getStringWithTypeName() [T = ") - 1; |
| 41 const size_t stringWithTypeNamePostfixLength = sizeof("]") - 1; | 42 const size_t stringWithTypeNamePostfixLength = sizeof("]") - 1; |
| 42 #elif COMPILER(GCC) | 43 #elif COMPILER(GCC) |
| 43 const size_t stringWithTypeNamePrefixLength = sizeof("const char* WTF::getString
WithTypeName() [with T = ") - 1; | 44 const size_t stringWithTypeNamePrefixLength = |
| 45 sizeof("const char* WTF::getStringWithTypeName() [with T = ") - 1; |
| 44 const size_t stringWithTypeNamePostfixLength = sizeof("]") - 1; | 46 const size_t stringWithTypeNamePostfixLength = sizeof("]") - 1; |
| 45 #elif COMPILER(MSVC) | 47 #elif COMPILER(MSVC) |
| 46 const size_t stringWithTypeNamePrefixLength = sizeof("const char *__cdecl WTF::g
etStringWithTypeName<class ") - 1; | 48 const size_t stringWithTypeNamePrefixLength = |
| 49 sizeof("const char *__cdecl WTF::getStringWithTypeName<class ") - 1; |
| 47 const size_t stringWithTypeNamePostfixLength = sizeof(">(void)") - 1; | 50 const size_t stringWithTypeNamePostfixLength = sizeof(">(void)") - 1; |
| 48 #else | 51 #else |
| 49 #warning "Extracting typename is supported only in compiler GCC, CLANG and MSVC
at this moment" | 52 #warning \ |
| 53 "Extracting typename is supported only in compiler GCC, CLANG and MSVC at th
is moment" |
| 50 #endif | 54 #endif |
| 51 | 55 |
| 52 // This function is used to stringify a typename T without using RTTI. | 56 // This function is used to stringify a typename T without using RTTI. |
| 53 // The result of stringWithTypeName<T>() is given as |funcName|. |extractTypeNam
eFromFunctionName| then extracts a typename string from |funcName|. | 57 // The result of stringWithTypeName<T>() is given as |funcName|. |extractTypeNam
eFromFunctionName| then extracts a typename string from |funcName|. |
| 54 String extractTypeNameFromFunctionName(const char* funcName) | 58 String extractTypeNameFromFunctionName(const char* funcName) { |
| 55 { | |
| 56 #if COMPILER(CLANG) || COMPILER(GCC) || COMPILER(MSVC) | 59 #if COMPILER(CLANG) || COMPILER(GCC) || COMPILER(MSVC) |
| 57 size_t funcNameLength = strlen(funcName); | 60 size_t funcNameLength = strlen(funcName); |
| 58 ASSERT(funcNameLength > stringWithTypeNamePrefixLength + stringWithTypeNameP
ostfixLength); | 61 ASSERT(funcNameLength > |
| 62 stringWithTypeNamePrefixLength + stringWithTypeNamePostfixLength); |
| 59 | 63 |
| 60 const char* funcNameWithoutPrefix = funcName + stringWithTypeNamePrefixLengt
h; | 64 const char* funcNameWithoutPrefix = funcName + stringWithTypeNamePrefixLength; |
| 61 return String(funcNameWithoutPrefix, funcNameLength - stringWithTypeNamePref
ixLength - stringWithTypeNamePostfixLength); | 65 return String(funcNameWithoutPrefix, funcNameLength - |
| 66 stringWithTypeNamePrefixLength - |
| 67 stringWithTypeNamePostfixLength); |
| 62 #else | 68 #else |
| 63 return String("unknown"); | 69 return String("unknown"); |
| 64 #endif | 70 #endif |
| 65 } | 71 } |
| 66 | 72 |
| 67 class InstanceCounter { | 73 class InstanceCounter { |
| 68 public: | 74 public: |
| 69 void incrementInstanceCount(const String& instanceName, void* ptr); | 75 void incrementInstanceCount(const String& instanceName, void* ptr); |
| 70 void decrementInstanceCount(const String& instanceName, void* ptr); | 76 void decrementInstanceCount(const String& instanceName, void* ptr); |
| 71 String dump(); | 77 String dump(); |
| 72 | 78 |
| 73 static InstanceCounter* instance() | 79 static InstanceCounter* instance() { |
| 74 { | 80 DEFINE_STATIC_LOCAL(InstanceCounter, self, ()); |
| 75 DEFINE_STATIC_LOCAL(InstanceCounter, self, ()); | 81 return &self; |
| 76 return &self; | 82 } |
| 77 } | |
| 78 | 83 |
| 79 private: | 84 private: |
| 80 InstanceCounter() { } | 85 InstanceCounter() {} |
| 81 | 86 |
| 82 Mutex m_mutex; | 87 Mutex m_mutex; |
| 83 HashMap<String, int> m_counterMap; | 88 HashMap<String, int> m_counterMap; |
| 84 }; | 89 }; |
| 85 | 90 |
| 86 void incrementInstanceCount(const char* stringWithTypeNameName, void* ptr) | 91 void incrementInstanceCount(const char* stringWithTypeNameName, void* ptr) { |
| 87 { | 92 String instanceName = extractTypeNameFromFunctionName(stringWithTypeNameName); |
| 88 String instanceName = extractTypeNameFromFunctionName(stringWithTypeNameName
); | 93 InstanceCounter::instance()->incrementInstanceCount(instanceName, ptr); |
| 89 InstanceCounter::instance()->incrementInstanceCount(instanceName, ptr); | |
| 90 } | 94 } |
| 91 | 95 |
| 92 void decrementInstanceCount(const char* stringWithTypeNameName, void* ptr) | 96 void decrementInstanceCount(const char* stringWithTypeNameName, void* ptr) { |
| 93 { | 97 String instanceName = extractTypeNameFromFunctionName(stringWithTypeNameName); |
| 94 String instanceName = extractTypeNameFromFunctionName(stringWithTypeNameName
); | 98 InstanceCounter::instance()->decrementInstanceCount(instanceName, ptr); |
| 95 InstanceCounter::instance()->decrementInstanceCount(instanceName, ptr); | |
| 96 } | 99 } |
| 97 | 100 |
| 98 String dumpRefCountedInstanceCounts() | 101 String dumpRefCountedInstanceCounts() { |
| 99 { | 102 return InstanceCounter::instance()->dump(); |
| 100 return InstanceCounter::instance()->dump(); | |
| 101 } | 103 } |
| 102 | 104 |
| 103 void InstanceCounter::incrementInstanceCount(const String& instanceName, void* p
tr) | 105 void InstanceCounter::incrementInstanceCount(const String& instanceName, |
| 104 { | 106 void* ptr) { |
| 105 MutexLocker locker(m_mutex); | 107 MutexLocker locker(m_mutex); |
| 106 HashMap<String, int>::AddResult result = m_counterMap.add(instanceName, 1); | 108 HashMap<String, int>::AddResult result = m_counterMap.add(instanceName, 1); |
| 107 if (!result.isNewEntry) | 109 if (!result.isNewEntry) |
| 108 ++(result.storedValue->value); | 110 ++(result.storedValue->value); |
| 109 } | 111 } |
| 110 | 112 |
| 111 void InstanceCounter::decrementInstanceCount(const String& instanceName, void* p
tr) | 113 void InstanceCounter::decrementInstanceCount(const String& instanceName, |
| 112 { | 114 void* ptr) { |
| 113 MutexLocker locker(m_mutex); | 115 MutexLocker locker(m_mutex); |
| 114 HashMap<String, int>::iterator it = m_counterMap.find(instanceName); | 116 HashMap<String, int>::iterator it = m_counterMap.find(instanceName); |
| 115 ASSERT(it != m_counterMap.end()); | 117 ASSERT(it != m_counterMap.end()); |
| 116 | 118 |
| 117 --(it->value); | 119 --(it->value); |
| 118 if (!it->value) | 120 if (!it->value) |
| 119 m_counterMap.remove(it); | 121 m_counterMap.remove(it); |
| 120 } | 122 } |
| 121 | 123 |
| 122 String InstanceCounter::dump() | 124 String InstanceCounter::dump() { |
| 123 { | 125 MutexLocker locker(m_mutex); |
| 124 MutexLocker locker(m_mutex); | |
| 125 | 126 |
| 126 StringBuilder builder; | 127 StringBuilder builder; |
| 127 | 128 |
| 128 builder.append('{'); | 129 builder.append('{'); |
| 129 HashMap<String, int>::iterator it = m_counterMap.begin(); | 130 HashMap<String, int>::iterator it = m_counterMap.begin(); |
| 130 HashMap<String, int>::iterator itEnd = m_counterMap.end(); | 131 HashMap<String, int>::iterator itEnd = m_counterMap.end(); |
| 131 for (; it != itEnd; ++it) { | 132 for (; it != itEnd; ++it) { |
| 132 if (it != m_counterMap.begin()) | 133 if (it != m_counterMap.begin()) |
| 133 builder.append(','); | 134 builder.append(','); |
| 134 builder.append('"'); | 135 builder.append('"'); |
| 135 builder.append(it->key); | 136 builder.append(it->key); |
| 136 builder.appendLiteral("\": "); | 137 builder.appendLiteral("\": "); |
| 137 builder.appendNumber(it->value); | 138 builder.appendNumber(it->value); |
| 138 } | 139 } |
| 139 builder.append('}'); | 140 builder.append('}'); |
| 140 | 141 |
| 141 return builder.toString(); | 142 return builder.toString(); |
| 142 } | 143 } |
| 143 | 144 |
| 144 #else | 145 #else |
| 145 | 146 |
| 146 String dumpRefCountedInstanceCounts() | 147 String dumpRefCountedInstanceCounts() { |
| 147 { | 148 return String("{}"); |
| 148 return String("{}"); | |
| 149 } | 149 } |
| 150 | 150 |
| 151 #endif // ENABLE(INSTANCE_COUNTER) || ENABLE(DETAILED_MEMORY_INFRA) | 151 #endif // ENABLE(INSTANCE_COUNTER) || ENABLE(DETAILED_MEMORY_INFRA) |
| 152 | 152 |
| 153 } // namespace WTF | 153 } // namespace WTF |
| OLD | NEW |