| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
| 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #include "wtf/InstanceCounter.h" | |
| 27 | |
| 28 #include "wtf/HashMap.h" | |
| 29 #include "wtf/StdLibExtras.h" | |
| 30 #include "wtf/ThreadingPrimitives.h" | |
| 31 #include "wtf/text/StringBuilder.h" | |
| 32 #include "wtf/text/StringHash.h" | |
| 33 #include "wtf/text/WTFString.h" | |
| 34 | |
| 35 namespace WTF { | |
| 36 | |
| 37 #if ENABLE(INSTANCE_COUNTER) | |
| 38 | |
| 39 #if COMPILER(CLANG) | |
| 40 const size_t stringWithTypeNamePrefixLength = | |
| 41 sizeof("const char *WTF::getStringWithTypeName() [T = ") - 1; | |
| 42 const size_t stringWithTypeNamePostfixLength = sizeof("]") - 1; | |
| 43 #elif COMPILER(GCC) | |
| 44 const size_t stringWithTypeNamePrefixLength = | |
| 45 sizeof("const char* WTF::getStringWithTypeName() [with T = ") - 1; | |
| 46 const size_t stringWithTypeNamePostfixLength = sizeof("]") - 1; | |
| 47 #elif COMPILER(MSVC) | |
| 48 const size_t stringWithTypeNamePrefixLength = | |
| 49 sizeof("const char *__cdecl WTF::getStringWithTypeName<class ") - 1; | |
| 50 const size_t stringWithTypeNamePostfixLength = sizeof(">(void)") - 1; | |
| 51 #else | |
| 52 #warning \ | |
| 53 "Extracting typename is supported only in compiler GCC, CLANG and MSVC at th
is moment" | |
| 54 #endif | |
| 55 | |
| 56 // This function is used to stringify a typename T without using RTTI. | |
| 57 // The result of stringWithTypeName<T>() is given as |funcName|. | |
| 58 // |extractTypeNameFromFunctionName| then extracts a typename string from | |
| 59 // |funcName|. | |
| 60 String extractTypeNameFromFunctionName(const char* funcName) { | |
| 61 #if COMPILER(CLANG) || COMPILER(GCC) || COMPILER(MSVC) | |
| 62 size_t funcNameLength = strlen(funcName); | |
| 63 DCHECK_GT(funcNameLength, | |
| 64 stringWithTypeNamePrefixLength + stringWithTypeNamePostfixLength); | |
| 65 | |
| 66 const char* funcNameWithoutPrefix = funcName + stringWithTypeNamePrefixLength; | |
| 67 return String(funcNameWithoutPrefix, funcNameLength - | |
| 68 stringWithTypeNamePrefixLength - | |
| 69 stringWithTypeNamePostfixLength); | |
| 70 #else | |
| 71 return String("unknown"); | |
| 72 #endif | |
| 73 } | |
| 74 | |
| 75 class InstanceCounter { | |
| 76 public: | |
| 77 void incrementInstanceCount(const String& instanceName, void* ptr); | |
| 78 void decrementInstanceCount(const String& instanceName, void* ptr); | |
| 79 String dump(); | |
| 80 | |
| 81 static InstanceCounter* instance() { | |
| 82 DEFINE_STATIC_LOCAL(InstanceCounter, self, ()); | |
| 83 return &self; | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 InstanceCounter() {} | |
| 88 | |
| 89 Mutex m_mutex; | |
| 90 HashMap<String, int> m_counterMap; | |
| 91 }; | |
| 92 | |
| 93 void incrementInstanceCount(const char* stringWithTypeNameName, void* ptr) { | |
| 94 String instanceName = extractTypeNameFromFunctionName(stringWithTypeNameName); | |
| 95 InstanceCounter::instance()->incrementInstanceCount(instanceName, ptr); | |
| 96 } | |
| 97 | |
| 98 void decrementInstanceCount(const char* stringWithTypeNameName, void* ptr) { | |
| 99 String instanceName = extractTypeNameFromFunctionName(stringWithTypeNameName); | |
| 100 InstanceCounter::instance()->decrementInstanceCount(instanceName, ptr); | |
| 101 } | |
| 102 | |
| 103 String dumpRefCountedInstanceCounts() { | |
| 104 return InstanceCounter::instance()->dump(); | |
| 105 } | |
| 106 | |
| 107 void InstanceCounter::incrementInstanceCount(const String& instanceName, | |
| 108 void* ptr) { | |
| 109 MutexLocker locker(m_mutex); | |
| 110 HashMap<String, int>::AddResult result = m_counterMap.add(instanceName, 1); | |
| 111 if (!result.isNewEntry) | |
| 112 ++(result.storedValue->value); | |
| 113 } | |
| 114 | |
| 115 void InstanceCounter::decrementInstanceCount(const String& instanceName, | |
| 116 void* ptr) { | |
| 117 MutexLocker locker(m_mutex); | |
| 118 HashMap<String, int>::iterator it = m_counterMap.find(instanceName); | |
| 119 DCHECK(it != m_counterMap.end()); | |
| 120 | |
| 121 --(it->value); | |
| 122 if (!it->value) | |
| 123 m_counterMap.remove(it); | |
| 124 } | |
| 125 | |
| 126 String InstanceCounter::dump() { | |
| 127 MutexLocker locker(m_mutex); | |
| 128 | |
| 129 StringBuilder builder; | |
| 130 | |
| 131 builder.append('{'); | |
| 132 HashMap<String, int>::iterator it = m_counterMap.begin(); | |
| 133 HashMap<String, int>::iterator itEnd = m_counterMap.end(); | |
| 134 for (; it != itEnd; ++it) { | |
| 135 if (it != m_counterMap.begin()) | |
| 136 builder.append(','); | |
| 137 builder.append('"'); | |
| 138 builder.append(it->key); | |
| 139 builder.append("\": "); | |
| 140 builder.appendNumber(it->value); | |
| 141 } | |
| 142 builder.append('}'); | |
| 143 | |
| 144 return builder.toString(); | |
| 145 } | |
| 146 | |
| 147 #else | |
| 148 | |
| 149 String dumpRefCountedInstanceCounts() { | |
| 150 return String("{}"); | |
| 151 } | |
| 152 | |
| 153 #endif // ENABLE(INSTANCE_COUNTER) | |
| 154 | |
| 155 } // namespace WTF | |
| OLD | NEW |