media/omx-plugin/include/gb/utils/Vector.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*
michael@0 2 * Copyright (C) 2005 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef ANDROID_VECTOR_H
michael@0 18 #define ANDROID_VECTOR_H
michael@0 19
michael@0 20 #include <new>
michael@0 21 #include <stdint.h>
michael@0 22 #include <sys/types.h>
michael@0 23
michael@0 24 #include <utils/Log.h>
michael@0 25 #include <utils/VectorImpl.h>
michael@0 26 #include <utils/TypeHelpers.h>
michael@0 27
michael@0 28 // ---------------------------------------------------------------------------
michael@0 29
michael@0 30 namespace android {
michael@0 31
michael@0 32 /*!
michael@0 33 * The main templated vector class ensuring type safety
michael@0 34 * while making use of VectorImpl.
michael@0 35 * This is the class users want to use.
michael@0 36 */
michael@0 37
michael@0 38 template <class TYPE>
michael@0 39 class Vector : private VectorImpl
michael@0 40 {
michael@0 41 public:
michael@0 42 typedef TYPE value_type;
michael@0 43
michael@0 44 /*!
michael@0 45 * Constructors and destructors
michael@0 46 */
michael@0 47
michael@0 48 Vector();
michael@0 49 Vector(const Vector<TYPE>& rhs);
michael@0 50 virtual ~Vector();
michael@0 51
michael@0 52 /*! copy operator */
michael@0 53 const Vector<TYPE>& operator = (const Vector<TYPE>& rhs) const;
michael@0 54 Vector<TYPE>& operator = (const Vector<TYPE>& rhs);
michael@0 55
michael@0 56 /*
michael@0 57 * empty the vector
michael@0 58 */
michael@0 59
michael@0 60 inline void clear() { VectorImpl::clear(); }
michael@0 61
michael@0 62 /*!
michael@0 63 * vector stats
michael@0 64 */
michael@0 65
michael@0 66 //! returns number of items in the vector
michael@0 67 inline size_t size() const { return VectorImpl::size(); }
michael@0 68 //! returns wether or not the vector is empty
michael@0 69 inline bool isEmpty() const { return VectorImpl::isEmpty(); }
michael@0 70 //! returns how many items can be stored without reallocating the backing store
michael@0 71 inline size_t capacity() const { return VectorImpl::capacity(); }
michael@0 72 //! setst the capacity. capacity can never be reduced less than size()
michael@0 73 inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); }
michael@0 74
michael@0 75 /*!
michael@0 76 * C-style array access
michael@0 77 */
michael@0 78
michael@0 79 //! read-only C-style access
michael@0 80 inline const TYPE* array() const;
michael@0 81 //! read-write C-style access
michael@0 82 TYPE* editArray();
michael@0 83
michael@0 84 /*!
michael@0 85 * accessors
michael@0 86 */
michael@0 87
michael@0 88 //! read-only access to an item at a given index
michael@0 89 inline const TYPE& operator [] (size_t index) const;
michael@0 90 //! alternate name for operator []
michael@0 91 inline const TYPE& itemAt(size_t index) const;
michael@0 92 //! stack-usage of the vector. returns the top of the stack (last element)
michael@0 93 const TYPE& top() const;
michael@0 94 //! same as operator [], but allows to access the vector backward (from the end) with a negative index
michael@0 95 const TYPE& mirrorItemAt(ssize_t index) const;
michael@0 96
michael@0 97 /*!
michael@0 98 * modifing the array
michael@0 99 */
michael@0 100
michael@0 101 //! copy-on write support, grants write access to an item
michael@0 102 TYPE& editItemAt(size_t index);
michael@0 103 //! grants right acces to the top of the stack (last element)
michael@0 104 TYPE& editTop();
michael@0 105
michael@0 106 /*!
michael@0 107 * append/insert another vector
michael@0 108 */
michael@0 109
michael@0 110 //! insert another vector at a given index
michael@0 111 ssize_t insertVectorAt(const Vector<TYPE>& vector, size_t index);
michael@0 112
michael@0 113 //! append another vector at the end of this one
michael@0 114 ssize_t appendVector(const Vector<TYPE>& vector);
michael@0 115
michael@0 116
michael@0 117 //! insert an array at a given index
michael@0 118 ssize_t insertArrayAt(const TYPE* array, size_t index, size_t length);
michael@0 119
michael@0 120 //! append an array at the end of this vector
michael@0 121 ssize_t appendArray(const TYPE* array, size_t length);
michael@0 122
michael@0 123 /*!
michael@0 124 * add/insert/replace items
michael@0 125 */
michael@0 126
michael@0 127 //! insert one or several items initialized with their default constructor
michael@0 128 inline ssize_t insertAt(size_t index, size_t numItems = 1);
michael@0 129 //! insert one or several items initialized from a prototype item
michael@0 130 ssize_t insertAt(const TYPE& prototype_item, size_t index, size_t numItems = 1);
michael@0 131 //! pop the top of the stack (removes the last element). No-op if the stack's empty
michael@0 132 inline void pop();
michael@0 133 //! pushes an item initialized with its default constructor
michael@0 134 inline void push();
michael@0 135 //! pushes an item on the top of the stack
michael@0 136 void push(const TYPE& item);
michael@0 137 //! same as push() but returns the index the item was added at (or an error)
michael@0 138 inline ssize_t add();
michael@0 139 //! same as push() but returns the index the item was added at (or an error)
michael@0 140 ssize_t add(const TYPE& item);
michael@0 141 //! replace an item with a new one initialized with its default constructor
michael@0 142 inline ssize_t replaceAt(size_t index);
michael@0 143 //! replace an item with a new one
michael@0 144 ssize_t replaceAt(const TYPE& item, size_t index);
michael@0 145
michael@0 146 /*!
michael@0 147 * remove items
michael@0 148 */
michael@0 149
michael@0 150 //! remove several items
michael@0 151 inline ssize_t removeItemsAt(size_t index, size_t count = 1);
michael@0 152 //! remove one item
michael@0 153 inline ssize_t removeAt(size_t index) { return removeItemsAt(index); }
michael@0 154
michael@0 155 /*!
michael@0 156 * sort (stable) the array
michael@0 157 */
michael@0 158
michael@0 159 typedef int (*compar_t)(const TYPE* lhs, const TYPE* rhs);
michael@0 160 typedef int (*compar_r_t)(const TYPE* lhs, const TYPE* rhs, void* state);
michael@0 161
michael@0 162 inline status_t sort(compar_t cmp);
michael@0 163 inline status_t sort(compar_r_t cmp, void* state);
michael@0 164
michael@0 165 protected:
michael@0 166 virtual void do_construct(void* storage, size_t num) const;
michael@0 167 virtual void do_destroy(void* storage, size_t num) const;
michael@0 168 virtual void do_copy(void* dest, const void* from, size_t num) const;
michael@0 169 virtual void do_splat(void* dest, const void* item, size_t num) const;
michael@0 170 virtual void do_move_forward(void* dest, const void* from, size_t num) const;
michael@0 171 virtual void do_move_backward(void* dest, const void* from, size_t num) const;
michael@0 172 };
michael@0 173
michael@0 174
michael@0 175 // ---------------------------------------------------------------------------
michael@0 176 // No user serviceable parts from here...
michael@0 177 // ---------------------------------------------------------------------------
michael@0 178
michael@0 179 template<class TYPE> inline
michael@0 180 Vector<TYPE>::Vector()
michael@0 181 : VectorImpl(sizeof(TYPE),
michael@0 182 ((traits<TYPE>::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0)
michael@0 183 |(traits<TYPE>::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0)
michael@0 184 |(traits<TYPE>::has_trivial_copy ? HAS_TRIVIAL_COPY : 0))
michael@0 185 )
michael@0 186 {
michael@0 187 }
michael@0 188
michael@0 189 template<class TYPE> inline
michael@0 190 Vector<TYPE>::Vector(const Vector<TYPE>& rhs)
michael@0 191 : VectorImpl(rhs) {
michael@0 192 }
michael@0 193
michael@0 194 template<class TYPE> inline
michael@0 195 Vector<TYPE>::~Vector() {
michael@0 196 finish_vector();
michael@0 197 }
michael@0 198
michael@0 199 template<class TYPE> inline
michael@0 200 Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) {
michael@0 201 VectorImpl::operator = (rhs);
michael@0 202 return *this;
michael@0 203 }
michael@0 204
michael@0 205 template<class TYPE> inline
michael@0 206 const Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) const {
michael@0 207 VectorImpl::operator = (rhs);
michael@0 208 return *this;
michael@0 209 }
michael@0 210
michael@0 211 template<class TYPE> inline
michael@0 212 const TYPE* Vector<TYPE>::array() const {
michael@0 213 return static_cast<const TYPE *>(arrayImpl());
michael@0 214 }
michael@0 215
michael@0 216 template<class TYPE> inline
michael@0 217 TYPE* Vector<TYPE>::editArray() {
michael@0 218 return static_cast<TYPE *>(editArrayImpl());
michael@0 219 }
michael@0 220
michael@0 221
michael@0 222 template<class TYPE> inline
michael@0 223 const TYPE& Vector<TYPE>::operator[](size_t index) const {
michael@0 224 LOG_FATAL_IF( index>=size(),
michael@0 225 "itemAt: index %d is past size %d", (int)index, (int)size() );
michael@0 226 return *(array() + index);
michael@0 227 }
michael@0 228
michael@0 229 template<class TYPE> inline
michael@0 230 const TYPE& Vector<TYPE>::itemAt(size_t index) const {
michael@0 231 return operator[](index);
michael@0 232 }
michael@0 233
michael@0 234 template<class TYPE> inline
michael@0 235 const TYPE& Vector<TYPE>::mirrorItemAt(ssize_t index) const {
michael@0 236 LOG_FATAL_IF( (index>0 ? index : -index)>=size(),
michael@0 237 "mirrorItemAt: index %d is past size %d",
michael@0 238 (int)index, (int)size() );
michael@0 239 return *(array() + ((index<0) ? (size()-index) : index));
michael@0 240 }
michael@0 241
michael@0 242 template<class TYPE> inline
michael@0 243 const TYPE& Vector<TYPE>::top() const {
michael@0 244 return *(array() + size() - 1);
michael@0 245 }
michael@0 246
michael@0 247 template<class TYPE> inline
michael@0 248 TYPE& Vector<TYPE>::editItemAt(size_t index) {
michael@0 249 return *( static_cast<TYPE *>(editItemLocation(index)) );
michael@0 250 }
michael@0 251
michael@0 252 template<class TYPE> inline
michael@0 253 TYPE& Vector<TYPE>::editTop() {
michael@0 254 return *( static_cast<TYPE *>(editItemLocation(size()-1)) );
michael@0 255 }
michael@0 256
michael@0 257 template<class TYPE> inline
michael@0 258 ssize_t Vector<TYPE>::insertVectorAt(const Vector<TYPE>& vector, size_t index) {
michael@0 259 return VectorImpl::insertVectorAt(reinterpret_cast<const VectorImpl&>(vector), index);
michael@0 260 }
michael@0 261
michael@0 262 template<class TYPE> inline
michael@0 263 ssize_t Vector<TYPE>::appendVector(const Vector<TYPE>& vector) {
michael@0 264 return VectorImpl::appendVector(reinterpret_cast<const VectorImpl&>(vector));
michael@0 265 }
michael@0 266
michael@0 267 template<class TYPE> inline
michael@0 268 ssize_t Vector<TYPE>::insertArrayAt(const TYPE* array, size_t index, size_t length) {
michael@0 269 return VectorImpl::insertArrayAt(array, index, length);
michael@0 270 }
michael@0 271
michael@0 272 template<class TYPE> inline
michael@0 273 ssize_t Vector<TYPE>::appendArray(const TYPE* array, size_t length) {
michael@0 274 return VectorImpl::appendArray(array, length);
michael@0 275 }
michael@0 276
michael@0 277 template<class TYPE> inline
michael@0 278 ssize_t Vector<TYPE>::insertAt(const TYPE& item, size_t index, size_t numItems) {
michael@0 279 return VectorImpl::insertAt(&item, index, numItems);
michael@0 280 }
michael@0 281
michael@0 282 template<class TYPE> inline
michael@0 283 void Vector<TYPE>::push(const TYPE& item) {
michael@0 284 return VectorImpl::push(&item);
michael@0 285 }
michael@0 286
michael@0 287 template<class TYPE> inline
michael@0 288 ssize_t Vector<TYPE>::add(const TYPE& item) {
michael@0 289 return VectorImpl::add(&item);
michael@0 290 }
michael@0 291
michael@0 292 template<class TYPE> inline
michael@0 293 ssize_t Vector<TYPE>::replaceAt(const TYPE& item, size_t index) {
michael@0 294 return VectorImpl::replaceAt(&item, index);
michael@0 295 }
michael@0 296
michael@0 297 template<class TYPE> inline
michael@0 298 ssize_t Vector<TYPE>::insertAt(size_t index, size_t numItems) {
michael@0 299 return VectorImpl::insertAt(index, numItems);
michael@0 300 }
michael@0 301
michael@0 302 template<class TYPE> inline
michael@0 303 void Vector<TYPE>::pop() {
michael@0 304 VectorImpl::pop();
michael@0 305 }
michael@0 306
michael@0 307 template<class TYPE> inline
michael@0 308 void Vector<TYPE>::push() {
michael@0 309 VectorImpl::push();
michael@0 310 }
michael@0 311
michael@0 312 template<class TYPE> inline
michael@0 313 ssize_t Vector<TYPE>::add() {
michael@0 314 return VectorImpl::add();
michael@0 315 }
michael@0 316
michael@0 317 template<class TYPE> inline
michael@0 318 ssize_t Vector<TYPE>::replaceAt(size_t index) {
michael@0 319 return VectorImpl::replaceAt(index);
michael@0 320 }
michael@0 321
michael@0 322 template<class TYPE> inline
michael@0 323 ssize_t Vector<TYPE>::removeItemsAt(size_t index, size_t count) {
michael@0 324 return VectorImpl::removeItemsAt(index, count);
michael@0 325 }
michael@0 326
michael@0 327 template<class TYPE> inline
michael@0 328 status_t Vector<TYPE>::sort(Vector<TYPE>::compar_t cmp) {
michael@0 329 return VectorImpl::sort((VectorImpl::compar_t)cmp);
michael@0 330 }
michael@0 331
michael@0 332 template<class TYPE> inline
michael@0 333 status_t Vector<TYPE>::sort(Vector<TYPE>::compar_r_t cmp, void* state) {
michael@0 334 return VectorImpl::sort((VectorImpl::compar_r_t)cmp, state);
michael@0 335 }
michael@0 336
michael@0 337 // ---------------------------------------------------------------------------
michael@0 338
michael@0 339 template<class TYPE>
michael@0 340 void Vector<TYPE>::do_construct(void* storage, size_t num) const {
michael@0 341 construct_type( reinterpret_cast<TYPE*>(storage), num );
michael@0 342 }
michael@0 343
michael@0 344 template<class TYPE>
michael@0 345 void Vector<TYPE>::do_destroy(void* storage, size_t num) const {
michael@0 346 destroy_type( reinterpret_cast<TYPE*>(storage), num );
michael@0 347 }
michael@0 348
michael@0 349 template<class TYPE>
michael@0 350 void Vector<TYPE>::do_copy(void* dest, const void* from, size_t num) const {
michael@0 351 copy_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
michael@0 352 }
michael@0 353
michael@0 354 template<class TYPE>
michael@0 355 void Vector<TYPE>::do_splat(void* dest, const void* item, size_t num) const {
michael@0 356 splat_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(item), num );
michael@0 357 }
michael@0 358
michael@0 359 template<class TYPE>
michael@0 360 void Vector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const {
michael@0 361 move_forward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
michael@0 362 }
michael@0 363
michael@0 364 template<class TYPE>
michael@0 365 void Vector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const {
michael@0 366 move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
michael@0 367 }
michael@0 368
michael@0 369 }; // namespace android
michael@0 370
michael@0 371
michael@0 372 // ---------------------------------------------------------------------------
michael@0 373
michael@0 374 #endif // ANDROID_VECTOR_H

mercurial