OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_CONTAINERS_STACK_CONTAINER_H_ | 5 #ifndef BASE_CONTAINERS_STACK_CONTAINER_H_ |
6 #define BASE_CONTAINERS_STACK_CONTAINER_H_ | 6 #define BASE_CONTAINERS_STACK_CONTAINER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/aligned_memory.h" | |
14 #include "build/build_config.h" | 13 #include "build/build_config.h" |
15 | 14 |
16 namespace base { | 15 namespace base { |
17 | 16 |
18 // This allocator can be used with STL containers to provide a stack buffer | 17 // This allocator can be used with STL containers to provide a stack buffer |
19 // from which to allocate memory and overflows onto the heap. This stack buffer | 18 // from which to allocate memory and overflows onto the heap. This stack buffer |
20 // would be allocated on the stack and allows us to avoid heap operations in | 19 // would be allocated on the stack and allows us to avoid heap operations in |
21 // some situations. | 20 // some situations. |
22 // | 21 // |
23 // STL likes to make copies of allocators, so the allocator itself can't hold | 22 // STL likes to make copies of allocators, so the allocator itself can't hold |
(...skipping 16 matching lines...) Expand all Loading... |
40 typedef typename std::allocator<T>::size_type size_type; | 39 typedef typename std::allocator<T>::size_type size_type; |
41 | 40 |
42 // Backing store for the allocator. The container owner is responsible for | 41 // Backing store for the allocator. The container owner is responsible for |
43 // maintaining this for as long as any containers using this allocator are | 42 // maintaining this for as long as any containers using this allocator are |
44 // live. | 43 // live. |
45 struct Source { | 44 struct Source { |
46 Source() : used_stack_buffer_(false) { | 45 Source() : used_stack_buffer_(false) { |
47 } | 46 } |
48 | 47 |
49 // Casts the buffer in its right type. | 48 // Casts the buffer in its right type. |
50 T* stack_buffer() { return stack_buffer_.template data_as<T>(); } | 49 T* stack_buffer() { return reinterpret_cast<T*>(stack_buffer_); } |
51 const T* stack_buffer() const { | 50 const T* stack_buffer() const { |
52 return stack_buffer_.template data_as<T>(); | 51 return reinterpret_cast<const T*>(&stack_buffer_); |
53 } | 52 } |
54 | 53 |
55 // The buffer itself. It is not of type T because we don't want the | 54 // The buffer itself. It is not of type T because we don't want the |
56 // constructors and destructors to be automatically called. Define a POD | 55 // constructors and destructors to be automatically called. Define a POD |
57 // buffer of the right size instead. | 56 // buffer of the right size instead. |
58 base::AlignedMemory<sizeof(T[stack_capacity]), ALIGNOF(T)> stack_buffer_; | 57 alignas(T) char stack_buffer_[sizeof(T[stack_capacity])]; |
59 #if defined(__GNUC__) && !defined(ARCH_CPU_X86_FAMILY) | 58 #if defined(__GNUC__) && !defined(ARCH_CPU_X86_FAMILY) |
60 static_assert(ALIGNOF(T) <= 16, "http://crbug.com/115612"); | 59 static_assert(alignof(T) <= 16, "http://crbug.com/115612"); |
61 #endif | 60 #endif |
62 | 61 |
63 // Set when the stack buffer is used for an allocation. We do not track | 62 // Set when the stack buffer is used for an allocation. We do not track |
64 // how much of the buffer is used, only that somebody is using it. | 63 // how much of the buffer is used, only that somebody is using it. |
65 bool used_stack_buffer_; | 64 bool used_stack_buffer_; |
66 }; | 65 }; |
67 | 66 |
68 // Used by containers when they want to refer to an allocator of type U. | 67 // Used by containers when they want to refer to an allocator of type U. |
69 template<typename U> | 68 template<typename U> |
70 struct rebind { | 69 struct rebind { |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 // operator-> (using "->at()" does exception stuff we don't want). | 220 // operator-> (using "->at()" does exception stuff we don't want). |
222 T& operator[](size_t i) { return this->container().operator[](i); } | 221 T& operator[](size_t i) { return this->container().operator[](i); } |
223 const T& operator[](size_t i) const { | 222 const T& operator[](size_t i) const { |
224 return this->container().operator[](i); | 223 return this->container().operator[](i); |
225 } | 224 } |
226 }; | 225 }; |
227 | 226 |
228 } // namespace base | 227 } // namespace base |
229 | 228 |
230 #endif // BASE_CONTAINERS_STACK_CONTAINER_H_ | 229 #endif // BASE_CONTAINERS_STACK_CONTAINER_H_ |
OLD | NEW |