Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / Partitions.h
blob276380472bd64b9e5beed85d8c9c172309b45d2d
1 /*
2 * Copyright (C) 2013 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 Partitions_h
32 #define Partitions_h
34 #include "wtf/PartitionAlloc.h"
35 #include "wtf/WTF.h"
36 #include "wtf/WTFExport.h"
38 namespace WTF {
40 class WTF_EXPORT Partitions {
41 public:
42 static void initialize();
43 // TODO(bashi): Remove this function and make initialize() take
44 // HistogramEnumerationFunction when we can make sure that WTF::initialize()
45 // is called before using this class.
46 static void setHistogramEnumeration(HistogramEnumerationFunction);
47 static void shutdown();
48 ALWAYS_INLINE static PartitionRootGeneric* bufferPartition()
50 // TODO(haraken): This check is needed because some call sites allocate
51 // Blink things before WTF::initialize(). We should fix those call sites
52 // and remove the check.
53 if (UNLIKELY(!s_initialized))
54 initialize();
55 return m_bufferAllocator.root();
58 ALWAYS_INLINE static PartitionRootGeneric* fastMallocPartition()
60 // TODO(haraken): This check is needed because some call sites allocate
61 // Blink things before WTF::initialize(). We should fix those call sites
62 // and remove the check.
63 if (UNLIKELY(!s_initialized))
64 initialize();
65 return m_fastMallocAllocator.root();
68 ALWAYS_INLINE static PartitionRoot* nodePartition()
70 ASSERT(s_initialized);
71 return m_nodeAllocator.root();
73 ALWAYS_INLINE static PartitionRoot* layoutPartition()
75 ASSERT(s_initialized);
76 return m_layoutAllocator.root();
79 static size_t currentDOMMemoryUsage()
81 ASSERT(s_initialized);
82 return m_nodeAllocator.root()->totalSizeOfCommittedPages;
85 static size_t totalSizeOfCommittedPages()
87 size_t totalSize = 0;
88 totalSize += m_fastMallocAllocator.root()->totalSizeOfCommittedPages;
89 totalSize += m_bufferAllocator.root()->totalSizeOfCommittedPages;
90 totalSize += m_nodeAllocator.root()->totalSizeOfCommittedPages;
91 totalSize += m_layoutAllocator.root()->totalSizeOfCommittedPages;
92 return totalSize;
95 static void decommitFreeableMemory();
97 static void reportMemoryUsageHistogram();
99 static void dumpMemoryStats(bool isLightDump, PartitionStatsDumper*);
101 ALWAYS_INLINE static void* bufferMalloc(size_t n)
103 return partitionAllocGeneric(bufferPartition(), n);
106 ALWAYS_INLINE static void* bufferRealloc(void* p, size_t n)
108 return partitionReallocGeneric(bufferPartition(), p, n);
111 ALWAYS_INLINE static void bufferFree(void* p)
113 partitionFreeGeneric(bufferPartition(), p);
116 ALWAYS_INLINE static size_t bufferActualSize(size_t n)
118 return partitionAllocActualSize(bufferPartition(), n);
121 static void handleOutOfMemory();
123 private:
124 static int s_initializationLock;
125 static bool s_initialized;
127 // We have the following four partitions.
128 // - Node partition: A partition to allocate Nodes. We prepare a
129 // dedicated partition for Nodes because Nodes are likely to be
130 // a source of use-after-frees. Another reason is for performance:
131 // Since Nodes are guaranteed to be used only by the main
132 // thread, we can bypass acquiring a lock. Also we can improve memory
133 // locality by putting Nodes together.
134 // - Layout object partition: A partition to allocate LayoutObjects.
135 // we prepare a dedicated partition for the same reason as Nodes.
136 // - Buffer partition: A partition to allocate objects that have a strong
137 // risk where the length and/or the contents are exploited from user
138 // scripts. Vectors, HashTables, ArrayBufferContents and Strings are
139 // allocated in the buffer partition.
140 // - Fast malloc partition: A partition to allocate all other objects.
141 static PartitionAllocatorGeneric m_fastMallocAllocator;
142 static PartitionAllocatorGeneric m_bufferAllocator;
143 static SizeSpecificPartitionAllocator<3328> m_nodeAllocator;
144 static SizeSpecificPartitionAllocator<1024> m_layoutAllocator;
145 static HistogramEnumerationFunction m_histogramEnumeration;
148 } // namespace WTF
150 #endif // Partitions_h