OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 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 Partitions_h | |
32 #define Partitions_h | |
33 | |
34 #include "wtf/WTF.h" | |
35 #include "wtf/WTFExport.h" | |
36 #include "wtf/allocator/PartitionAlloc.h" | |
37 #include <string.h> | |
38 | |
39 namespace WTF { | |
40 | |
41 class WTF_EXPORT Partitions { | |
42 public: | |
43 typedef void (*ReportPartitionAllocSizeFunction)(size_t); | |
44 | |
45 // Name of allocator used by tracing for marking sub-allocations while take | |
46 // memory snapshots. | |
47 static const char* const kAllocatedObjectPoolName; | |
48 | |
49 static void initialize(ReportPartitionAllocSizeFunction); | |
50 static void shutdown(); | |
51 ALWAYS_INLINE static PartitionRootGeneric* bufferPartition() { | |
52 ASSERT(s_initialized); | |
53 return m_bufferAllocator.root(); | |
54 } | |
55 | |
56 ALWAYS_INLINE static PartitionRootGeneric* fastMallocPartition() { | |
57 ASSERT(s_initialized); | |
58 return m_fastMallocAllocator.root(); | |
59 } | |
60 | |
61 ALWAYS_INLINE static PartitionRoot* nodePartition() { | |
62 ASSERT_NOT_REACHED(); | |
63 return nullptr; | |
64 } | |
65 ALWAYS_INLINE static PartitionRoot* layoutPartition() { | |
66 ASSERT(s_initialized); | |
67 return m_layoutAllocator.root(); | |
68 } | |
69 | |
70 static size_t currentDOMMemoryUsage() { | |
71 ASSERT(s_initialized); | |
72 ASSERT_NOT_REACHED(); | |
73 return 0; | |
74 } | |
75 | |
76 static size_t totalSizeOfCommittedPages() { | |
77 size_t totalSize = 0; | |
78 totalSize += m_fastMallocAllocator.root()->totalSizeOfCommittedPages; | |
79 totalSize += m_bufferAllocator.root()->totalSizeOfCommittedPages; | |
80 totalSize += m_layoutAllocator.root()->totalSizeOfCommittedPages; | |
81 return totalSize; | |
82 } | |
83 | |
84 static void decommitFreeableMemory(); | |
85 | |
86 static void reportMemoryUsageHistogram(); | |
87 | |
88 static void dumpMemoryStats(bool isLightDump, PartitionStatsDumper*); | |
89 | |
90 ALWAYS_INLINE static void* bufferMalloc(size_t n, const char* typeName) { | |
91 return partitionAllocGeneric(bufferPartition(), n, typeName); | |
92 } | |
93 ALWAYS_INLINE static void* bufferRealloc(void* p, | |
94 size_t n, | |
95 const char* typeName) { | |
96 return partitionReallocGeneric(bufferPartition(), p, n, typeName); | |
97 } | |
98 ALWAYS_INLINE static void bufferFree(void* p) { | |
99 partitionFreeGeneric(bufferPartition(), p); | |
100 } | |
101 ALWAYS_INLINE static size_t bufferActualSize(size_t n) { | |
102 return partitionAllocActualSize(bufferPartition(), n); | |
103 } | |
104 static void* fastMalloc(size_t n, const char* typeName) { | |
105 return partitionAllocGeneric(Partitions::fastMallocPartition(), n, | |
106 typeName); | |
107 } | |
108 static void* fastZeroedMalloc(size_t n, const char* typeName) { | |
109 void* result = fastMalloc(n, typeName); | |
110 memset(result, 0, n); | |
111 return result; | |
112 } | |
113 static void* fastRealloc(void* p, size_t n, const char* typeName) { | |
114 return partitionReallocGeneric(Partitions::fastMallocPartition(), p, n, | |
115 typeName); | |
116 } | |
117 static void fastFree(void* p) { | |
118 partitionFreeGeneric(Partitions::fastMallocPartition(), p); | |
119 } | |
120 | |
121 static void handleOutOfMemory(); | |
122 | |
123 private: | |
124 static SpinLock s_initializationLock; | |
125 static bool s_initialized; | |
126 | |
127 // We have the following four partitions. | |
128 // - LayoutObject partition: A partition to allocate LayoutObjects. | |
129 // We prepare a dedicated partition for LayoutObjects because they | |
130 // are likely to be a source of use-after-frees. Another reason | |
131 // is for performance: As LayoutObjects are guaranteed to only be used | |
132 // by the main thread, we can bypass acquiring a lock. Also we can | |
133 // improve memory locality by putting LayoutObjects together. | |
134 // - Buffer partition: A partition to allocate objects that have a strong | |
135 // risk where the length and/or the contents are exploited from user | |
136 // scripts. Vectors, HashTables, ArrayBufferContents and Strings are | |
137 // allocated in the buffer partition. | |
138 // - Fast malloc partition: A partition to allocate all other objects. | |
139 static PartitionAllocatorGeneric m_fastMallocAllocator; | |
140 static PartitionAllocatorGeneric m_bufferAllocator; | |
141 static SizeSpecificPartitionAllocator<1024> m_layoutAllocator; | |
142 static ReportPartitionAllocSizeFunction m_reportSizeFunction; | |
143 }; | |
144 | |
145 } // namespace WTF | |
146 | |
147 #endif // Partitions_h | |
OLD | NEW |