This source file includes following definitions.
- m_locksCount
- reserveCapacity
- moveDataFromDiscardableToVector
- clearDiscardable
- append
- grow
- clear
- data
- adopt
- lock
- unlock
- isLocked
- reservePurgeableCapacity
- adjustPurgeableCapacity
#include "config.h"
#include "platform/PurgeableVector.h"
#include "public/platform/Platform.h"
#include "public/platform/WebDiscardableMemory.h"
#include "wtf/Assertions.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
#include <cstring>
namespace WebCore {
static const size_t minimumDiscardableAllocationSize = 4 * 4096;
PurgeableVector::PurgeableVector(PurgeableOption purgeable)
: m_discardableCapacity(0)
, m_discardableSize(0)
, m_isPurgeable(purgeable == Purgeable)
, m_locksCount(1)
{
}
PurgeableVector::~PurgeableVector()
{
}
void PurgeableVector::reserveCapacity(size_t capacity)
{
ASSERT(isLocked());
if (m_isPurgeable) {
if (reservePurgeableCapacity(capacity, UseExactCapacity))
return;
}
if (!m_vector.capacity()) {
m_vector.reserveInitialCapacity(capacity);
} else {
m_vector.reserveCapacity(capacity);
}
moveDataFromDiscardableToVector();
}
void PurgeableVector::moveDataFromDiscardableToVector()
{
if (m_discardable) {
m_vector.append(static_cast<const char*>(m_discardable->data()), m_discardableSize);
clearDiscardable();
}
}
void PurgeableVector::clearDiscardable()
{
m_discardable.clear();
m_discardableCapacity = 0;
m_discardableSize = 0;
}
void PurgeableVector::append(const char* data, size_t length)
{
ASSERT(isLocked());
if (!m_isPurgeable) {
m_vector.append(data, length);
return;
}
const size_t currentSize = m_discardable ? m_discardableSize : m_vector.size();
const size_t newBufferSize = currentSize + length;
if (!reservePurgeableCapacity(newBufferSize, UseExponentialGrowth)) {
moveDataFromDiscardableToVector();
m_vector.append(data, length);
return;
}
ASSERT(m_discardableSize + length <= m_discardableCapacity);
memcpy(static_cast<char*>(m_discardable->data()) + m_discardableSize, data, length);
m_discardableSize += length;
}
void PurgeableVector::grow(size_t newSize)
{
ASSERT(newSize >= size());
if (m_isPurgeable) {
if (reservePurgeableCapacity(newSize, UseExponentialGrowth)) {
m_discardableSize = newSize;
return;
}
moveDataFromDiscardableToVector();
}
m_vector.resize(newSize);
}
void PurgeableVector::clear()
{
clearDiscardable();
m_vector.clear();
}
char* PurgeableVector::data()
{
ASSERT(isLocked());
return m_discardable ? static_cast<char*>(m_discardable->data()) : m_vector.data();
}
size_t PurgeableVector::size() const
{
return m_discardable ? m_discardableSize : m_vector.size();
}
void PurgeableVector::adopt(Vector<char>& other)
{
if (size() > 0)
clear();
if (!m_isPurgeable) {
m_vector.swap(other);
return;
}
if (other.isEmpty())
return;
append(other.data(), other.size());
other.clear();
}
bool PurgeableVector::lock()
{
++m_locksCount;
if (m_locksCount > 1)
return true;
ASSERT(m_locksCount == 1);
if (!m_discardable)
return true;
return m_discardable->lock();
}
void PurgeableVector::unlock()
{
ASSERT(isLocked());
--m_locksCount;
if (m_locksCount > 0)
return;
if (!m_vector.isEmpty()) {
ASSERT(!m_discardable);
m_isPurgeable = true;
if (!reservePurgeableCapacity(m_vector.size(), UseExactCapacity))
return;
}
if (m_discardable)
m_discardable->unlock();
}
bool PurgeableVector::isLocked() const
{
ASSERT(m_locksCount >= 0);
return m_locksCount > 0;
}
bool PurgeableVector::reservePurgeableCapacity(size_t capacity, PurgeableAllocationStrategy allocationStrategy)
{
ASSERT(m_isPurgeable);
if (m_discardable && m_discardableCapacity >= capacity) {
ASSERT(!m_vector.capacity());
return true;
}
if (capacity < minimumDiscardableAllocationSize)
return false;
if (allocationStrategy == UseExponentialGrowth)
capacity = adjustPurgeableCapacity(capacity);
OwnPtr<blink::WebDiscardableMemory> discardable = adoptPtr(
blink::Platform::current()->allocateAndLockDiscardableMemory(capacity));
if (!discardable) {
m_isPurgeable = false;
return false;
}
m_discardableCapacity = capacity;
if (m_discardable) {
memcpy(discardable->data(), m_discardable->data(), m_discardableSize);
} else {
memcpy(discardable->data(), m_vector.data(), m_vector.size());
m_discardableSize = m_vector.size();
m_vector.clear();
}
m_discardable.swap(discardable);
ASSERT(!m_vector.capacity());
return true;
}
size_t PurgeableVector::adjustPurgeableCapacity(size_t capacity) const
{
ASSERT(capacity >= minimumDiscardableAllocationSize);
const float growthFactor = 1.5;
size_t newCapacity = std::max(capacity, static_cast<size_t>(m_discardableCapacity * growthFactor));
const size_t kPageSize = 4096;
newCapacity = (newCapacity + kPageSize - 1) & ~(kPageSize - 1);
return std::max(capacity, newCapacity);
}
}