OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COURGETTE_MEMORY_ALLOCATOR_H_ | 5 #ifndef COURGETTE_MEMORY_ALLOCATOR_H_ |
6 #define COURGETTE_MEMORY_ALLOCATOR_H_ | 6 #define COURGETTE_MEMORY_ALLOCATOR_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 // temporary file. The interface is STL inspired but the class does not throw | 144 // temporary file. The interface is STL inspired but the class does not throw |
145 // STL exceptions on allocation failure. Instead it returns NULL. | 145 // STL exceptions on allocation failure. Instead it returns NULL. |
146 // A file allocation will be made if either the requested memory size exceeds | 146 // A file allocation will be made if either the requested memory size exceeds |
147 // |kMaxHeapAllocationSize| or if a heap allocation fails. | 147 // |kMaxHeapAllocationSize| or if a heap allocation fails. |
148 // Allocating the memory as a mapping of a temporary file solves the problem | 148 // Allocating the memory as a mapping of a temporary file solves the problem |
149 // that there might not be enough physical memory and pagefile to support the | 149 // that there might not be enough physical memory and pagefile to support the |
150 // allocation. This can happen because these resources are too small, or | 150 // allocation. This can happen because these resources are too small, or |
151 // already committed to other processes. Provided there is enough disk, the | 151 // already committed to other processes. Provided there is enough disk, the |
152 // temporary file acts like a pagefile that other processes can't access. | 152 // temporary file acts like a pagefile that other processes can't access. |
153 template<class T> | 153 template<class T> |
154 class MemoryAllocator { | 154 class MemoryAllocator { |
tommi (sloooow) - chröme
2017/03/03 08:32:33
actually... is there a difference now between the
cblume
2017/03/06 21:25:45
It looks like there is a difference.
On Mac & Linu
| |
155 public: | 155 public: |
156 typedef T value_type; | 156 typedef T value_type; |
157 typedef value_type* pointer; | 157 typedef value_type* pointer; |
158 typedef value_type& reference; | 158 typedef value_type& reference; |
159 typedef const value_type* const_pointer; | 159 typedef const value_type* const_pointer; |
160 typedef const value_type& const_reference; | 160 typedef const value_type& const_reference; |
161 typedef size_t size_type; | 161 typedef size_t size_type; |
162 typedef ptrdiff_t difference_type; | 162 typedef ptrdiff_t difference_type; |
163 | 163 |
164 // Each allocation is tagged with a single byte so that we know how to | 164 // Each allocation is tagged with a single byte so that we know how to |
165 // deallocate it. | 165 // deallocate it. |
166 enum AllocationType { | 166 enum AllocationType { |
167 HEAP_ALLOCATION, | 167 HEAP_ALLOCATION, |
168 FILE_ALLOCATION, | 168 FILE_ALLOCATION, |
169 }; | 169 }; |
170 | 170 |
171 // 5MB is the maximum heap allocation size that we'll attempt. | 171 // 5MB is the maximum heap allocation size that we'll attempt. |
172 // When applying a patch for Chrome 10.X we found that at this | 172 // When applying a patch for Chrome 10.X we found that at this |
173 // threshold there were 17 allocations higher than this threshold | 173 // threshold there were 17 allocations higher than this threshold |
174 // (largest at 136MB) 10 allocations just below the threshold and 6362 | 174 // (largest at 136MB) 10 allocations just below the threshold and 6362 |
175 // smaller allocations. | 175 // smaller allocations. |
176 static const size_t kMaxHeapAllocationSize = 1024 * 1024 * 5; | 176 static const size_t kMaxHeapAllocationSize = 1024 * 1024 * 5; |
177 | 177 |
178 template<class OtherT> | 178 template<class OtherT> |
179 struct rebind { | 179 struct rebind { |
180 // convert a MemoryAllocator<T> to a MemoryAllocator<OtherT> | 180 // convert a MemoryAllocator<T> to a MemoryAllocator<OtherT> |
181 typedef MemoryAllocator<OtherT> other; | 181 typedef MemoryAllocator<OtherT> other; |
182 }; | 182 }; |
183 | 183 |
184 MemoryAllocator() _THROW0() { | 184 MemoryAllocator() {} |
185 } | |
186 | 185 |
187 // We can't use an explicit constructor here, as dictated by our style guide. | 186 // We can't use an explicit constructor here, as dictated by our style guide. |
188 // The implementation of basic_string in Visual Studio 2010 prevents this. | 187 // The implementation of basic_string in Visual Studio 2010 prevents this. |
189 MemoryAllocator(const MemoryAllocator<T>& other) _THROW0() { // NOLINT | 188 MemoryAllocator(const MemoryAllocator<T>& other) { // NOLINT |
190 } | 189 } |
191 | 190 |
192 template<class OtherT> | 191 template <class OtherT> |
193 MemoryAllocator(const MemoryAllocator<OtherT>& other) _THROW0() { // NOLINT | 192 MemoryAllocator(const MemoryAllocator<OtherT>& other) { // NOLINT |
194 } | 193 } |
195 | 194 |
196 ~MemoryAllocator() { | 195 ~MemoryAllocator() { |
197 } | 196 } |
198 | 197 |
199 void deallocate(pointer ptr, size_type size) { | 198 void deallocate(pointer ptr, size_type size) { |
200 uint8_t* mem = reinterpret_cast<uint8_t*>(ptr); | 199 uint8_t* mem = reinterpret_cast<uint8_t*>(ptr); |
201 mem -= sizeof(T); | 200 mem -= sizeof(T); |
202 if (mem[0] == HEAP_ALLOCATION) { | 201 if (mem[0] == HEAP_ALLOCATION) { |
203 free(mem); | 202 free(mem); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 } | 244 } |
246 | 245 |
247 void construct(pointer ptr, const T& value) { | 246 void construct(pointer ptr, const T& value) { |
248 ::new(ptr) T(value); | 247 ::new(ptr) T(value); |
249 } | 248 } |
250 | 249 |
251 void destroy(pointer ptr) { | 250 void destroy(pointer ptr) { |
252 ptr->~T(); | 251 ptr->~T(); |
253 } | 252 } |
254 | 253 |
255 size_type max_size() const _THROW0() { | 254 size_type max_size() const { |
256 size_type count = static_cast<size_type>(-1) / sizeof(T); | 255 size_type count = static_cast<size_type>(-1) / sizeof(T); |
257 return (0 < count ? count : 1); | 256 return (0 < count ? count : 1); |
258 } | 257 } |
259 }; | 258 }; |
260 | 259 |
261 #else // OS_WIN | 260 #else // OS_WIN |
262 | 261 |
263 // On Mac, Linux, we use a bare bones implementation that only does | 262 // On Mac, Linux, we use a bare bones implementation that only does |
264 // heap allocations. | 263 // heap allocations. |
265 template<class T> | 264 template<class T> |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 protected: | 497 protected: |
499 T* buffer_; | 498 T* buffer_; |
500 size_t size_; // how much of the buffer we're using. | 499 size_t size_; // how much of the buffer we're using. |
501 size_t alloc_size_; // how much space we have allocated. | 500 size_t alloc_size_; // how much space we have allocated. |
502 Allocator alloc_; | 501 Allocator alloc_; |
503 }; | 502 }; |
504 | 503 |
505 } // namespace courgette | 504 } // namespace courgette |
506 | 505 |
507 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ | 506 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ |
OLD | NEW |