OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 BASE_STACK_CONTAINER_H_ | 5 #ifndef BASE_STACK_CONTAINER_H_ |
6 #define BASE_STACK_CONTAINER_H_ | 6 #define BASE_STACK_CONTAINER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "build/build_config.h" |
13 #include "base/memory/aligned_memory.h" | 14 #include "base/memory/aligned_memory.h" |
14 | 15 |
15 // This allocator can be used with STL containers to provide a stack buffer | 16 // This allocator can be used with STL containers to provide a stack buffer |
16 // from which to allocate memory and overflows onto the heap. This stack buffer | 17 // from which to allocate memory and overflows onto the heap. This stack buffer |
17 // would be allocated on the stack and allows us to avoid heap operations in | 18 // would be allocated on the stack and allows us to avoid heap operations in |
18 // some situations. | 19 // some situations. |
19 // | 20 // |
20 // STL likes to make copies of allocators, so the allocator itself can't hold | 21 // STL likes to make copies of allocators, so the allocator itself can't hold |
21 // the data. Instead, we make the creator responsible for creating a | 22 // the data. Instead, we make the creator responsible for creating a |
22 // StackAllocator::Source which contains the data. Copying the allocator | 23 // StackAllocator::Source which contains the data. Copying the allocator |
(...skipping 23 matching lines...) Expand all Loading... |
46 // Casts the buffer in its right type. | 47 // Casts the buffer in its right type. |
47 T* stack_buffer() { return stack_buffer_.template data_as<T>(); } | 48 T* stack_buffer() { return stack_buffer_.template data_as<T>(); } |
48 const T* stack_buffer() const { | 49 const T* stack_buffer() const { |
49 return stack_buffer_.template data_as<T>(); | 50 return stack_buffer_.template data_as<T>(); |
50 } | 51 } |
51 | 52 |
52 // The buffer itself. It is not of type T because we don't want the | 53 // The buffer itself. It is not of type T because we don't want the |
53 // constructors and destructors to be automatically called. Define a POD | 54 // constructors and destructors to be automatically called. Define a POD |
54 // buffer of the right size instead. | 55 // buffer of the right size instead. |
55 base::AlignedMemory<sizeof(T[stack_capacity]), ALIGNOF(T)> stack_buffer_; | 56 base::AlignedMemory<sizeof(T[stack_capacity]), ALIGNOF(T)> stack_buffer_; |
| 57 #if defined(OS_ANDROID) |
| 58 COMPILE_ASSERT(ALIGNOF(T) <= 16, crbug_115612); |
| 59 #endif |
56 | 60 |
57 // Set when the stack buffer is used for an allocation. We do not track | 61 // Set when the stack buffer is used for an allocation. We do not track |
58 // how much of the buffer is used, only that somebody is using it. | 62 // how much of the buffer is used, only that somebody is using it. |
59 bool used_stack_buffer_; | 63 bool used_stack_buffer_; |
60 }; | 64 }; |
61 | 65 |
62 // Used by containers when they want to refer to an allocator of type U. | 66 // Used by containers when they want to refer to an allocator of type U. |
63 template<typename U> | 67 template<typename U> |
64 struct rebind { | 68 struct rebind { |
65 typedef StackAllocator<U, stack_capacity> other; | 69 typedef StackAllocator<U, stack_capacity> other; |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 | 243 |
240 // Vectors are commonly indexed, which isn't very convenient even with | 244 // Vectors are commonly indexed, which isn't very convenient even with |
241 // operator-> (using "->at()" does exception stuff we don't want). | 245 // operator-> (using "->at()" does exception stuff we don't want). |
242 T& operator[](size_t i) { return this->container().operator[](i); } | 246 T& operator[](size_t i) { return this->container().operator[](i); } |
243 const T& operator[](size_t i) const { | 247 const T& operator[](size_t i) const { |
244 return this->container().operator[](i); | 248 return this->container().operator[](i); |
245 } | 249 } |
246 }; | 250 }; |
247 | 251 |
248 #endif // BASE_STACK_CONTAINER_H_ | 252 #endif // BASE_STACK_CONTAINER_H_ |
OLD | NEW |