| 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 "base/memory/aligned_memory.h" |
| 13 | 14 |
| 14 // This allocator can be used with STL containers to provide a stack buffer | 15 // This allocator can be used with STL containers to provide a stack buffer |
| 15 // from which to allocate memory and overflows onto the heap. This stack buffer | 16 // from which to allocate memory and overflows onto the heap. This stack buffer |
| 16 // would be allocated on the stack and allows us to avoid heap operations in | 17 // would be allocated on the stack and allows us to avoid heap operations in |
| 17 // some situations. | 18 // some situations. |
| 18 // | 19 // |
| 19 // STL likes to make copies of allocators, so the allocator itself can't hold | 20 // STL likes to make copies of allocators, so the allocator itself can't hold |
| 20 // the data. Instead, we make the creator responsible for creating a | 21 // the data. Instead, we make the creator responsible for creating a |
| 21 // StackAllocator::Source which contains the data. Copying the allocator | 22 // StackAllocator::Source which contains the data. Copying the allocator |
| 22 // merely copies the pointer to this shared source, so all allocators created | 23 // merely copies the pointer to this shared source, so all allocators created |
| (...skipping 13 matching lines...) Expand all Loading... |
| 36 typedef typename std::allocator<T>::size_type size_type; | 37 typedef typename std::allocator<T>::size_type size_type; |
| 37 | 38 |
| 38 // Backing store for the allocator. The container owner is responsible for | 39 // Backing store for the allocator. The container owner is responsible for |
| 39 // maintaining this for as long as any containers using this allocator are | 40 // maintaining this for as long as any containers using this allocator are |
| 40 // live. | 41 // live. |
| 41 struct Source { | 42 struct Source { |
| 42 Source() : used_stack_buffer_(false) { | 43 Source() : used_stack_buffer_(false) { |
| 43 } | 44 } |
| 44 | 45 |
| 45 // Casts the buffer in its right type. | 46 // Casts the buffer in its right type. |
| 46 T* stack_buffer() { return reinterpret_cast<T*>(stack_buffer_); } | 47 T* stack_buffer() { return stack_buffer_.template data_as<T>(); } |
| 47 const T* stack_buffer() const { | 48 const T* stack_buffer() const { |
| 48 return reinterpret_cast<const T*>(stack_buffer_); | 49 return stack_buffer_.template data_as<T>(); |
| 49 } | 50 } |
| 50 | 51 |
| 51 // | |
| 52 // IMPORTANT: Take care to ensure that stack_buffer_ is aligned | |
| 53 // since it is used to mimic an array of T. | |
| 54 // Be careful while declaring any unaligned types (like bool) | |
| 55 // before stack_buffer_. | |
| 56 // | |
| 57 | |
| 58 // The buffer itself. It is not of type T because we don't want the | 52 // The buffer itself. It is not of type T because we don't want the |
| 59 // constructors and destructors to be automatically called. Define a POD | 53 // constructors and destructors to be automatically called. Define a POD |
| 60 // buffer of the right size instead. | 54 // buffer of the right size instead. |
| 61 char stack_buffer_[sizeof(T[stack_capacity])]; | 55 base::AlignedMemory<sizeof(T[stack_capacity]), ALIGNOF(T)> stack_buffer_; |
| 62 | 56 |
| 63 // Set when the stack buffer is used for an allocation. We do not track | 57 // 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. | 58 // how much of the buffer is used, only that somebody is using it. |
| 65 bool used_stack_buffer_; | 59 bool used_stack_buffer_; |
| 66 }; | 60 }; |
| 67 | 61 |
| 68 // Used by containers when they want to refer to an allocator of type U. | 62 // Used by containers when they want to refer to an allocator of type U. |
| 69 template<typename U> | 63 template<typename U> |
| 70 struct rebind { | 64 struct rebind { |
| 71 typedef StackAllocator<U, stack_capacity> other; | 65 typedef StackAllocator<U, stack_capacity> other; |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 | 239 |
| 246 // Vectors are commonly indexed, which isn't very convenient even with | 240 // Vectors are commonly indexed, which isn't very convenient even with |
| 247 // operator-> (using "->at()" does exception stuff we don't want). | 241 // operator-> (using "->at()" does exception stuff we don't want). |
| 248 T& operator[](size_t i) { return this->container().operator[](i); } | 242 T& operator[](size_t i) { return this->container().operator[](i); } |
| 249 const T& operator[](size_t i) const { | 243 const T& operator[](size_t i) const { |
| 250 return this->container().operator[](i); | 244 return this->container().operator[](i); |
| 251 } | 245 } |
| 252 }; | 246 }; |
| 253 | 247 |
| 254 #endif // BASE_STACK_CONTAINER_H_ | 248 #endif // BASE_STACK_CONTAINER_H_ |
| OLD | NEW |