1 /* 2 * Copyright (C) 2014 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31 #ifndef PurgeableVector_h 32 #define PurgeableVector_h 33 34 #include "platform/PlatformExport.h" 35 #include "wtf/Forward.h" 36 #include "wtf/Noncopyable.h" 37 #include "wtf/Vector.h" 38 39 namespace blink { 40 41 class WebDiscardableMemory; 42 43 } // namespace blink 44 45 namespace WebCore { 46 47 // A simple vector implementation that supports purgeable memory. The vector is 48 // already locked at construction and locking uses an internal counter which 49 // means that N calls to lock() must be followed by N+1 calls to unlock() to 50 // actually make the vector purgeable. 51 class PLATFORM_EXPORT PurgeableVector { 52 WTF_MAKE_NONCOPYABLE(PurgeableVector); 53 public: 54 enum PurgeableOption { 55 NotPurgeable, 56 Purgeable, 57 }; 58 59 // Clients who know in advance that they will call unlock() should construct 60 // the instance with the Purgeable option so that the instance uses 61 // discardable memory from the start and unlock() doesn't cause a memcpy(). 62 PurgeableVector(PurgeableOption = Purgeable); 63 64 ~PurgeableVector(); 65 66 // WARNING: This causes a memcpy() if the instance was constructed with the 67 // Purgeable hint or had its internal vector moved to discardable memory 68 // after a call to unlock(). 69 void adopt(Vector<char>& other); 70 71 void append(const char* data, size_t length); 72 73 void grow(size_t); 74 75 void clear(); 76 77 // The instance must be locked before calling this. 78 char* data(); 79 80 size_t size() const; 81 82 // Returns whether the memory is still resident. 83 bool lock(); 84 85 // WARNING: Calling unlock() on an instance that wasn't created with the 86 // Purgeable option does an extra memcpy(). 87 void unlock(); 88 89 bool isLocked() const; 90 91 // Note that this method should be used carefully since it may not use 92 // exponential growth internally. This means that repeated/invalid uses of 93 // it can result in O(N^2) append(). If you don't exactly know what you are 94 // doing then you should probably not call this method. 95 void reserveCapacity(size_t capacity); 96 97 private: 98 enum PurgeableAllocationStrategy { 99 UseExactCapacity, 100 UseExponentialGrowth, 101 }; 102 103 // Copies data from the discardable buffer to the vector and clears the 104 // discardable buffer. 105 void moveDataFromDiscardableToVector(); 106 107 void clearDiscardable(); 108 109 bool reservePurgeableCapacity(size_t capacity, PurgeableAllocationStrategy); 110 111 size_t adjustPurgeableCapacity(size_t capacity) const; 112 113 // Vector used when the instance is constructed without the purgeability 114 // hint or when discardable memory allocation fails. 115 // Note that there can't be data both in |m_vector| and 116 // |m_discardable|, i.e. only one of them is used at a given time. 117 Vector<char> m_vector; 118 OwnPtr<blink::WebDiscardableMemory> m_discardable; 119 size_t m_discardableCapacity; 120 size_t m_discardableSize; 121 bool m_isPurgeable; 122 int m_locksCount; 123 }; 124 125 } // namespace WebCore 126 127 #endif // PurgeableVector_h 128