blob: 99fe46a6748eb5151e5e8854e0d12bed90b20d98 [file] [log] [blame]
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef MemoryInstrumentation_h
#define MemoryInstrumentation_h
#include <wtf/Forward.h>
#include <wtf/OwnPtr.h>
#include <wtf/RefPtr.h>
namespace WebCore {
class MemoryObjectInfo;
class MemoryInstrumentation {
public:
virtual ~MemoryInstrumentation() { }
enum ObjectType {
Other,
DOM,
CSS,
LastTypeEntry
};
template <typename T> void reportInstrumentedObject(const T&);
template <typename T> void reportObject(const T&) { }
template <typename T> void reportInstrumentedPointer(const T*);
template <typename T> void reportPointer(const T* object, ObjectType objectType)
{
if (!object || visited(object))
return;
countObjectSize(objectType, sizeof(T));
}
private:
friend class MemoryObjectInfo;
virtual void reportString(ObjectType, const String&) = 0;
virtual void countObjectSize(ObjectType, size_t) = 0;
virtual bool visited(const void*) = 0;
};
class MemoryObjectInfo {
public:
MemoryObjectInfo(MemoryInstrumentation* memoryInstrumentation)
: m_memoryInstrumentation(memoryInstrumentation)
, m_objectType(MemoryInstrumentation::Other)
, m_objectSize(0)
{ }
template <typename P>
void reportInstrumentedPointer(const P* memberPointer)
{
m_memoryInstrumentation->reportInstrumentedPointer(memberPointer);
}
template <typename P>
void reportPointer(const P* pointer, MemoryInstrumentation::ObjectType objectType)
{
m_memoryInstrumentation->reportPointer(pointer, objectType);
}
template <typename T>
void reportInstrumentedObject(const T& memberObject)
{
m_memoryInstrumentation->reportInstrumentedObject(memberObject);
}
template <typename T>
void reportObject(const T& object) { m_memoryInstrumentation->reportObject(object); }
template <typename T>
void reportObjectInfo(const T*, MemoryInstrumentation::ObjectType objectType)
{
if (m_objectType != MemoryInstrumentation::Other)
return;
m_objectType = objectType;
m_objectSize = sizeof(T);
}
void reportString(const String& string)
{
m_memoryInstrumentation->reportString(objectType(), string);
}
MemoryInstrumentation::ObjectType objectType() const { return m_objectType; }
size_t objectSize() const { return m_objectSize; }
private:
MemoryInstrumentation* m_memoryInstrumentation;
MemoryInstrumentation::ObjectType m_objectType;
size_t m_objectSize;
};
template <typename T>
void MemoryInstrumentation::reportInstrumentedPointer(const T* const object)
{
if (!object || visited(object))
return;
MemoryObjectInfo memoryObjectInfo(this);
object->reportMemoryUsage(&memoryObjectInfo);
countObjectSize(memoryObjectInfo.objectType(), memoryObjectInfo.objectSize());
}
template<typename T>
void MemoryInstrumentation::reportInstrumentedObject(const T& object)
{
if (visited(&object))
return;
MemoryObjectInfo memoryObjectInfo(this);
object.reportMemoryUsage(&memoryObjectInfo);
}
} // namespace WebCore
#endif // !defined(MemoryInstrumentation_h)