Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1169)

Side by Side Diff: third_party/WebKit/Source/wtf/allocator/PartitionAllocator.h

Issue 2518253002: Move Partition Allocator into Chromium base. (Closed)
Patch Set: Move OOM_CRASH into its own, more specific header. Fixes Windows build. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 WTF_PartitionAllocator_h
32 #define WTF_PartitionAllocator_h 6 #define WTF_PartitionAllocator_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" 12 #include "base/allocator/partition_allocator/partition_alloc.h"
13 #include "third_party/WebKit/Source/wtf/Allocator.h"
39 #include "wtf/Assertions.h" 14 #include "wtf/Assertions.h"
40 #include "wtf/allocator/PartitionAlloc.h" 15 #include "wtf/TypeTraits.h"
41 #include "wtf/allocator/Partitions.h" 16 #include "wtf/WTFExport.h"
42
43 #include <string.h> 17 #include <string.h>
44 18
45 namespace WTF { 19 namespace WTF {
46 20
47 class PartitionAllocatorDummyVisitor { 21 class PartitionAllocatorDummyVisitor {
48 DISALLOW_NEW(); 22 DISALLOW_NEW();
49 }; 23 };
50 24
51 class WTF_EXPORT PartitionAllocator { 25 class WTF_EXPORT PartitionAllocator {
52 public: 26 public:
53 typedef PartitionAllocatorDummyVisitor Visitor; 27 typedef PartitionAllocatorDummyVisitor Visitor;
54 static const bool isGarbageCollected = false; 28 static const bool isGarbageCollected = false;
55 29
56 template <typename T> 30 template <typename T>
57 static size_t quantizedSize(size_t count) { 31 static size_t quantizedSize(size_t count) {
58 RELEASE_ASSERT(count <= kGenericMaxDirectMapped / sizeof(T)); 32 RELEASE_ASSERT(count <= base::kGenericMaxDirectMapped / sizeof(T));
59 return partitionAllocActualSize(Partitions::bufferPartition(), 33 return partitionAllocActualSize(WTF::Partitions::bufferPartition(),
60 count * sizeof(T)); 34 count * sizeof(T));
61 } 35 }
62 template <typename T> 36 template <typename T>
63 static T* allocateVectorBacking(size_t size) { 37 static T* allocateVectorBacking(size_t size) {
64 return reinterpret_cast<T*>( 38 return reinterpret_cast<T*>(
65 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T))); 39 allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T)));
66 } 40 }
67 template <typename T> 41 template <typename T>
68 static T* allocateExpandedVectorBacking(size_t size) { 42 static T* allocateExpandedVectorBacking(size_t size) {
69 return reinterpret_cast<T*>( 43 return reinterpret_cast<T*>(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 template <typename T, typename HashTable> 75 template <typename T, typename HashTable>
102 static T* allocateZeroedHashTableBacking(size_t size) { 76 static T* allocateZeroedHashTableBacking(size_t size) {
103 void* result = allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T)); 77 void* result = allocateBacking(size, WTF_HEAP_PROFILER_TYPE_NAME(T));
104 memset(result, 0, size); 78 memset(result, 0, size);
105 return reinterpret_cast<T*>(result); 79 return reinterpret_cast<T*>(result);
106 } 80 }
107 static void freeHashTableBacking(void* address); 81 static void freeHashTableBacking(void* address);
108 82
109 template <typename Return, typename Metadata> 83 template <typename Return, typename Metadata>
110 static Return malloc(size_t size, const char* typeName) { 84 static Return malloc(size_t size, const char* typeName) {
111 return reinterpret_cast<Return>(Partitions::fastMalloc(size, typeName)); 85 return reinterpret_cast<Return>(
86 WTF::Partitions::fastMalloc(size, typeName));
112 } 87 }
113 88
114 static inline bool expandHashTableBacking(void*, size_t) { return false; } 89 static inline bool expandHashTableBacking(void*, size_t) { return false; }
115 static void free(void* address) { Partitions::fastFree(address); } 90 static void free(void* address) { WTF::Partitions::fastFree(address); }
116 template <typename T> 91 template <typename T>
117 static void* newArray(size_t bytes) { 92 static void* newArray(size_t bytes) {
118 return malloc<void*, void>(bytes, WTF_HEAP_PROFILER_TYPE_NAME(T)); 93 return malloc<void*, void>(bytes, WTF_HEAP_PROFILER_TYPE_NAME(T));
119 } 94 }
120 static void deleteArray(void* ptr) { 95 static void deleteArray(void* ptr) {
121 free(ptr); // Not the system free, the one from this class. 96 free(ptr); // Not the system free, the one from this class.
122 } 97 }
123 98
124 static bool isAllocationAllowed() { return true; } 99 static bool isAllocationAllowed() { return true; }
125 100
126 static void enterGCForbiddenScope() {} 101 static void enterGCForbiddenScope() {}
127 static void leaveGCForbiddenScope() {} 102 static void leaveGCForbiddenScope() {}
128 103
129 private: 104 private:
130 static void* allocateBacking(size_t, const char* typeName); 105 static void* allocateBacking(size_t, const char* typeName);
131 }; 106 };
132 107
133 // Specializations for heap profiling, so type profiling of |char| is possible 108 // 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 109 // even in official builds (because |char| makes up a large portion of the
135 // heap.) 110 // heap.)
136 template <> 111 template <>
137 WTF_EXPORT char* PartitionAllocator::allocateVectorBacking<char>(size_t); 112 WTF_EXPORT char* PartitionAllocator::allocateVectorBacking<char>(size_t);
138 template <> 113 template <>
139 WTF_EXPORT char* PartitionAllocator::allocateExpandedVectorBacking<char>( 114 WTF_EXPORT char* PartitionAllocator::allocateExpandedVectorBacking<char>(
140 size_t); 115 size_t);
141 116
142 } // namespace WTF 117 } // namespace WTF
143 118
144 #define WTF_USE_ALLOCATOR(ClassName, Allocator) \ 119 #define USE_ALLOCATOR(ClassName, Allocator) \
145 public: \ 120 public: \
146 void* operator new(size_t size) { \ 121 void* operator new(size_t size) { \
147 return Allocator::template malloc<void*, ClassName>( \ 122 return Allocator::template malloc<void*, ClassName>( \
148 size, WTF_HEAP_PROFILER_TYPE_NAME(ClassName)); \ 123 size, WTF_HEAP_PROFILER_TYPE_NAME(ClassName)); \
149 } \ 124 } \
150 void operator delete(void* p) { Allocator::free(p); } \ 125 void operator delete(void* p) { Allocator::free(p); } \
151 void* operator new[](size_t size) { \ 126 void* operator new[](size_t size) { \
152 return Allocator::template newArray<ClassName>(size); \ 127 return Allocator::template newArray<ClassName>(size); \
153 } \ 128 } \
154 void operator delete[](void* p) { Allocator::deleteArray(p); } \ 129 void operator delete[](void* p) { Allocator::deleteArray(p); } \
155 void* operator new(size_t, NotNullTag, void* location) { \ 130 void* operator new(size_t, NotNullTag, void* location) { \
156 ASSERT(location); \ 131 ASSERT(location); \
157 return location; \ 132 return location; \
158 } \ 133 } \
159 void* operator new(size_t, void* location) { return location; } \ 134 void* operator new(size_t, void* location) { return location; } \
160 \ 135 \
161 private: \ 136 private: \
162 typedef int __thisIsHereToForceASemicolonAfterThisMacro 137 typedef int __thisIsHereToForceASemicolonAfterThisMacro
163 138
164 using WTF::PartitionAllocator;
165
166 #endif // WTF_PartitionAllocator_h 139 #endif // WTF_PartitionAllocator_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698