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); scoped_ptr<base::DiscardableMemory> discardable = base::DiscardableMemoryAllocator::GetInstance()->AllocateLockedDiscardableMemory(capacity); ASSERT(discardable); m_discardableCapacity = capacity; // Copy the data that was either in the previous purgeable buffer or in the vector to the new // purgeable buffer. 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 = std::move(discardable); ASSERT(!m_vector.capacity()); return true; }
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<WebDiscardableMemory> discardable = adoptPtr( Platform::current()->allocateAndLockDiscardableMemory(capacity)); if (!discardable) { // Discardable memory is not supported. m_isPurgeable = false; return false; } m_discardableCapacity = capacity; // Copy the data that was either in the previous purgeable buffer or in the vector to the new // purgeable buffer. 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; }