| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2005 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #define LOG_TAG "Vector" | 
|  | 18 |  | 
| Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 19 | #include <utils/VectorImpl.h> | 
|  | 20 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <stdio.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 22 | #include <stdlib.h> | 
|  | 23 | #include <string.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 |  | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 25 | #include <log/log.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 |  | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 27 | #include <safe_iop.h> | 
|  | 28 |  | 
| Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 29 | #include "SharedBuffer.h" | 
|  | 30 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | /*****************************************************************************/ | 
|  | 32 |  | 
|  | 33 |  | 
|  | 34 | namespace android { | 
|  | 35 |  | 
|  | 36 | // ---------------------------------------------------------------------------- | 
|  | 37 |  | 
|  | 38 | const size_t kMinVectorCapacity = 4; | 
|  | 39 |  | 
|  | 40 | static inline size_t max(size_t a, size_t b) { | 
|  | 41 | return a>b ? a : b; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | // ---------------------------------------------------------------------------- | 
|  | 45 |  | 
|  | 46 | VectorImpl::VectorImpl(size_t itemSize, uint32_t flags) | 
|  | 47 | : mStorage(0), mCount(0), mFlags(flags), mItemSize(itemSize) | 
|  | 48 | { | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | VectorImpl::VectorImpl(const VectorImpl& rhs) | 
|  | 52 | :   mStorage(rhs.mStorage), mCount(rhs.mCount), | 
|  | 53 | mFlags(rhs.mFlags), mItemSize(rhs.mItemSize) | 
|  | 54 | { | 
|  | 55 | if (mStorage) { | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 56 | SharedBuffer::bufferFromData(mStorage)->acquire(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | } | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | VectorImpl::~VectorImpl() | 
|  | 61 | { | 
| Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 62 | ALOGW_IF(mCount, | 
|  | 63 | "[%p] subclasses of VectorImpl must call finish_vector()" | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | " in their destructor. Leaking %d bytes.", | 
|  | 65 | this, (int)(mCount*mItemSize)); | 
|  | 66 | // We can't call _do_destroy() here because the vtable is already gone. | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | VectorImpl& VectorImpl::operator = (const VectorImpl& rhs) | 
|  | 70 | { | 
| Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 71 | LOG_ALWAYS_FATAL_IF(mItemSize != rhs.mItemSize, | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | "Vector<> have different types (this=%p, rhs=%p)", this, &rhs); | 
|  | 73 | if (this != &rhs) { | 
|  | 74 | release_storage(); | 
|  | 75 | if (rhs.mCount) { | 
|  | 76 | mStorage = rhs.mStorage; | 
|  | 77 | mCount = rhs.mCount; | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 78 | SharedBuffer::bufferFromData(mStorage)->acquire(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | } else { | 
|  | 80 | mStorage = 0; | 
|  | 81 | mCount = 0; | 
|  | 82 | } | 
|  | 83 | } | 
|  | 84 | return *this; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | void* VectorImpl::editArrayImpl() | 
|  | 88 | { | 
|  | 89 | if (mStorage) { | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 90 | const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage); | 
|  | 91 | SharedBuffer* editable = sb->attemptEdit(); | 
|  | 92 | if (editable == 0) { | 
|  | 93 | // If we're here, we're not the only owner of the buffer. | 
|  | 94 | // We must make a copy of it. | 
|  | 95 | editable = SharedBuffer::alloc(sb->size()); | 
|  | 96 | // Fail instead of returning a pointer to storage that's not | 
|  | 97 | // editable. Otherwise we'd be editing the contents of a buffer | 
|  | 98 | // for which we're not the only owner, which is undefined behaviour. | 
|  | 99 | LOG_ALWAYS_FATAL_IF(editable == NULL); | 
|  | 100 | _do_copy(editable->data(), mStorage, mCount); | 
|  | 101 | release_storage(); | 
|  | 102 | mStorage = editable->data(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | } | 
|  | 104 | } | 
|  | 105 | return mStorage; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | size_t VectorImpl::capacity() const | 
|  | 109 | { | 
|  | 110 | if (mStorage) { | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 111 | return SharedBuffer::bufferFromData(mStorage)->size() / mItemSize; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } | 
|  | 113 | return 0; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | ssize_t VectorImpl::insertVectorAt(const VectorImpl& vector, size_t index) | 
|  | 117 | { | 
| Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 118 | return insertArrayAt(vector.arrayImpl(), index, vector.size()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
|  | 121 | ssize_t VectorImpl::appendVector(const VectorImpl& vector) | 
|  | 122 | { | 
|  | 123 | return insertVectorAt(vector, size()); | 
|  | 124 | } | 
|  | 125 |  | 
| Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 126 | ssize_t VectorImpl::insertArrayAt(const void* array, size_t index, size_t length) | 
|  | 127 | { | 
|  | 128 | if (index > size()) | 
|  | 129 | return BAD_INDEX; | 
|  | 130 | void* where = _grow(index, length); | 
|  | 131 | if (where) { | 
|  | 132 | _do_copy(where, array, length); | 
|  | 133 | } | 
|  | 134 | return where ? index : (ssize_t)NO_MEMORY; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | ssize_t VectorImpl::appendArray(const void* array, size_t length) | 
|  | 138 | { | 
|  | 139 | return insertArrayAt(array, size(), length); | 
|  | 140 | } | 
|  | 141 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | ssize_t VectorImpl::insertAt(size_t index, size_t numItems) | 
|  | 143 | { | 
|  | 144 | return insertAt(0, index, numItems); | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | ssize_t VectorImpl::insertAt(const void* item, size_t index, size_t numItems) | 
|  | 148 | { | 
|  | 149 | if (index > size()) | 
|  | 150 | return BAD_INDEX; | 
|  | 151 | void* where = _grow(index, numItems); | 
|  | 152 | if (where) { | 
|  | 153 | if (item) { | 
|  | 154 | _do_splat(where, item, numItems); | 
|  | 155 | } else { | 
|  | 156 | _do_construct(where, numItems); | 
|  | 157 | } | 
|  | 158 | } | 
|  | 159 | return where ? index : (ssize_t)NO_MEMORY; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | static int sortProxy(const void* lhs, const void* rhs, void* func) | 
|  | 163 | { | 
|  | 164 | return (*(VectorImpl::compar_t)func)(lhs, rhs); | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | status_t VectorImpl::sort(VectorImpl::compar_t cmp) | 
|  | 168 | { | 
|  | 169 | return sort(sortProxy, (void*)cmp); | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state) | 
|  | 173 | { | 
|  | 174 | // the sort must be stable. we're using insertion sort which | 
|  | 175 | // is well suited for small and already sorted arrays | 
|  | 176 | // for big arrays, it could be better to use mergesort | 
|  | 177 | const ssize_t count = size(); | 
|  | 178 | if (count > 1) { | 
|  | 179 | void* array = const_cast<void*>(arrayImpl()); | 
|  | 180 | void* temp = 0; | 
|  | 181 | ssize_t i = 1; | 
|  | 182 | while (i < count) { | 
|  | 183 | void* item = reinterpret_cast<char*>(array) + mItemSize*(i); | 
|  | 184 | void* curr = reinterpret_cast<char*>(array) + mItemSize*(i-1); | 
|  | 185 | if (cmp(curr, item, state) > 0) { | 
|  | 186 |  | 
|  | 187 | if (!temp) { | 
|  | 188 | // we're going to have to modify the array... | 
|  | 189 | array = editArrayImpl(); | 
|  | 190 | if (!array) return NO_MEMORY; | 
|  | 191 | temp = malloc(mItemSize); | 
|  | 192 | if (!temp) return NO_MEMORY; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | item = reinterpret_cast<char*>(array) + mItemSize*(i); | 
|  | 194 | curr = reinterpret_cast<char*>(array) + mItemSize*(i-1); | 
| Mathias Agopian | 15d0edc | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 195 | } else { | 
|  | 196 | _do_destroy(temp, 1); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
|  | 199 | _do_copy(temp, item, 1); | 
|  | 200 |  | 
|  | 201 | ssize_t j = i-1; | 
|  | 202 | void* next = reinterpret_cast<char*>(array) + mItemSize*(i); | 
|  | 203 | do { | 
| Mathias Agopian | 15d0edc | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 204 | _do_destroy(next, 1); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | _do_copy(next, curr, 1); | 
|  | 206 | next = curr; | 
|  | 207 | --j; | 
| Nick Kralevich | c76698f | 2015-08-28 06:40:23 -0700 | [diff] [blame] | 208 | curr = NULL; | 
|  | 209 | if (j >= 0) { | 
|  | 210 | curr = reinterpret_cast<char*>(array) + mItemSize*(j); | 
|  | 211 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | } while (j>=0 && (cmp(curr, temp, state) > 0)); | 
|  | 213 |  | 
| Mathias Agopian | 15d0edc | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 214 | _do_destroy(next, 1); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | _do_copy(next, temp, 1); | 
|  | 216 | } | 
|  | 217 | i++; | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | if (temp) { | 
|  | 221 | _do_destroy(temp, 1); | 
|  | 222 | free(temp); | 
|  | 223 | } | 
|  | 224 | } | 
|  | 225 | return NO_ERROR; | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | void VectorImpl::pop() | 
|  | 229 | { | 
|  | 230 | if (size()) | 
|  | 231 | removeItemsAt(size()-1, 1); | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | void VectorImpl::push() | 
|  | 235 | { | 
|  | 236 | push(0); | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | void VectorImpl::push(const void* item) | 
|  | 240 | { | 
|  | 241 | insertAt(item, size()); | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | ssize_t VectorImpl::add() | 
|  | 245 | { | 
|  | 246 | return add(0); | 
|  | 247 | } | 
|  | 248 |  | 
| Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 249 | ssize_t VectorImpl::add(const void* item) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | { | 
| Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 251 | return insertAt(item, size()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | } | 
|  | 253 |  | 
|  | 254 | ssize_t VectorImpl::replaceAt(size_t index) | 
|  | 255 | { | 
|  | 256 | return replaceAt(0, index); | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | ssize_t VectorImpl::replaceAt(const void* prototype, size_t index) | 
|  | 260 | { | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 261 | ALOG_ASSERT(index<size(), | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | "[%p] replace: index=%d, size=%d", this, (int)index, (int)size()); | 
|  | 263 |  | 
| Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 264 | if (index >= size()) { | 
|  | 265 | return BAD_INDEX; | 
|  | 266 | } | 
|  | 267 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | void* item = editItemLocation(index); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 269 | if (item != prototype) { | 
|  | 270 | if (item == 0) | 
|  | 271 | return NO_MEMORY; | 
|  | 272 | _do_destroy(item, 1); | 
|  | 273 | if (prototype == 0) { | 
|  | 274 | _do_construct(item, 1); | 
|  | 275 | } else { | 
|  | 276 | _do_copy(item, prototype, 1); | 
|  | 277 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | } | 
|  | 279 | return ssize_t(index); | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | ssize_t VectorImpl::removeItemsAt(size_t index, size_t count) | 
|  | 283 | { | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 284 | ALOG_ASSERT((index+count)<=size(), | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | "[%p] remove: index=%d, count=%d, size=%d", | 
|  | 286 | this, (int)index, (int)count, (int)size()); | 
|  | 287 |  | 
|  | 288 | if ((index+count) > size()) | 
|  | 289 | return BAD_VALUE; | 
|  | 290 | _shrink(index, count); | 
|  | 291 | return index; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | void VectorImpl::finish_vector() | 
|  | 295 | { | 
|  | 296 | release_storage(); | 
|  | 297 | mStorage = 0; | 
|  | 298 | mCount = 0; | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | void VectorImpl::clear() | 
|  | 302 | { | 
|  | 303 | _shrink(0, mCount); | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | void* VectorImpl::editItemLocation(size_t index) | 
|  | 307 | { | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 308 | ALOG_ASSERT(index<capacity(), | 
| Mathias Agopian | 266a7d6 | 2011-07-11 16:26:18 -0700 | [diff] [blame] | 309 | "[%p] editItemLocation: index=%d, capacity=%d, count=%d", | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | this, (int)index, (int)capacity(), (int)mCount); | 
| Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 311 |  | 
|  | 312 | if (index < capacity()) { | 
|  | 313 | void* buffer = editArrayImpl(); | 
|  | 314 | if (buffer) { | 
|  | 315 | return reinterpret_cast<char*>(buffer) + index*mItemSize; | 
|  | 316 | } | 
|  | 317 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | return 0; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | const void* VectorImpl::itemLocation(size_t index) const | 
|  | 322 | { | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 323 | ALOG_ASSERT(index<capacity(), | 
| Mathias Agopian | 266a7d6 | 2011-07-11 16:26:18 -0700 | [diff] [blame] | 324 | "[%p] itemLocation: index=%d, capacity=%d, count=%d", | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | this, (int)index, (int)capacity(), (int)mCount); | 
|  | 326 |  | 
| Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 327 | if (index < capacity()) { | 
|  | 328 | const  void* buffer = arrayImpl(); | 
|  | 329 | if (buffer) { | 
|  | 330 | return reinterpret_cast<const char*>(buffer) + index*mItemSize; | 
|  | 331 | } | 
|  | 332 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | return 0; | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | ssize_t VectorImpl::setCapacity(size_t new_capacity) | 
|  | 337 | { | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 338 | // The capacity must always be greater than or equal to the size | 
|  | 339 | // of this vector. | 
|  | 340 | if (new_capacity <= size()) { | 
|  | 341 | return capacity(); | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | size_t new_allocation_size = 0; | 
|  | 345 | LOG_ALWAYS_FATAL_IF(!safe_mul(&new_allocation_size, new_capacity, mItemSize)); | 
|  | 346 | SharedBuffer* sb = SharedBuffer::alloc(new_allocation_size); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | if (sb) { | 
|  | 348 | void* array = sb->data(); | 
|  | 349 | _do_copy(array, mStorage, size()); | 
|  | 350 | release_storage(); | 
|  | 351 | mStorage = const_cast<void*>(array); | 
|  | 352 | } else { | 
|  | 353 | return NO_MEMORY; | 
|  | 354 | } | 
|  | 355 | return new_capacity; | 
|  | 356 | } | 
|  | 357 |  | 
| Jesse Hall | b73559d | 2013-03-11 10:16:48 -0700 | [diff] [blame] | 358 | ssize_t VectorImpl::resize(size_t size) { | 
|  | 359 | ssize_t result = NO_ERROR; | 
|  | 360 | if (size > mCount) { | 
|  | 361 | result = insertAt(mCount, size - mCount); | 
|  | 362 | } else if (size < mCount) { | 
|  | 363 | result = removeItemsAt(size, mCount - size); | 
|  | 364 | } | 
|  | 365 | return result < 0 ? result : size; | 
|  | 366 | } | 
|  | 367 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | void VectorImpl::release_storage() | 
|  | 369 | { | 
|  | 370 | if (mStorage) { | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 371 | const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | if (sb->release(SharedBuffer::eKeepStorage) == 1) { | 
|  | 373 | _do_destroy(mStorage, mCount); | 
|  | 374 | SharedBuffer::dealloc(sb); | 
|  | 375 | } | 
|  | 376 | } | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | void* VectorImpl::_grow(size_t where, size_t amount) | 
|  | 380 | { | 
| Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 381 | //    ALOGV("_grow(this=%p, where=%d, amount=%d) count=%d, capacity=%d", | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | //        this, (int)where, (int)amount, (int)mCount, (int)capacity()); | 
|  | 383 |  | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 384 | ALOG_ASSERT(where <= mCount, | 
| Jeff Brown | aa5daed | 2011-07-13 22:22:02 -0700 | [diff] [blame] | 385 | "[%p] _grow: where=%d, amount=%d, count=%d", | 
|  | 386 | this, (int)where, (int)amount, (int)mCount); // caller already checked | 
|  | 387 |  | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 388 | size_t new_size; | 
|  | 389 | LOG_ALWAYS_FATAL_IF(!safe_add(&new_size, mCount, amount), "new_size overflow"); | 
|  | 390 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | if (capacity() < new_size) { | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 392 | // NOTE: This implementation used to resize vectors as per ((3*x + 1) / 2) | 
|  | 393 | // (sigh..). Also note, the " + 1" was necessary to handle the special case | 
|  | 394 | // where x == 1, where the resized_capacity will be equal to the old | 
|  | 395 | // capacity without the +1. The old calculation wouldn't work properly | 
|  | 396 | // if x was zero. | 
|  | 397 | // | 
|  | 398 | // This approximates the old calculation, using (x + (x/2) + 1) instead. | 
|  | 399 | size_t new_capacity = 0; | 
|  | 400 | LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_size, (new_size / 2)), | 
|  | 401 | "new_capacity overflow"); | 
|  | 402 | LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_capacity, static_cast<size_t>(1u)), | 
|  | 403 | "new_capacity overflow"); | 
|  | 404 | new_capacity = max(kMinVectorCapacity, new_capacity); | 
|  | 405 |  | 
|  | 406 | size_t new_alloc_size = 0; | 
|  | 407 | LOG_ALWAYS_FATAL_IF(!safe_mul(&new_alloc_size, new_capacity, mItemSize), | 
|  | 408 | "new_alloc_size overflow"); | 
|  | 409 |  | 
| Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 410 | //        ALOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | if ((mStorage) && | 
|  | 412 | (mCount==where) && | 
|  | 413 | (mFlags & HAS_TRIVIAL_COPY) && | 
|  | 414 | (mFlags & HAS_TRIVIAL_DTOR)) | 
|  | 415 | { | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 416 | const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage); | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 417 | SharedBuffer* sb = cur_sb->editResize(new_alloc_size); | 
| Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 418 | if (sb) { | 
|  | 419 | mStorage = sb->data(); | 
|  | 420 | } else { | 
|  | 421 | return NULL; | 
|  | 422 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | } else { | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 424 | SharedBuffer* sb = SharedBuffer::alloc(new_alloc_size); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | if (sb) { | 
|  | 426 | void* array = sb->data(); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 427 | if (where != 0) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | _do_copy(array, mStorage, where); | 
|  | 429 | } | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 430 | if (where != mCount) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | const void* from = reinterpret_cast<const uint8_t *>(mStorage) + where*mItemSize; | 
|  | 432 | void* dest = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; | 
|  | 433 | _do_copy(dest, from, mCount-where); | 
|  | 434 | } | 
|  | 435 | release_storage(); | 
|  | 436 | mStorage = const_cast<void*>(array); | 
| Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 437 | } else { | 
|  | 438 | return NULL; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | } | 
|  | 440 | } | 
|  | 441 | } else { | 
| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 442 | void* array = editArrayImpl(); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 443 | if (where != mCount) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize; | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 445 | void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; | 
|  | 446 | _do_move_forward(to, from, mCount - where); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | } | 
|  | 448 | } | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 449 | mCount = new_size; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | void* free_space = const_cast<void*>(itemLocation(where)); | 
|  | 451 | return free_space; | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | void VectorImpl::_shrink(size_t where, size_t amount) | 
|  | 455 | { | 
|  | 456 | if (!mStorage) | 
|  | 457 | return; | 
|  | 458 |  | 
| Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 459 | //    ALOGV("_shrink(this=%p, where=%d, amount=%d) count=%d, capacity=%d", | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | //        this, (int)where, (int)amount, (int)mCount, (int)capacity()); | 
|  | 461 |  | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 462 | ALOG_ASSERT(where + amount <= mCount, | 
| Jeff Brown | aa5daed | 2011-07-13 22:22:02 -0700 | [diff] [blame] | 463 | "[%p] _shrink: where=%d, amount=%d, count=%d", | 
|  | 464 | this, (int)where, (int)amount, (int)mCount); // caller already checked | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 |  | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 466 | size_t new_size; | 
|  | 467 | LOG_ALWAYS_FATAL_IF(!safe_sub(&new_size, mCount, amount)); | 
|  | 468 |  | 
|  | 469 | if (new_size < (capacity() / 2)) { | 
|  | 470 | // NOTE: (new_size * 2) is safe because capacity didn't overflow and | 
|  | 471 | // new_size < (capacity / 2)). | 
|  | 472 | const size_t new_capacity = max(kMinVectorCapacity, new_size * 2); | 
|  | 473 |  | 
|  | 474 | // NOTE: (new_capacity * mItemSize), (where * mItemSize) and | 
|  | 475 | // ((where + amount) * mItemSize) beyond this point are safe because | 
|  | 476 | // we are always reducing the capacity of the underlying SharedBuffer. | 
|  | 477 | // In other words, (old_capacity * mItemSize) did not overflow, and | 
|  | 478 | // where < (where + amount) < new_capacity < old_capacity. | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 479 | if ((where == new_size) && | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | (mFlags & HAS_TRIVIAL_COPY) && | 
|  | 481 | (mFlags & HAS_TRIVIAL_DTOR)) | 
|  | 482 | { | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 483 | const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize); | 
| Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 485 | if (sb) { | 
|  | 486 | mStorage = sb->data(); | 
|  | 487 | } else { | 
|  | 488 | return; | 
|  | 489 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | } else { | 
|  | 491 | SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); | 
|  | 492 | if (sb) { | 
|  | 493 | void* array = sb->data(); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 494 | if (where != 0) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | _do_copy(array, mStorage, where); | 
|  | 496 | } | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 497 | if (where != new_size) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | const void* from = reinterpret_cast<const uint8_t *>(mStorage) + (where+amount)*mItemSize; | 
|  | 499 | void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize; | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 500 | _do_copy(dest, from, new_size - where); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | } | 
|  | 502 | release_storage(); | 
|  | 503 | mStorage = const_cast<void*>(array); | 
| Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 504 | } else{ | 
|  | 505 | return; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } | 
|  | 507 | } | 
|  | 508 | } else { | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 509 | void* array = editArrayImpl(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize; | 
|  | 511 | _do_destroy(to, amount); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 512 | if (where != new_size) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 514 | _do_move_backward(to, from, new_size - where); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | } | 
|  | 516 | } | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 517 | mCount = new_size; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | } | 
|  | 519 |  | 
|  | 520 | size_t VectorImpl::itemSize() const { | 
|  | 521 | return mItemSize; | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | void VectorImpl::_do_construct(void* storage, size_t num) const | 
|  | 525 | { | 
|  | 526 | if (!(mFlags & HAS_TRIVIAL_CTOR)) { | 
|  | 527 | do_construct(storage, num); | 
|  | 528 | } | 
|  | 529 | } | 
|  | 530 |  | 
|  | 531 | void VectorImpl::_do_destroy(void* storage, size_t num) const | 
|  | 532 | { | 
|  | 533 | if (!(mFlags & HAS_TRIVIAL_DTOR)) { | 
|  | 534 | do_destroy(storage, num); | 
|  | 535 | } | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | void VectorImpl::_do_copy(void* dest, const void* from, size_t num) const | 
|  | 539 | { | 
|  | 540 | if (!(mFlags & HAS_TRIVIAL_COPY)) { | 
|  | 541 | do_copy(dest, from, num); | 
|  | 542 | } else { | 
|  | 543 | memcpy(dest, from, num*itemSize()); | 
|  | 544 | } | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const { | 
|  | 548 | do_splat(dest, item, num); | 
|  | 549 | } | 
|  | 550 |  | 
|  | 551 | void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const { | 
|  | 552 | do_move_forward(dest, from, num); | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) const { | 
|  | 556 | do_move_backward(dest, from, num); | 
|  | 557 | } | 
|  | 558 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | /*****************************************************************************/ | 
|  | 560 |  | 
|  | 561 | SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags) | 
|  | 562 | : VectorImpl(itemSize, flags) | 
|  | 563 | { | 
|  | 564 | } | 
|  | 565 |  | 
|  | 566 | SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs) | 
|  | 567 | : VectorImpl(rhs) | 
|  | 568 | { | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | SortedVectorImpl::~SortedVectorImpl() | 
|  | 572 | { | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs) | 
|  | 576 | { | 
|  | 577 | return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) ); | 
|  | 578 | } | 
|  | 579 |  | 
|  | 580 | ssize_t SortedVectorImpl::indexOf(const void* item) const | 
|  | 581 | { | 
|  | 582 | return _indexOrderOf(item); | 
|  | 583 | } | 
|  | 584 |  | 
|  | 585 | size_t SortedVectorImpl::orderOf(const void* item) const | 
|  | 586 | { | 
|  | 587 | size_t o; | 
|  | 588 | _indexOrderOf(item, &o); | 
|  | 589 | return o; | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const | 
|  | 593 | { | 
| Nick Kralevich | 1f28698 | 2015-08-22 14:27:03 -0700 | [diff] [blame] | 594 | if (order) *order = 0; | 
|  | 595 | if (isEmpty()) { | 
|  | 596 | return NAME_NOT_FOUND; | 
|  | 597 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | // binary search | 
|  | 599 | ssize_t err = NAME_NOT_FOUND; | 
|  | 600 | ssize_t l = 0; | 
|  | 601 | ssize_t h = size()-1; | 
|  | 602 | ssize_t mid; | 
|  | 603 | const void* a = arrayImpl(); | 
|  | 604 | const size_t s = itemSize(); | 
|  | 605 | while (l <= h) { | 
|  | 606 | mid = l + (h - l)/2; | 
|  | 607 | const void* const curr = reinterpret_cast<const char *>(a) + (mid*s); | 
|  | 608 | const int c = do_compare(curr, item); | 
|  | 609 | if (c == 0) { | 
|  | 610 | err = l = mid; | 
|  | 611 | break; | 
|  | 612 | } else if (c < 0) { | 
|  | 613 | l = mid + 1; | 
|  | 614 | } else { | 
|  | 615 | h = mid - 1; | 
|  | 616 | } | 
|  | 617 | } | 
|  | 618 | if (order) *order = l; | 
|  | 619 | return err; | 
|  | 620 | } | 
|  | 621 |  | 
|  | 622 | ssize_t SortedVectorImpl::add(const void* item) | 
|  | 623 | { | 
|  | 624 | size_t order; | 
|  | 625 | ssize_t index = _indexOrderOf(item, &order); | 
|  | 626 | if (index < 0) { | 
|  | 627 | index = VectorImpl::insertAt(item, order, 1); | 
|  | 628 | } else { | 
|  | 629 | index = VectorImpl::replaceAt(item, index); | 
|  | 630 | } | 
|  | 631 | return index; | 
|  | 632 | } | 
|  | 633 |  | 
|  | 634 | ssize_t SortedVectorImpl::merge(const VectorImpl& vector) | 
|  | 635 | { | 
|  | 636 | // naive merge... | 
|  | 637 | if (!vector.isEmpty()) { | 
|  | 638 | const void* buffer = vector.arrayImpl(); | 
|  | 639 | const size_t is = itemSize(); | 
|  | 640 | size_t s = vector.size(); | 
|  | 641 | for (size_t i=0 ; i<s ; i++) { | 
|  | 642 | ssize_t err = add( reinterpret_cast<const char*>(buffer) + i*is ); | 
|  | 643 | if (err<0) { | 
|  | 644 | return err; | 
|  | 645 | } | 
|  | 646 | } | 
|  | 647 | } | 
|  | 648 | return NO_ERROR; | 
|  | 649 | } | 
|  | 650 |  | 
|  | 651 | ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector) | 
|  | 652 | { | 
|  | 653 | // we've merging a sorted vector... nice! | 
|  | 654 | ssize_t err = NO_ERROR; | 
|  | 655 | if (!vector.isEmpty()) { | 
|  | 656 | // first take care of the case where the vectors are sorted together | 
|  | 657 | if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) { | 
|  | 658 | err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&>(vector), 0); | 
|  | 659 | } else if (do_compare(vector.arrayImpl(), itemLocation(size()-1)) >= 0) { | 
|  | 660 | err = VectorImpl::appendVector(static_cast<const VectorImpl&>(vector)); | 
|  | 661 | } else { | 
|  | 662 | // this could be made a little better | 
|  | 663 | err = merge(static_cast<const VectorImpl&>(vector)); | 
|  | 664 | } | 
|  | 665 | } | 
|  | 666 | return err; | 
|  | 667 | } | 
|  | 668 |  | 
|  | 669 | ssize_t SortedVectorImpl::remove(const void* item) | 
|  | 670 | { | 
|  | 671 | ssize_t i = indexOf(item); | 
|  | 672 | if (i>=0) { | 
|  | 673 | VectorImpl::removeItemsAt(i, 1); | 
|  | 674 | } | 
|  | 675 | return i; | 
|  | 676 | } | 
|  | 677 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | /*****************************************************************************/ | 
|  | 679 |  | 
|  | 680 | }; // namespace android | 
|  | 681 |  |