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.
31 #ifndef PurgeableVector_h
32 #define PurgeableVector_h
34 #include "platform/PlatformExport.h"
35 #include "wtf/Forward.h"
36 #include "wtf/Noncopyable.h"
37 #include "wtf/Vector.h"
41 class WebDiscardableMemory
;
43 // A simple vector implementation that supports purgeable memory. The vector is
44 // already locked at construction and locking uses an internal counter which
45 // means that N calls to lock() must be followed by N+1 calls to unlock() to
46 // actually make the vector purgeable.
47 class PLATFORM_EXPORT PurgeableVector
{
48 WTF_MAKE_NONCOPYABLE(PurgeableVector
);
50 enum PurgeableOption
{
55 // Clients who know in advance that they will call unlock() should construct
56 // the instance with the Purgeable option so that the instance uses
57 // discardable memory from the start and unlock() doesn't cause a memcpy().
58 PurgeableVector(PurgeableOption
= Purgeable
);
62 // WARNING: This causes a memcpy() if the instance was constructed with the
63 // Purgeable hint or had its internal vector moved to discardable memory
64 // after a call to unlock().
65 void adopt(Vector
<char>& other
);
67 void append(const char* data
, size_t length
);
73 // The instance must be locked before calling this.
78 // Returns whether the memory is still resident.
81 // WARNING: Calling unlock() on an instance that wasn't created with the
82 // Purgeable option does an extra memcpy().
85 bool isLocked() const;
87 // Note that this method should be used carefully since it may not use
88 // exponential growth internally. This means that repeated/invalid uses of
89 // it can result in O(N^2) append(). If you don't exactly know what you are
90 // doing then you should probably not call this method.
91 void reserveCapacity(size_t capacity
);
94 enum PurgeableAllocationStrategy
{
99 // Copies data from the discardable buffer to the vector and clears the
100 // discardable buffer.
101 void moveDataFromDiscardableToVector();
103 void clearDiscardable();
105 bool reservePurgeableCapacity(size_t capacity
, PurgeableAllocationStrategy
);
107 size_t adjustPurgeableCapacity(size_t capacity
) const;
109 // Vector used when the instance is constructed without the purgeability
110 // hint or when discardable memory allocation fails.
111 // Note that there can't be data both in |m_vector| and
112 // |m_discardable|, i.e. only one of them is used at a given time.
113 Vector
<char> m_vector
;
114 OwnPtr
<WebDiscardableMemory
> m_discardable
;
115 size_t m_discardableCapacity
;
116 size_t m_discardableSize
;
123 #endif // PurgeableVector_h