Chromium Code Reviews| 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 typename std::aligned_storage<sizeof(T[stack_capacity]), alignof(T)>::type |
|
danakj
2017/06/12 18:32:10
Since you're having issues maybe we can/should spl
| |
| 58 stack_buffer_; | |
| 59 #if defined(__GNUC__) && !defined(ARCH_CPU_X86_FAMILY) | 59 #if defined(__GNUC__) && !defined(ARCH_CPU_X86_FAMILY) |
| 60 static_assert(ALIGNOF(T) <= 16, "http://crbug.com/115612"); | 60 static_assert(alignof(T) <= 16, "http://crbug.com/115612"); |
| 61 #endif | 61 #endif |
| 62 | 62 |
| 63 // Set when the stack buffer is used for an allocation. We do not track | 63 // 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. | 64 // how much of the buffer is used, only that somebody is using it. |
| 65 bool used_stack_buffer_; | 65 bool used_stack_buffer_; |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // Used by containers when they want to refer to an allocator of type U. | 68 // Used by containers when they want to refer to an allocator of type U. |
| 69 template<typename U> | 69 template<typename U> |
| 70 struct rebind { | 70 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). | 221 // operator-> (using "->at()" does exception stuff we don't want). |
| 222 T& operator[](size_t i) { return this->container().operator[](i); } | 222 T& operator[](size_t i) { return this->container().operator[](i); } |
| 223 const T& operator[](size_t i) const { | 223 const T& operator[](size_t i) const { |
| 224 return this->container().operator[](i); | 224 return this->container().operator[](i); |
| 225 } | 225 } |
| 226 }; | 226 }; |
| 227 | 227 |
| 228 } // namespace base | 228 } // namespace base |
| 229 | 229 |
| 230 #endif // BASE_CONTAINERS_STACK_CONTAINER_H_ | 230 #endif // BASE_CONTAINERS_STACK_CONTAINER_H_ |
| OLD | NEW |