Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / PurgeableVector.h
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 // 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);
49 public:
50     enum PurgeableOption {
51         NotPurgeable,
52         Purgeable,
53     };
54
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);
59
60     ~PurgeableVector();
61
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);
66
67     void append(const char* data, size_t length);
68
69     void grow(size_t);
70
71     void clear();
72
73     // The instance must be locked before calling this.
74     char* data();
75
76     size_t size() const;
77
78     // Returns whether the memory is still resident.
79     bool lock();
80
81     // WARNING: Calling unlock() on an instance that wasn't created with the
82     // Purgeable option does an extra memcpy().
83     void unlock();
84
85     bool isLocked() const;
86
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);
92
93 private:
94     enum PurgeableAllocationStrategy {
95         UseExactCapacity,
96         UseExponentialGrowth,
97     };
98
99     // Copies data from the discardable buffer to the vector and clears the
100     // discardable buffer.
101     void moveDataFromDiscardableToVector();
102
103     void clearDiscardable();
104
105     bool reservePurgeableCapacity(size_t capacity, PurgeableAllocationStrategy);
106
107     size_t adjustPurgeableCapacity(size_t capacity) const;
108
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;
117     bool m_isPurgeable;
118     int m_locksCount;
119 };
120
121 } // namespace blink
122
123 #endif // PurgeableVector_h