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