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
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
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 WTF_DefaultAllocator_h
32 #define WTF_DefaultAllocator_h
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.
38 #include "wtf/Assertions.h"
39 #include "wtf/FastAllocBase.h"
40 #include "wtf/PartitionAlloc.h"
41 #include "wtf/Partitions.h"
47 class DefaultAllocatorDummyVisitor
;
49 class WTF_EXPORT DefaultAllocator
{
51 typedef DefaultAllocatorDummyVisitor Visitor
;
52 static const bool isGarbageCollected
= false;
55 static size_t quantizedSize(size_t count
)
57 RELEASE_ASSERT(count
<= kGenericMaxDirectMapped
/ sizeof(T
));
58 return partitionAllocActualSize(Partitions::bufferPartition(), count
* sizeof(T
));
61 static T
* allocateVectorBacking(size_t size
)
63 return reinterpret_cast<T
*>(allocateBacking(size
));
66 static T
* allocateExpandedVectorBacking(size_t size
)
68 return reinterpret_cast<T
*>(allocateBacking(size
));
70 static void freeVectorBacking(void* address
);
71 static inline bool expandVectorBacking(void*, size_t)
75 static inline bool shrinkVectorBacking(void* address
, size_t quantizedCurrentSize
, size_t quantizedShrunkSize
)
77 // Optimization: if we're downsizing inside the same allocator bucket,
78 // we can skip reallocation.
79 return quantizedCurrentSize
== quantizedShrunkSize
;
82 static T
* allocateInlineVectorBacking(size_t size
) { return allocateVectorBacking
<T
>(size
); }
83 static inline void freeInlineVectorBacking(void* address
) { freeVectorBacking(address
); }
84 static inline bool expandInlineVectorBacking(void*, size_t) { return false; }
85 static inline bool shrinkInlineVectorBacking(void* address
, size_t quantizedCurrentSize
, size_t quantizedShrunkSize
) { return shrinkVectorBacking(address
, quantizedCurrentSize
, quantizedShrunkSize
); }
87 template <typename T
, typename HashTable
>
88 static T
* allocateHashTableBacking(size_t size
)
90 return reinterpret_cast<T
*>(allocateBacking(size
));
92 template <typename T
, typename HashTable
>
93 static T
* allocateZeroedHashTableBacking(size_t size
)
95 void* result
= allocateBacking(size
);
96 memset(result
, 0, size
);
97 return reinterpret_cast<T
*>(result
);
99 static void freeHashTableBacking(void* address
);
101 template <typename Return
, typename Metadata
>
102 static Return
malloc(size_t size
)
104 return reinterpret_cast<Return
>(fastMalloc(size
));
107 static inline bool expandHashTableBacking(void*, size_t)
111 static void free(void* address
)
116 static void* newArray(size_t bytes
)
118 return malloc
<void*, void>(bytes
);
121 deleteArray(void* ptr
)
123 free(ptr
); // Not the system free, the one from this class.
126 static bool isAllocationAllowed() { return true; }
128 static bool isHeapObjectAlive(T
* object
)
130 ASSERT_NOT_REACHED();
134 static void markNoTracing(...)
136 ASSERT_NOT_REACHED();
139 static void registerDelayedMarkNoTracing(...)
141 ASSERT_NOT_REACHED();
144 static void registerWeakMembers(...)
146 ASSERT_NOT_REACHED();
149 static void registerWeakTable(...)
151 ASSERT_NOT_REACHED();
155 static bool weakTableRegistered(...)
157 ASSERT_NOT_REACHED();
162 template<typename T
, typename Traits
>
163 static void trace(...)
165 ASSERT_NOT_REACHED();
174 static T
& getOther(T
* other
)
179 static void enterNoAllocationScope() { }
180 static void leaveNoAllocationScope() { }
181 static void enterGCForbiddenScope() { }
182 static void leaveGCForbiddenScope() { }
185 static void* allocateBacking(size_t);
188 // The Windows compiler seems to be very eager to instantiate things it won't
189 // need, so unless we have this class we get compile errors.
190 class DefaultAllocatorDummyVisitor
{
192 template<typename T
> inline bool isHeapObjectAlive(T obj
)
194 ASSERT_NOT_REACHED();
201 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \
203 void* operator new(size_t size) \
205 return Allocator::template malloc<void*, ClassName>(size); \
207 void operator delete(void* p) { Allocator::free(p); } \
208 void* operator new[](size_t size) { return Allocator::template newArray<ClassName>(size); } \
209 void operator delete[](void* p) { Allocator::deleteArray(p); } \
210 void* operator new(size_t, NotNullTag, void* location) \
216 typedef int __thisIsHereToForceASemicolonAfterThisMacro
218 using WTF::DefaultAllocator
;
220 #endif // WTF_DefaultAllocator_h