Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / PurgeableVector.h
blobc1734f85c3afa45f12e575e74c6423297f32ad96
1 /*
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
6 * met:
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.
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"
39 namespace blink {
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);
49 public:
50 enum PurgeableOption {
51 NotPurgeable,
52 Purgeable,
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);
60 ~PurgeableVector();
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);
69 void grow(size_t);
71 void clear();
73 // The instance must be locked before calling this.
74 char* data();
76 size_t size() const;
78 // Returns whether the memory is still resident.
79 bool lock();
81 // WARNING: Calling unlock() on an instance that wasn't created with the
82 // Purgeable option does an extra memcpy().
83 void unlock();
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);
93 private:
94 enum PurgeableAllocationStrategy {
95 UseExactCapacity,
96 UseExponentialGrowth,
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;
117 bool m_isPurgeable;
118 int m_locksCount;
121 } // namespace blink
123 #endif // PurgeableVector_h