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