OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 28 matching lines...) Expand all Loading... |
39 #include <memory> | 39 #include <memory> |
40 | 40 |
41 namespace base { | 41 namespace base { |
42 class DiscardableMemory; | 42 class DiscardableMemory; |
43 } | 43 } |
44 | 44 |
45 namespace blink { | 45 namespace blink { |
46 | 46 |
47 class WebProcessMemoryDump; | 47 class WebProcessMemoryDump; |
48 | 48 |
49 // A simple vector implementation that supports purgeable memory. The vector is | 49 // A simple vector implementation that supports purgeable memory. |
50 // already locked at construction and locking uses an internal counter which | 50 // TODO(hiroshige): PurgeableVector is about to be removed and some |
51 // means that N calls to lock() must be followed by N+1 calls to unlock() to | 51 // functionality has already been removed https://crbug.com/603791. |
52 // actually make the vector purgeable. | |
53 class PLATFORM_EXPORT PurgeableVector { | 52 class PLATFORM_EXPORT PurgeableVector { |
54 DISALLOW_NEW(); | 53 DISALLOW_NEW(); |
55 WTF_MAKE_NONCOPYABLE(PurgeableVector); | 54 WTF_MAKE_NONCOPYABLE(PurgeableVector); |
56 public: | 55 public: |
57 enum PurgeableOption { | 56 enum PurgeableOption { |
58 NotPurgeable, | 57 NotPurgeable, |
59 Purgeable, | 58 Purgeable, |
60 }; | 59 }; |
61 | 60 |
62 // Clients who know in advance that they will call unlock() should construct | |
63 // the instance with the Purgeable option so that the instance uses | |
64 // discardable memory from the start and unlock() doesn't cause a memcpy(). | |
65 PurgeableVector(PurgeableOption = Purgeable); | 61 PurgeableVector(PurgeableOption = Purgeable); |
66 | 62 |
67 ~PurgeableVector(); | 63 ~PurgeableVector(); |
68 | 64 |
69 // WARNING: This causes a memcpy() if the instance was constructed with the | |
70 // Purgeable hint or had its internal vector moved to discardable memory | |
71 // after a call to unlock(). | |
72 void adopt(Vector<char>& other); | 65 void adopt(Vector<char>& other); |
73 | 66 |
74 void append(const char* data, size_t length); | 67 void append(const char* data, size_t length); |
75 | 68 |
76 void grow(size_t); | 69 void grow(size_t); |
77 | 70 |
78 void clear(); | 71 void clear(); |
79 | 72 |
80 // The instance must be locked before calling this. | |
81 char* data(); | 73 char* data(); |
82 | 74 |
83 size_t size() const; | 75 size_t size() const; |
84 | 76 |
85 // Returns whether the memory is still resident. | |
86 bool lock(); | |
87 | |
88 // WARNING: Calling unlock() on an instance that wasn't created with the | |
89 // Purgeable option does an extra memcpy(). | |
90 void unlock(); | |
91 | |
92 bool isLocked() const; | |
93 | |
94 // Note that this method should be used carefully since it may not use | 77 // Note that this method should be used carefully since it may not use |
95 // exponential growth internally. This means that repeated/invalid uses of | 78 // exponential growth internally. This means that repeated/invalid uses of |
96 // it can result in O(N^2) append(). If you don't exactly know what you are | 79 // it can result in O(N^2) append(). If you don't exactly know what you are |
97 // doing then you should probably not call this method. | 80 // doing then you should probably not call this method. |
98 void reserveCapacity(size_t capacity); | 81 void reserveCapacity(size_t capacity); |
99 | 82 |
100 void onMemoryDump(const String& dumpPrefix, WebProcessMemoryDump*) const; | 83 void onMemoryDump(const String& dumpPrefix, WebProcessMemoryDump*) const; |
101 | 84 |
102 private: | 85 private: |
103 enum PurgeableAllocationStrategy { | 86 enum PurgeableAllocationStrategy { |
(...skipping 13 matching lines...) Expand all Loading... |
117 | 100 |
118 // Vector used when the instance is constructed without the purgeability | 101 // Vector used when the instance is constructed without the purgeability |
119 // hint or when discardable memory allocation fails. | 102 // hint or when discardable memory allocation fails. |
120 // Note that there can't be data both in |m_vector| and | 103 // Note that there can't be data both in |m_vector| and |
121 // |m_discardable|, i.e. only one of them is used at a given time. | 104 // |m_discardable|, i.e. only one of them is used at a given time. |
122 Vector<char> m_vector; | 105 Vector<char> m_vector; |
123 std::unique_ptr<base::DiscardableMemory> m_discardable; | 106 std::unique_ptr<base::DiscardableMemory> m_discardable; |
124 size_t m_discardableCapacity; | 107 size_t m_discardableCapacity; |
125 size_t m_discardableSize; | 108 size_t m_discardableSize; |
126 bool m_isPurgeable; | 109 bool m_isPurgeable; |
127 int m_locksCount; | |
128 }; | 110 }; |
129 | 111 |
130 } // namespace blink | 112 } // namespace blink |
131 | 113 |
132 #endif // PurgeableVector_h | 114 #endif // PurgeableVector_h |
OLD | NEW |