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 <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // For this constructor, we cannot share storage; there's | 83 // For this constructor, we cannot share storage; there's |
84 // no guarantee that the Source buffer of Ts is large enough | 84 // no guarantee that the Source buffer of Ts is large enough |
85 // for Us. | 85 // for Us. |
86 // TODO: If we were fancy pants, perhaps we could share storage | 86 // TODO: If we were fancy pants, perhaps we could share storage |
87 // iff sizeof(T) == sizeof(U). | 87 // iff sizeof(T) == sizeof(U). |
88 template<typename U, size_t other_capacity> | 88 template<typename U, size_t other_capacity> |
89 StackAllocator(const StackAllocator<U, other_capacity>& other) | 89 StackAllocator(const StackAllocator<U, other_capacity>& other) |
90 : source_(NULL) { | 90 : source_(NULL) { |
91 } | 91 } |
92 | 92 |
| 93 // This constructor must exist. It creates a default allocator that doesn't |
| 94 // actually have a stack buffer. glibc's std::string() will compare the |
| 95 // current allocator against the default-constructed allocator, so this |
| 96 // should be fast. |
| 97 StackAllocator() : source_(NULL) { |
| 98 } |
| 99 |
93 explicit StackAllocator(Source* source) : source_(source) { | 100 explicit StackAllocator(Source* source) : source_(source) { |
94 } | 101 } |
95 | 102 |
96 // Actually do the allocation. Use the stack buffer if nobody has used it yet | 103 // Actually do the allocation. Use the stack buffer if nobody has used it yet |
97 // and the size requested fits. Otherwise, fall through to the standard | 104 // and the size requested fits. Otherwise, fall through to the standard |
98 // allocator. | 105 // allocator. |
99 pointer allocate(size_type n, void* hint = 0) { | 106 pointer allocate(size_type n, void* hint = 0) { |
100 if (source_ != NULL && !source_->used_stack_buffer_ | 107 if (source_ != NULL && !source_->used_stack_buffer_ |
101 && n <= stack_capacity) { | 108 && n <= stack_capacity) { |
102 source_->used_stack_buffer_ = true; | 109 source_->used_stack_buffer_ = true; |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 // operator-> (using "->at()" does exception stuff we don't want). | 256 // operator-> (using "->at()" does exception stuff we don't want). |
250 T& operator[](size_t i) { return this->container().operator[](i); } | 257 T& operator[](size_t i) { return this->container().operator[](i); } |
251 const T& operator[](size_t i) const { | 258 const T& operator[](size_t i) const { |
252 return this->container().operator[](i); | 259 return this->container().operator[](i); |
253 } | 260 } |
254 }; | 261 }; |
255 | 262 |
256 } // namespace base | 263 } // namespace base |
257 | 264 |
258 #endif // BASE_CONTAINERS_STACK_CONTAINER_H_ | 265 #endif // BASE_CONTAINERS_STACK_CONTAINER_H_ |
OLD | NEW |