• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
markNoTracing(...)106     static void markNoTracing(...)
107     {
108         ASSERT_NOT_REACHED();
109     }
110 
registerWeakMembers(...)111     static void registerWeakMembers(...)
112     {
113         ASSERT_NOT_REACHED();
114     }
115 
116     template<typename T, typename Traits>
trace(...)117     static void trace(...)
118     {
119         ASSERT_NOT_REACHED();
120     }
121 
122     template<typename T>
123     struct OtherType {
124         typedef T* Type;
125     };
126 
127     template<typename T>
getOther(T * other)128     static T& getOther(T* other)
129     {
130         return *other;
131     }
132 
133 private:
134     WTF_EXPORT static void* backingAllocate(size_t);
135 };
136 
137 // The Windows compiler seems to be very eager to instantiate things it won't
138 // need, so unless we have this class we get compile errors.
139 class DefaultAllocatorDummyVisitor {
140 public:
isAlive(T obj)141     template<typename T> inline bool isAlive(T obj)
142     {
143         ASSERT_NOT_REACHED();
144         return false;
145     }
146 };
147 
148 } // namespace WTF
149 
150 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \
151 public: \
152     void* operator new(size_t size) \
153     { \
154         return Allocator::template malloc<void*, ClassName>(size); \
155     } \
156     void operator delete(void* p) { Allocator::free(p); } \
157     void* operator new[](size_t size) { return Allocator::template newArray<ClassName>(size); } \
158     void operator delete[](void* p) { Allocator::deleteArray(p); } \
159     void* operator new(size_t, NotNullTag, void* location) \
160     { \
161         ASSERT(location); \
162         return location; \
163     } \
164 private: \
165 typedef int __thisIsHereToForceASemicolonAfterThisMacro
166 
167 using WTF::DefaultAllocator;
168 
169 #endif // WTF_DefaultAllocator_h
170