This source file includes following definitions.
- extractNameFromFunctionName
- instance
- incrementInstanceCount
- decrementInstanceCount
- dumpRefCountedInstanceCounts
- incrementInstanceCount
- decrementInstanceCount
- dump
- dumpRefCountedInstanceCounts
#include "config.h"
#include "InstanceCounter.h"
#include "wtf/HashMap.h"
#include "wtf/StdLibExtras.h"
#include "wtf/ThreadingPrimitives.h"
#include "wtf/text/StringBuilder.h"
#include "wtf/text/StringHash.h"
#include "wtf/text/WTFString.h"
namespace WTF {
#ifdef ENABLE_INSTANCE_COUNTER
String extractNameFromFunctionName(const char* funcName)
{
#if COMPILER(GCC)
const size_t prefixLength = sizeof("const char* WTF::extractNameFunc() [with T = ") - 1;
size_t funcNameLength = strlen(funcName);
ASSERT(funcNameLength > prefixLength + 1);
const char* funcNameWithoutPrefix = funcName + prefixLength;
return String(funcNameWithoutPrefix, funcNameLength - prefixLength - 1 );
#else
ASSERT(false);
#endif
}
class InstanceCounter {
public:
void incrementInstanceCount(const String& instanceName, void* ptr);
void decrementInstanceCount(const String& instanceName, void* ptr);
String dump();
static InstanceCounter* instance()
{
DEFINE_STATIC_LOCAL(InstanceCounter, self, ());
return &self;
}
private:
InstanceCounter() { }
Mutex m_mutex;
HashMap<String, int> m_counterMap;
};
void incrementInstanceCount(const char* extractNameFuncName, void* ptr)
{
String instanceName = extractNameFromFunctionName(extractNameFuncName);
InstanceCounter::instance()->incrementInstanceCount(instanceName, ptr);
}
void decrementInstanceCount(const char* extractNameFuncName, void* ptr)
{
String instanceName = extractNameFromFunctionName(extractNameFuncName);
InstanceCounter::instance()->decrementInstanceCount(instanceName, ptr);
}
String dumpRefCountedInstanceCounts()
{
return InstanceCounter::instance()->dump();
}
void InstanceCounter::incrementInstanceCount(const String& instanceName, void* ptr)
{
MutexLocker locker(m_mutex);
HashMap<String, int>::AddResult result = m_counterMap.add(instanceName, 1);
if (!result.isNewEntry)
++(result.storedValue->value);
}
void InstanceCounter::decrementInstanceCount(const String& instanceName, void* ptr)
{
MutexLocker locker(m_mutex);
HashMap<String, int>::iterator it = m_counterMap.find(instanceName);
ASSERT(it != m_counterMap.end());
--(it->value);
if (!it->value)
m_counterMap.remove(it);
}
String InstanceCounter::dump()
{
MutexLocker locker(m_mutex);
StringBuilder builder;
builder.append("{");
HashMap<String, int>::iterator it = m_counterMap.begin();
HashMap<String, int>::iterator itEnd = m_counterMap.end();
for (; it != itEnd; ++it) {
if (it != m_counterMap.begin())
builder.append(",");
builder.append("\"");
builder.append(it->key);
builder.append("\": ");
builder.append(String::number(it->value));
}
builder.append("}");
return builder.toString();
}
#else
String dumpRefCountedInstanceCounts()
{
return String("{}");
}
#endif
}