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 WTF_DefaultAllocator_h 32 #define WTF_DefaultAllocator_h 33 34 // This is the allocator that is used for allocations that are not on the 35 // traced, garbage collected heap. It uses FastMalloc for collections, 36 // but uses the partition allocator for the backing store of the collections. 37 38 #include "wtf/Assertions.h" 39 #include "wtf/FastAllocBase.h" 40 #include "wtf/PartitionAlloc.h" 41 #include "wtf/WTF.h" 42 43 #include <string.h> 44 45 namespace WTF { 46 47 class DefaultAllocatorDummyVisitor; 48 49 class DefaultAllocatorQuantizer { 50 public: 51 template<typename T> quantizedSize(size_t count)52 static size_t quantizedSize(size_t count) 53 { 54 RELEASE_ASSERT(count <= kMaxUnquantizedAllocation / sizeof(T)); 55 return partitionAllocActualSize(Partitions::getBufferPartition(), count * sizeof(T)); 56 } 57 static const size_t kMaxUnquantizedAllocation = kGenericMaxDirectMapped; 58 }; 59 60 class DefaultAllocator { 61 public: 62 typedef DefaultAllocatorQuantizer Quantizer; 63 typedef DefaultAllocatorDummyVisitor Visitor; 64 static const bool isGarbageCollected = false; 65 template<typename T, typename Traits> 66 struct VectorBackingHelper { 67 typedef void Type; 68 }; 69 template<typename T> 70 struct HashTableBackingHelper { 71 typedef void Type; 72 }; 73 template <typename Return, typename Metadata> backingMalloc(size_t size)74 static Return backingMalloc(size_t size) 75 { 76 return reinterpret_cast<Return>(backingAllocate(size)); 77 } 78 template <typename Return, typename Metadata> zeroedBackingMalloc(size_t size)79 static Return zeroedBackingMalloc(size_t size) 80 { 81 void* result = backingAllocate(size); 82 memset(result, 0, size); 83 return reinterpret_cast<Return>(result); 84 } 85 template <typename Return, typename Metadata> malloc(size_t size)86 static Return malloc(size_t size) 87 { 88 return reinterpret_cast<Return>(fastMalloc(size)); 89 } 90 WTF_EXPORT static void backingFree(void* address); free(void * address)91 static void free(void* address) 92 { 93 fastFree(address); 94 } 95 template<typename T> newArray(size_t bytes)96 static void* newArray(size_t bytes) 97 { 98 return malloc<void*, void>(bytes); 99 } 100 static void deleteArray(void * ptr)101 deleteArray(void* ptr) 102 { 103 free(ptr); // Not the system free, the one from this class. 104 } 105 isAllocationAllowed()106 static bool isAllocationAllowed() { return true; } 107 markNoTracing(...)108 static void markNoTracing(...) 109 { 110 ASSERT_NOT_REACHED(); 111 } 112 registerDelayedMarkNoTracing(...)113 static void registerDelayedMarkNoTracing(...) 114 { 115 ASSERT_NOT_REACHED(); 116 } 117 registerWeakMembers(...)118 static void registerWeakMembers(...) 119 { 120 ASSERT_NOT_REACHED(); 121 } 122 registerWeakTable(...)123 static void registerWeakTable(...) 124 { 125 ASSERT_NOT_REACHED(); 126 } 127 128 #if ENABLE(ASSERT) weakTableRegistered(...)129 static bool weakTableRegistered(...) 130 { 131 ASSERT_NOT_REACHED(); 132 return false; 133 } 134 #endif 135 136 template<typename T, typename Traits> trace(...)137 static void trace(...) 138 { 139 ASSERT_NOT_REACHED(); 140 } 141 142 template<typename T> 143 struct OtherType { 144 typedef T* Type; 145 }; 146 147 template<typename T> getOther(T * other)148 static T& getOther(T* other) 149 { 150 return *other; 151 } 152 enterNoAllocationScope()153 static void enterNoAllocationScope() { } leaveNoAllocationScope()154 static void leaveNoAllocationScope() { } 155 156 private: 157 WTF_EXPORT static void* backingAllocate(size_t); 158 }; 159 160 // The Windows compiler seems to be very eager to instantiate things it won't 161 // need, so unless we have this class we get compile errors. 162 class DefaultAllocatorDummyVisitor { 163 public: isAlive(T obj)164 template<typename T> inline bool isAlive(T obj) 165 { 166 ASSERT_NOT_REACHED(); 167 return false; 168 } 169 }; 170 171 } // namespace WTF 172 173 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \ 174 public: \ 175 void* operator new(size_t size) \ 176 { \ 177 return Allocator::template malloc<void*, ClassName>(size); \ 178 } \ 179 void operator delete(void* p) { Allocator::free(p); } \ 180 void* operator new[](size_t size) { return Allocator::template newArray<ClassName>(size); } \ 181 void operator delete[](void* p) { Allocator::deleteArray(p); } \ 182 void* operator new(size_t, NotNullTag, void* location) \ 183 { \ 184 ASSERT(location); \ 185 return location; \ 186 } \ 187 private: \ 188 typedef int __thisIsHereToForceASemicolonAfterThisMacro 189 190 using WTF::DefaultAllocator; 191 192 #endif // WTF_DefaultAllocator_h 193