OLD | NEW |
---|---|
1 /* | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * | 3 // found in the LICENSE file. |
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 | 4 |
31 #ifndef WTF_PartitionAllocator_h | 5 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOCATOR_H |
32 #define WTF_PartitionAllocator_h | 6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOCATOR_H |
33 | 7 |
34 // This is the allocator that is used for allocations that are not on the | 8 // This is the allocator that is used for allocations that are not on the |
35 // traced, garbage collected heap. It uses FastMalloc for collections, | 9 // traced, garbage collected heap. It uses FastMalloc for collections, |
36 // but uses the partition allocator for the backing store of the collections. | 10 // but uses the partition allocator for the backing store of the collections. |
37 | 11 |
38 #include "wtf/Allocator.h" | |
39 #include "wtf/Assertions.h" | |
40 #include "wtf/allocator/PartitionAlloc.h" | |
41 #include "wtf/allocator/Partitions.h" | |
42 | |
43 #include <string.h> | 12 #include <string.h> |
44 | 13 |
45 namespace WTF { | 14 #include "base/allocator/partition_allocator/partition_alloc.h" |
15 #include "base/allocator/partition_allocator/partitions.h" | |
Primiano Tucci (use gerrit)
2016/11/28 12:06:50
I think you want to remove this include
palmer
2016/12/01 00:48:24
This file is moving back to wtf, so I'll soon see.
| |
16 | |
17 namespace base { | |
46 | 18 |
47 class PartitionAllocatorDummyVisitor { | 19 class PartitionAllocatorDummyVisitor { |
48 DISALLOW_NEW(); | 20 DISALLOW_NEW(); |
49 }; | 21 }; |
50 | 22 |
51 class WTF_EXPORT PartitionAllocator { | 23 class BASE_EXPORT PartitionAllocator { |
52 public: | 24 public: |
53 typedef PartitionAllocatorDummyVisitor Visitor; | 25 typedef PartitionAllocatorDummyVisitor Visitor; |
54 static const bool isGarbageCollected = false; | 26 static const bool isGarbageCollected = false; |
55 | 27 |
56 template <typename T> | 28 template <typename T> |
57 static size_t quantizedSize(size_t count) { | 29 static size_t quantizedSize(size_t count) { |
58 RELEASE_ASSERT(count <= kGenericMaxDirectMapped / sizeof(T)); | 30 CHECK(count <= kGenericMaxDirectMapped / sizeof(T)); |
59 return partitionAllocActualSize(Partitions::bufferPartition(), | 31 return partitionAllocActualSize(Partitions::bufferPartition(), |
60 count * sizeof(T)); | 32 count * sizeof(T)); |
61 } | 33 } |
62 template <typename T> | 34 template <typename T> |
63 static T* allocateVectorBacking(size_t size) { | 35 static T* allocateVectorBacking(size_t size) { |
64 return reinterpret_cast<T*>( | 36 return reinterpret_cast<T*>( |
65 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | 37 allocateBacking(size, PARTITION_HEAP_PROFILER_TYPE_NAME(T))); |
66 } | 38 } |
67 template <typename T> | 39 template <typename T> |
68 static T* allocateExpandedVectorBacking(size_t size) { | 40 static T* allocateExpandedVectorBacking(size_t size) { |
69 return reinterpret_cast<T*>( | 41 return reinterpret_cast<T*>( |
70 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | 42 allocateBacking(size, PARTITION_HEAP_PROFILER_TYPE_NAME(T))); |
71 } | 43 } |
72 static void freeVectorBacking(void* address); | 44 static void freeVectorBacking(void* address); |
73 static inline bool expandVectorBacking(void*, size_t) { return false; } | 45 static inline bool expandVectorBacking(void*, size_t) { return false; } |
74 static inline bool shrinkVectorBacking(void* address, | 46 static inline bool shrinkVectorBacking(void* address, |
75 size_t quantizedCurrentSize, | 47 size_t quantizedCurrentSize, |
76 size_t quantizedShrunkSize) { | 48 size_t quantizedShrunkSize) { |
77 // Optimization: if we're downsizing inside the same allocator bucket, | 49 // Optimization: if we're downsizing inside the same allocator bucket, |
78 // we can skip reallocation. | 50 // we can skip reallocation. |
79 return quantizedCurrentSize == quantizedShrunkSize; | 51 return quantizedCurrentSize == quantizedShrunkSize; |
80 } | 52 } |
81 template <typename T> | 53 template <typename T> |
82 static T* allocateInlineVectorBacking(size_t size) { | 54 static T* allocateInlineVectorBacking(size_t size) { |
83 return allocateVectorBacking<T>(size); | 55 return allocateVectorBacking<T>(size); |
84 } | 56 } |
85 static inline void freeInlineVectorBacking(void* address) { | 57 static inline void freeInlineVectorBacking(void* address) { |
86 freeVectorBacking(address); | 58 freeVectorBacking(address); |
87 } | 59 } |
88 static inline bool expandInlineVectorBacking(void*, size_t) { return false; } | 60 static inline bool expandInlineVectorBacking(void*, size_t) { return false; } |
89 static inline bool shrinkInlineVectorBacking(void* address, | 61 static inline bool shrinkInlineVectorBacking(void* address, |
90 size_t quantizedCurrentSize, | 62 size_t quantizedCurrentSize, |
91 size_t quantizedShrunkSize) { | 63 size_t quantizedShrunkSize) { |
92 return shrinkVectorBacking(address, quantizedCurrentSize, | 64 return shrinkVectorBacking(address, quantizedCurrentSize, |
93 quantizedShrunkSize); | 65 quantizedShrunkSize); |
94 } | 66 } |
95 | 67 |
96 template <typename T, typename HashTable> | 68 template <typename T, typename HashTable> |
97 static T* allocateHashTableBacking(size_t size) { | 69 static T* allocateHashTableBacking(size_t size) { |
98 return reinterpret_cast<T*>( | 70 return reinterpret_cast<T*>( |
99 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); | 71 allocateBacking(size, PARTITION_HEAP_PROFILER_TYPE_NAME(T))); |
100 } | 72 } |
101 template <typename T, typename HashTable> | 73 template <typename T, typename HashTable> |
102 static T* allocateZeroedHashTableBacking(size_t size) { | 74 static T* allocateZeroedHashTableBacking(size_t size) { |
103 void* result = allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T)); | 75 void* result = allocateBacking(size, PARTITION_HEAP_PROFILER_TYPE_NAME(T)); |
104 memset(result, 0, size); | 76 memset(result, 0, size); |
105 return reinterpret_cast<T*>(result); | 77 return reinterpret_cast<T*>(result); |
106 } | 78 } |
107 static void freeHashTableBacking(void* address); | 79 static void freeHashTableBacking(void* address); |
108 | 80 |
109 template <typename Return, typename Metadata> | 81 template <typename Return, typename Metadata> |
110 static Return malloc(size_t size, const char* typeName) { | 82 static Return malloc(size_t size, const char* typeName) { |
111 return reinterpret_cast<Return>(Partitions::fastMalloc(size, typeName)); | 83 return reinterpret_cast<Return>(Partitions::fastMalloc(size, typeName)); |
Primiano Tucci (use gerrit)
2016/11/28 12:06:50
well you can't refer to partitions now here
| |
112 } | 84 } |
113 | 85 |
114 static inline bool expandHashTableBacking(void*, size_t) { return false; } | 86 static inline bool expandHashTableBacking(void*, size_t) { return false; } |
115 static void free(void* address) { Partitions::fastFree(address); } | 87 static void free(void* address) { Partitions::fastFree(address); } |
116 template <typename T> | 88 template <typename T> |
117 static void* newArray(size_t bytes) { | 89 static void* newArray(size_t bytes) { |
118 return malloc<void*, void>(bytes, WTF_HEAP_PROFILER_TYPE_NAME(T)); | 90 return malloc<void*, void>(bytes, PARTITION_HEAP_PROFILER_TYPE_NAME(T)); |
119 } | 91 } |
120 static void deleteArray(void* ptr) { | 92 static void deleteArray(void* ptr) { |
121 free(ptr); // Not the system free, the one from this class. | 93 free(ptr); // Not the system free, the one from this class. |
122 } | 94 } |
123 | 95 |
124 static bool isAllocationAllowed() { return true; } | 96 static bool isAllocationAllowed() { return true; } |
125 | 97 |
126 static void enterGCForbiddenScope() {} | 98 static void enterGCForbiddenScope() {} |
127 static void leaveGCForbiddenScope() {} | 99 static void leaveGCForbiddenScope() {} |
128 | 100 |
129 private: | 101 private: |
130 static void* allocateBacking(size_t, const char* typeName); | 102 static void* allocateBacking(size_t, const char* typeName); |
131 }; | 103 }; |
132 | 104 |
133 // Specializations for heap profiling, so type profiling of |char| is possible | 105 // Specializations for heap profiling, so type profiling of |char| is possible |
134 // even in official builds (because |char| makes up a large portion of the | 106 // even in official builds (because |char| makes up a large portion of the |
135 // heap.) | 107 // heap.) |
136 template <> | 108 template <> |
137 WTF_EXPORT char* PartitionAllocator::allocateVectorBacking<char>(size_t); | 109 BASE_EXPORT char* PartitionAllocator::allocateVectorBacking<char>(size_t); |
138 template <> | 110 template <> |
139 WTF_EXPORT char* PartitionAllocator::allocateExpandedVectorBacking<char>( | 111 BASE_EXPORT char* PartitionAllocator::allocateExpandedVectorBacking<char>( |
140 size_t); | 112 size_t); |
141 | 113 |
142 } // namespace WTF | 114 } // namespace base |
143 | 115 |
144 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \ | 116 #define USE_ALLOCATOR(ClassName, Allocator) \ |
145 public: \ | 117 public: \ |
146 void* operator new(size_t size) { \ | 118 void* operator new(size_t size) { \ |
147 return Allocator::template malloc<void*, ClassName>( \ | 119 return Allocator::template malloc<void*, ClassName>( \ |
148 size, WTF_HEAP_PROFILER_TYPE_NAME(ClassName)); \ | 120 size, PARTITION_HEAP_PROFILER_TYPE_NAME(ClassName)); \ |
149 } \ | 121 } \ |
150 void operator delete(void* p) { Allocator::free(p); } \ | 122 void operator delete(void* p) { Allocator::free(p); } \ |
151 void* operator new[](size_t size) { \ | 123 void* operator new[](size_t size) { \ |
152 return Allocator::template newArray<ClassName>(size); \ | 124 return Allocator::template newArray<ClassName>(size); \ |
153 } \ | 125 } \ |
154 void operator delete[](void* p) { Allocator::deleteArray(p); } \ | 126 void operator delete[](void* p) { Allocator::deleteArray(p); } \ |
155 void* operator new(size_t, NotNullTag, void* location) { \ | 127 void* operator new(size_t, NotNullTag, void* location) { \ |
156 ASSERT(location); \ | 128 ASSERT(location); \ |
157 return location; \ | 129 return location; \ |
158 } \ | 130 } \ |
159 void* operator new(size_t, void* location) { return location; } \ | 131 void* operator new(size_t, void* location) { return location; } \ |
160 \ | 132 \ |
161 private: \ | 133 private: \ |
162 typedef int __thisIsHereToForceASemicolonAfterThisMacro | 134 typedef int __thisIsHereToForceASemicolonAfterThisMacro |
163 | 135 |
164 using WTF::PartitionAllocator; | 136 #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOCATOR_H |
165 | |
166 #endif // WTF_PartitionAllocator_h | |
OLD | NEW |