| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/thread_collision_warner.h" |
| 12 | 13 |
| 13 // This allocator can be used with STL containers to provide a stack buffer | 14 // This allocator can be used with STL containers to provide a stack buffer |
| 14 // from which to allocate memory and overflows onto the heap. This stack buffer | 15 // from which to allocate memory and overflows onto the heap. This stack buffer |
| 15 // would be allocated on the stack and allows us to avoid heap operations in | 16 // would be allocated on the stack and allows us to avoid heap operations in |
| 16 // some situations. | 17 // some situations. |
| 17 // | 18 // |
| 18 // STL likes to make copies of allocators, so the allocator itself can't hold | 19 // STL likes to make copies of allocators, so the allocator itself can't hold |
| 19 // the data. Instead, we make the creator responsible for creating a | 20 // the data. Instead, we make the creator responsible for creating a |
| 20 // StackAllocator::Source which contains the data. Copying the allocator | 21 // StackAllocator::Source which contains the data. Copying the allocator |
| 21 // merely copies the pointer to this shared source, so all allocators created | 22 // merely copies the pointer to this shared source, so all allocators created |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // Danger: any copies of this made using the copy constructor must have | 146 // Danger: any copies of this made using the copy constructor must have |
| 146 // shorter lifetimes than the source. The copy will share the same allocator | 147 // shorter lifetimes than the source. The copy will share the same allocator |
| 147 // and therefore the same stack buffer as the original. Use std::copy to | 148 // and therefore the same stack buffer as the original. Use std::copy to |
| 148 // copy into a "real" container for longer-lived objects. | 149 // copy into a "real" container for longer-lived objects. |
| 149 ContainerType& container() { return container_; } | 150 ContainerType& container() { return container_; } |
| 150 const ContainerType& container() const { return container_; } | 151 const ContainerType& container() const { return container_; } |
| 151 | 152 |
| 152 // Support operator-> to get to the container. This allows nicer syntax like: | 153 // Support operator-> to get to the container. This allows nicer syntax like: |
| 153 // StackContainer<...> foo; | 154 // StackContainer<...> foo; |
| 154 // std::sort(foo->begin(), foo->end()); | 155 // std::sort(foo->begin(), foo->end()); |
| 155 ContainerType* operator->() { return &container_; } | 156 ContainerType* operator->() { |
| 156 const ContainerType* operator->() const { return &container_; } | 157 D_BOOK_CRITICAL_SECTION(arrow_operator_); |
| 158 return &container_; |
| 159 } |
| 160 const ContainerType* operator->() const { |
| 161 D_BOOK_CRITICAL_SECTION(arrow_operator_); |
| 162 return &container_; |
| 163 } |
| 157 | 164 |
| 158 #ifdef UNIT_TEST | 165 #ifdef UNIT_TEST |
| 159 // Retrieves the stack source so that that unit tests can verify that the | 166 // Retrieves the stack source so that that unit tests can verify that the |
| 160 // buffer is being used properly. | 167 // buffer is being used properly. |
| 161 const typename Allocator::Source& stack_data() const { | 168 const typename Allocator::Source& stack_data() const { |
| 162 return stack_data_; | 169 return stack_data_; |
| 163 } | 170 } |
| 164 #endif | 171 #endif |
| 165 | 172 |
| 166 protected: | 173 protected: |
| 167 typename Allocator::Source stack_data_; | 174 typename Allocator::Source stack_data_; |
| 168 Allocator allocator_; | 175 Allocator allocator_; |
| 169 ContainerType container_; | 176 ContainerType container_; |
| 170 | 177 |
| 178 D_DEFINE_CRITICAL_SECTION(arrow_operator_); |
| 179 |
| 171 DISALLOW_EVIL_CONSTRUCTORS(StackContainer); | 180 DISALLOW_EVIL_CONSTRUCTORS(StackContainer); |
| 172 }; | 181 }; |
| 173 | 182 |
| 174 // StackString | 183 // StackString |
| 175 template<size_t stack_capacity> | 184 template<size_t stack_capacity> |
| 176 class StackString : public StackContainer< | 185 class StackString : public StackContainer< |
| 177 std::basic_string<char, | 186 std::basic_string<char, |
| 178 std::char_traits<char>, | 187 std::char_traits<char>, |
| 179 StackAllocator<char, stack_capacity> >, | 188 StackAllocator<char, stack_capacity> >, |
| 180 stack_capacity> { | 189 stack_capacity> { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 // Vectors are commonly indexed, which isn't very convenient even with | 254 // Vectors are commonly indexed, which isn't very convenient even with |
| 246 // operator-> (using "->at()" does exception stuff we don't want). | 255 // operator-> (using "->at()" does exception stuff we don't want). |
| 247 T& operator[](size_t i) { return this->container().operator[](i); } | 256 T& operator[](size_t i) { return this->container().operator[](i); } |
| 248 const T& operator[](size_t i) const { | 257 const T& operator[](size_t i) const { |
| 249 return this->container().operator[](i); | 258 return this->container().operator[](i); |
| 250 } | 259 } |
| 251 }; | 260 }; |
| 252 | 261 |
| 253 #endif // BASE_STACK_CONTAINER_H__ | 262 #endif // BASE_STACK_CONTAINER_H__ |
| 254 | 263 |
| OLD | NEW |