2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/PurgeableVector.h"
34 #include "public/platform/Platform.h"
35 #include "public/platform/WebDiscardableMemory.h"
36 #include "wtf/Assertions.h"
37 #include "wtf/OwnPtr.h"
38 #include "wtf/PassOwnPtr.h"
44 // WebDiscardableMemory allocations are expensive and page-grained. We only use
45 // them when there's a reasonable amount of memory to be saved by the OS
46 // discarding the memory.
47 static const size_t minimumDiscardableAllocationSize = 4 * 4096;
49 PurgeableVector::PurgeableVector(PurgeableOption purgeable)
50 : m_discardableCapacity(0)
51 , m_discardableSize(0)
52 , m_isPurgeable(purgeable == Purgeable)
53 , m_locksCount(1) // The buffer is locked at creation.
57 PurgeableVector::~PurgeableVector()
61 void PurgeableVector::reserveCapacity(size_t capacity)
66 if (reservePurgeableCapacity(capacity, UseExactCapacity))
68 // Fallback to non-purgeable buffer allocation in case discardable memory allocation failed.
71 if (!m_vector.capacity()) {
72 // Using reserveInitialCapacity() on the underlying vector ensures that the vector uses the
73 // exact specified capacity to avoid consuming too much memory for small resources.
74 m_vector.reserveInitialCapacity(capacity);
76 m_vector.reserveCapacity(capacity);
79 moveDataFromDiscardableToVector();
82 void PurgeableVector::moveDataFromDiscardableToVector()
85 m_vector.append(static_cast<const char*>(m_discardable->data()), m_discardableSize);
90 void PurgeableVector::clearDiscardable()
92 m_discardable.clear();
93 m_discardableCapacity = 0;
94 m_discardableSize = 0;
97 void PurgeableVector::append(const char* data, size_t length)
101 if (!m_isPurgeable) {
102 m_vector.append(data, length);
106 const size_t currentSize = m_discardable ? m_discardableSize : m_vector.size();
107 const size_t newBufferSize = currentSize + length;
109 if (!reservePurgeableCapacity(newBufferSize, UseExponentialGrowth)) {
110 moveDataFromDiscardableToVector();
111 m_vector.append(data, length);
115 ASSERT(m_discardableSize + length <= m_discardableCapacity);
116 memcpy(static_cast<char*>(m_discardable->data()) + m_discardableSize, data, length);
117 m_discardableSize += length;
120 void PurgeableVector::grow(size_t newSize)
122 ASSERT(newSize >= size());
125 if (reservePurgeableCapacity(newSize, UseExponentialGrowth)) {
126 m_discardableSize = newSize;
129 moveDataFromDiscardableToVector();
132 m_vector.resize(newSize);
135 void PurgeableVector::clear()
141 char* PurgeableVector::data()
144 return m_discardable ? static_cast<char*>(m_discardable->data()) : m_vector.data();
147 size_t PurgeableVector::size() const
149 return m_discardable ? m_discardableSize : m_vector.size();
152 void PurgeableVector::adopt(Vector<char>& other)
157 if (!m_isPurgeable) {
158 m_vector.swap(other);
165 append(other.data(), other.size());
169 bool PurgeableVector::lock()
172 if (m_locksCount > 1)
175 ASSERT(m_locksCount == 1);
179 return m_discardable->lock();
182 void PurgeableVector::unlock()
186 if (m_locksCount > 0)
189 if (!m_vector.isEmpty()) {
190 ASSERT(!m_discardable);
191 m_isPurgeable = true;
192 if (!reservePurgeableCapacity(m_vector.size(), UseExactCapacity))
197 m_discardable->unlock();
200 bool PurgeableVector::isLocked() const
202 ASSERT(m_locksCount >= 0);
203 return m_locksCount > 0;
206 bool PurgeableVector::reservePurgeableCapacity(size_t capacity, PurgeableAllocationStrategy allocationStrategy)
208 ASSERT(m_isPurgeable);
210 if (m_discardable && m_discardableCapacity >= capacity) {
211 ASSERT(!m_vector.capacity());
215 if (capacity < minimumDiscardableAllocationSize)
218 if (allocationStrategy == UseExponentialGrowth)
219 capacity = adjustPurgeableCapacity(capacity);
221 OwnPtr<blink::WebDiscardableMemory> discardable = adoptPtr(
222 blink::Platform::current()->allocateAndLockDiscardableMemory(capacity));
224 // Discardable memory is not supported.
225 m_isPurgeable = false;
229 m_discardableCapacity = capacity;
230 // Copy the data that was either in the previous purgeable buffer or in the vector to the new
233 memcpy(discardable->data(), m_discardable->data(), m_discardableSize);
235 memcpy(discardable->data(), m_vector.data(), m_vector.size());
236 m_discardableSize = m_vector.size();
240 m_discardable.swap(discardable);
241 ASSERT(!m_vector.capacity());
245 size_t PurgeableVector::adjustPurgeableCapacity(size_t capacity) const
247 ASSERT(capacity >= minimumDiscardableAllocationSize);
249 const float growthFactor = 1.5;
250 size_t newCapacity = std::max(capacity, static_cast<size_t>(m_discardableCapacity * growthFactor));
252 // Discardable memory has page-granularity so align to the next page here to minimize
254 // Since the page size is only used below to minimize fragmentation it's still safe to use it
255 // even if it gets out of sync (e.g. due to the use of huge pages).
256 const size_t kPageSize = 4096;
257 newCapacity = (newCapacity + kPageSize - 1) & ~(kPageSize - 1);
259 return std::max(capacity, newCapacity); // Overflow check.