DocumentSharedObjectPool.cpp [plain text]
#include "config.h"
#include "DocumentSharedObjectPool.h"
#include "Element.h"
namespace WebCore {
class ShareableElementDataCacheKey {
public:
ShareableElementDataCacheKey(const Attribute* attributes, unsigned attributeCount)
: m_attributes(attributes)
, m_attributeCount(attributeCount)
{ }
bool operator!=(const ShareableElementDataCacheKey& other) const
{
if (m_attributeCount != other.m_attributeCount)
return true;
return memcmp(m_attributes, other.m_attributes, sizeof(Attribute) * m_attributeCount);
}
unsigned hash() const
{
return StringHasher::hashMemory(m_attributes, m_attributeCount * sizeof(Attribute));
}
private:
const Attribute* m_attributes;
unsigned m_attributeCount;
};
class ShareableElementDataCacheEntry {
public:
ShareableElementDataCacheEntry(const ShareableElementDataCacheKey& k, PassRefPtr<ShareableElementData> v)
: key(k)
, value(v)
{ }
ShareableElementDataCacheKey key;
RefPtr<ShareableElementData> value;
};
PassRefPtr<ShareableElementData> DocumentSharedObjectPool::cachedShareableElementDataWithAttributes(const Vector<Attribute>& attributes)
{
ASSERT(!attributes.isEmpty());
ShareableElementDataCacheKey cacheKey(attributes.data(), attributes.size());
unsigned cacheHash = cacheKey.hash();
ShareableElementDataCache::iterator cacheIterator = m_shareableElementDataCache.add(cacheHash, nullptr).iterator;
if (cacheIterator->value && cacheIterator->value->key != cacheKey)
cacheHash = 0;
RefPtr<ShareableElementData> elementData;
if (cacheHash && cacheIterator->value)
elementData = cacheIterator->value->value;
else
elementData = ShareableElementData::createWithAttributes(attributes);
if (!cacheHash || cacheIterator->value)
return elementData.release();
cacheIterator->value = adoptPtr(new ShareableElementDataCacheEntry(ShareableElementDataCacheKey(elementData->m_attributeArray, elementData->length()), elementData));
return elementData.release();
}
DocumentSharedObjectPool::DocumentSharedObjectPool()
{
}
DocumentSharedObjectPool::~DocumentSharedObjectPool()
{
}
}