ipc/chromium/src/base/stack_container.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #ifndef BASE_STACK_CONTAINER_H_
michael@0 6 #define BASE_STACK_CONTAINER_H_
michael@0 7
michael@0 8 #include <string>
michael@0 9 #include <vector>
michael@0 10
michael@0 11 #include "base/basictypes.h"
michael@0 12
michael@0 13 // This allocator can be used with STL containers to provide a stack buffer
michael@0 14 // from which to allocate memory and overflows onto the heap. This stack buffer
michael@0 15 // would be allocated on the stack and allows us to avoid heap operations in
michael@0 16 // some situations.
michael@0 17 //
michael@0 18 // STL likes to make copies of allocators, so the allocator itself can't hold
michael@0 19 // the data. Instead, we make the creator responsible for creating a
michael@0 20 // StackAllocator::Source which contains the data. Copying the allocator
michael@0 21 // merely copies the pointer to this shared source, so all allocators created
michael@0 22 // based on our allocator will share the same stack buffer.
michael@0 23 //
michael@0 24 // This stack buffer implementation is very simple. The first allocation that
michael@0 25 // fits in the stack buffer will use the stack buffer. Any subsequent
michael@0 26 // allocations will not use the stack buffer, even if there is unused room.
michael@0 27 // This makes it appropriate for array-like containers, but the caller should
michael@0 28 // be sure to reserve() in the container up to the stack buffer size. Otherwise
michael@0 29 // the container will allocate a small array which will "use up" the stack
michael@0 30 // buffer.
michael@0 31 template<typename T, size_t stack_capacity>
michael@0 32 class StackAllocator : public std::allocator<T> {
michael@0 33 public:
michael@0 34 typedef typename std::allocator<T>::pointer pointer;
michael@0 35 typedef typename std::allocator<T>::size_type size_type;
michael@0 36
michael@0 37 // Backing store for the allocator. The container owner is responsible for
michael@0 38 // maintaining this for as long as any containers using this allocator are
michael@0 39 // live.
michael@0 40 struct Source {
michael@0 41 Source() : used_stack_buffer_(false) {
michael@0 42 }
michael@0 43
michael@0 44 // Casts the buffer in its right type.
michael@0 45 T* stack_buffer() { return reinterpret_cast<T*>(stack_buffer_); }
michael@0 46 const T* stack_buffer() const {
michael@0 47 return reinterpret_cast<const T*>(stack_buffer_);
michael@0 48 }
michael@0 49
michael@0 50 //
michael@0 51 // IMPORTANT: Take care to ensure that stack_buffer_ is aligned
michael@0 52 // since it is used to mimic an array of T.
michael@0 53 // Be careful while declaring any unaligned types (like bool)
michael@0 54 // before stack_buffer_.
michael@0 55 //
michael@0 56
michael@0 57 // The buffer itself. It is not of type T because we don't want the
michael@0 58 // constructors and destructors to be automatically called. Define a POD
michael@0 59 // buffer of the right size instead.
michael@0 60 char stack_buffer_[sizeof(T[stack_capacity])];
michael@0 61
michael@0 62 // Set when the stack buffer is used for an allocation. We do not track
michael@0 63 // how much of the buffer is used, only that somebody is using it.
michael@0 64 bool used_stack_buffer_;
michael@0 65 };
michael@0 66
michael@0 67 // Used by containers when they want to refer to an allocator of type U.
michael@0 68 template<typename U>
michael@0 69 struct rebind {
michael@0 70 typedef StackAllocator<U, stack_capacity> other;
michael@0 71 };
michael@0 72
michael@0 73 // For the straight up copy c-tor, we can share storage.
michael@0 74 StackAllocator(const StackAllocator<T, stack_capacity>& rhs)
michael@0 75 : source_(rhs.source_) {
michael@0 76 }
michael@0 77
michael@0 78 // ISO C++ requires the following constructor to be defined,
michael@0 79 // and std::vector in VC++2008SP1 Release fails with an error
michael@0 80 // in the class _Container_base_aux_alloc_real (from <xutility>)
michael@0 81 // if the constructor does not exist.
michael@0 82 // For this constructor, we cannot share storage; there's
michael@0 83 // no guarantee that the Source buffer of Ts is large enough
michael@0 84 // for Us.
michael@0 85 // TODO: If we were fancy pants, perhaps we could share storage
michael@0 86 // iff sizeof(T) == sizeof(U).
michael@0 87 template<typename U, size_t other_capacity>
michael@0 88 StackAllocator(const StackAllocator<U, other_capacity>& other)
michael@0 89 : source_(NULL) {
michael@0 90 }
michael@0 91
michael@0 92 explicit StackAllocator(Source* source) : source_(source) {
michael@0 93 }
michael@0 94
michael@0 95 // Actually do the allocation. Use the stack buffer if nobody has used it yet
michael@0 96 // and the size requested fits. Otherwise, fall through to the standard
michael@0 97 // allocator.
michael@0 98 pointer allocate(size_type n, void* hint = 0) {
michael@0 99 if (source_ != NULL && !source_->used_stack_buffer_
michael@0 100 && n <= stack_capacity) {
michael@0 101 source_->used_stack_buffer_ = true;
michael@0 102 return source_->stack_buffer();
michael@0 103 } else {
michael@0 104 return std::allocator<T>::allocate(n, hint);
michael@0 105 }
michael@0 106 }
michael@0 107
michael@0 108 // Free: when trying to free the stack buffer, just mark it as free. For
michael@0 109 // non-stack-buffer pointers, just fall though to the standard allocator.
michael@0 110 void deallocate(pointer p, size_type n) {
michael@0 111 if (source_ != NULL && p == source_->stack_buffer())
michael@0 112 source_->used_stack_buffer_ = false;
michael@0 113 else
michael@0 114 std::allocator<T>::deallocate(p, n);
michael@0 115 }
michael@0 116
michael@0 117 private:
michael@0 118 Source* source_;
michael@0 119 };
michael@0 120
michael@0 121 // A wrapper around STL containers that maintains a stack-sized buffer that the
michael@0 122 // initial capacity of the vector is based on. Growing the container beyond the
michael@0 123 // stack capacity will transparently overflow onto the heap. The container must
michael@0 124 // support reserve().
michael@0 125 //
michael@0 126 // WATCH OUT: the ContainerType MUST use the proper StackAllocator for this
michael@0 127 // type. This object is really intended to be used only internally. You'll want
michael@0 128 // to use the wrappers below for different types.
michael@0 129 template<typename TContainerType, int stack_capacity>
michael@0 130 class StackContainer {
michael@0 131 public:
michael@0 132 typedef TContainerType ContainerType;
michael@0 133 typedef typename ContainerType::value_type ContainedType;
michael@0 134 typedef StackAllocator<ContainedType, stack_capacity> Allocator;
michael@0 135
michael@0 136 // Allocator must be constructed before the container!
michael@0 137 StackContainer() : allocator_(&stack_data_), container_(allocator_) {
michael@0 138 // Make the container use the stack allocation by reserving our buffer size
michael@0 139 // before doing anything else.
michael@0 140 container_.reserve(stack_capacity);
michael@0 141 }
michael@0 142
michael@0 143 // Getters for the actual container.
michael@0 144 //
michael@0 145 // Danger: any copies of this made using the copy constructor must have
michael@0 146 // shorter lifetimes than the source. The copy will share the same allocator
michael@0 147 // and therefore the same stack buffer as the original. Use std::copy to
michael@0 148 // copy into a "real" container for longer-lived objects.
michael@0 149 ContainerType& container() { return container_; }
michael@0 150 const ContainerType& container() const { return container_; }
michael@0 151
michael@0 152 // Support operator-> to get to the container. This allows nicer syntax like:
michael@0 153 // StackContainer<...> foo;
michael@0 154 // std::sort(foo->begin(), foo->end());
michael@0 155 ContainerType* operator->() { return &container_; }
michael@0 156 const ContainerType* operator->() const { return &container_; }
michael@0 157
michael@0 158 #ifdef UNIT_TEST
michael@0 159 // Retrieves the stack source so that that unit tests can verify that the
michael@0 160 // buffer is being used properly.
michael@0 161 const typename Allocator::Source& stack_data() const {
michael@0 162 return stack_data_;
michael@0 163 }
michael@0 164 #endif
michael@0 165
michael@0 166 protected:
michael@0 167 typename Allocator::Source stack_data_;
michael@0 168 Allocator allocator_;
michael@0 169 ContainerType container_;
michael@0 170
michael@0 171 DISALLOW_EVIL_CONSTRUCTORS(StackContainer);
michael@0 172 };
michael@0 173
michael@0 174 // StackString
michael@0 175 template<size_t stack_capacity>
michael@0 176 class StackString : public StackContainer<
michael@0 177 std::basic_string<char,
michael@0 178 std::char_traits<char>,
michael@0 179 StackAllocator<char, stack_capacity> >,
michael@0 180 stack_capacity> {
michael@0 181 public:
michael@0 182 StackString() : StackContainer<
michael@0 183 std::basic_string<char,
michael@0 184 std::char_traits<char>,
michael@0 185 StackAllocator<char, stack_capacity> >,
michael@0 186 stack_capacity>() {
michael@0 187 }
michael@0 188
michael@0 189 private:
michael@0 190 DISALLOW_EVIL_CONSTRUCTORS(StackString);
michael@0 191 };
michael@0 192
michael@0 193 // StackWString
michael@0 194 template<size_t stack_capacity>
michael@0 195 class StackWString : public StackContainer<
michael@0 196 std::basic_string<wchar_t,
michael@0 197 std::char_traits<wchar_t>,
michael@0 198 StackAllocator<wchar_t, stack_capacity> >,
michael@0 199 stack_capacity> {
michael@0 200 public:
michael@0 201 StackWString() : StackContainer<
michael@0 202 std::basic_string<wchar_t,
michael@0 203 std::char_traits<wchar_t>,
michael@0 204 StackAllocator<wchar_t, stack_capacity> >,
michael@0 205 stack_capacity>() {
michael@0 206 }
michael@0 207
michael@0 208 private:
michael@0 209 DISALLOW_EVIL_CONSTRUCTORS(StackWString);
michael@0 210 };
michael@0 211
michael@0 212 // StackVector
michael@0 213 //
michael@0 214 // Example:
michael@0 215 // StackVector<int, 16> foo;
michael@0 216 // foo->push_back(22); // we have overloaded operator->
michael@0 217 // foo[0] = 10; // as well as operator[]
michael@0 218 template<typename T, size_t stack_capacity>
michael@0 219 class StackVector : public StackContainer<
michael@0 220 std::vector<T, StackAllocator<T, stack_capacity> >,
michael@0 221 stack_capacity> {
michael@0 222 public:
michael@0 223 StackVector() : StackContainer<
michael@0 224 std::vector<T, StackAllocator<T, stack_capacity> >,
michael@0 225 stack_capacity>() {
michael@0 226 }
michael@0 227
michael@0 228 // We need to put this in STL containers sometimes, which requires a copy
michael@0 229 // constructor. We can't call the regular copy constructor because that will
michael@0 230 // take the stack buffer from the original. Here, we create an empty object
michael@0 231 // and make a stack buffer of its own.
michael@0 232 StackVector(const StackVector<T, stack_capacity>& other)
michael@0 233 : StackContainer<
michael@0 234 std::vector<T, StackAllocator<T, stack_capacity> >,
michael@0 235 stack_capacity>() {
michael@0 236 this->container().assign(other->begin(), other->end());
michael@0 237 }
michael@0 238
michael@0 239 StackVector<T, stack_capacity>& operator=(
michael@0 240 const StackVector<T, stack_capacity>& other) {
michael@0 241 this->container().assign(other->begin(), other->end());
michael@0 242 return *this;
michael@0 243 }
michael@0 244
michael@0 245 // Vectors are commonly indexed, which isn't very convenient even with
michael@0 246 // operator-> (using "->at()" does exception stuff we don't want).
michael@0 247 T& operator[](size_t i) { return this->container().operator[](i); }
michael@0 248 const T& operator[](size_t i) const {
michael@0 249 return this->container().operator[](i);
michael@0 250 }
michael@0 251 };
michael@0 252
michael@0 253 #endif // BASE_STACK_CONTAINER_H_

mercurial