blob: d951b8bbbc3c8b71d8fc0f9b2d003c8a5d823e9f [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Agopian22dbf392017-02-28 15:06:51 -080019#include <utils/VectorImpl.h>
20
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080021#include <stdio.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070022#include <stdlib.h>
23#include <string.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024
Mark Salyzyn30f991f2017-01-10 13:19:54 -080025#include <log/log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026
Sergio Girod2529f22015-09-23 16:22:59 +010027#include "SharedBuffer.h"
28
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080029/*****************************************************************************/
30
31
32namespace android {
33
34// ----------------------------------------------------------------------------
35
36const size_t kMinVectorCapacity = 4;
37
38static inline size_t max(size_t a, size_t b) {
39 return a>b ? a : b;
40}
41
42// ----------------------------------------------------------------------------
43
44VectorImpl::VectorImpl(size_t itemSize, uint32_t flags)
Yi Konge1731a42018-07-16 18:11:34 -070045 : mStorage(nullptr), mCount(0), mFlags(flags), mItemSize(itemSize)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080046{
47}
48
49VectorImpl::VectorImpl(const VectorImpl& rhs)
50 : mStorage(rhs.mStorage), mCount(rhs.mCount),
51 mFlags(rhs.mFlags), mItemSize(rhs.mItemSize)
52{
53 if (mStorage) {
Mathias Agopiane79aadd2012-08-31 16:20:23 -070054 SharedBuffer::bufferFromData(mStorage)->acquire();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080055 }
56}
57
58VectorImpl::~VectorImpl()
59{
Mathias Agopianbdf73c72012-08-09 19:39:15 -070060 ALOGW_IF(mCount,
61 "[%p] subclasses of VectorImpl must call finish_vector()"
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080062 " in their destructor. Leaking %d bytes.",
63 this, (int)(mCount*mItemSize));
Elliott Hughes643268f2018-10-08 11:10:11 -070064 // We can't call _do_destroy() here because the vtable is already gone.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065}
66
67VectorImpl& VectorImpl::operator = (const VectorImpl& rhs)
68{
Mathias Agopianbdf73c72012-08-09 19:39:15 -070069 LOG_ALWAYS_FATAL_IF(mItemSize != rhs.mItemSize,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080070 "Vector<> have different types (this=%p, rhs=%p)", this, &rhs);
71 if (this != &rhs) {
72 release_storage();
73 if (rhs.mCount) {
74 mStorage = rhs.mStorage;
75 mCount = rhs.mCount;
Mathias Agopiane79aadd2012-08-31 16:20:23 -070076 SharedBuffer::bufferFromData(mStorage)->acquire();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080077 } else {
Yi Konge1731a42018-07-16 18:11:34 -070078 mStorage = nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080079 mCount = 0;
80 }
81 }
82 return *this;
83}
84
85void* VectorImpl::editArrayImpl()
86{
87 if (mStorage) {
Narayan Kamathc609c312015-08-28 12:59:48 +010088 const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage);
89 SharedBuffer* editable = sb->attemptEdit();
Yi Konge1731a42018-07-16 18:11:34 -070090 if (editable == nullptr) {
Narayan Kamathc609c312015-08-28 12:59:48 +010091 // If we're here, we're not the only owner of the buffer.
92 // We must make a copy of it.
93 editable = SharedBuffer::alloc(sb->size());
94 // Fail instead of returning a pointer to storage that's not
95 // editable. Otherwise we'd be editing the contents of a buffer
96 // for which we're not the only owner, which is undefined behaviour.
Yi Konge1731a42018-07-16 18:11:34 -070097 LOG_ALWAYS_FATAL_IF(editable == nullptr);
Narayan Kamathc609c312015-08-28 12:59:48 +010098 _do_copy(editable->data(), mStorage, mCount);
99 release_storage();
100 mStorage = editable->data();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800101 }
102 }
103 return mStorage;
104}
105
106size_t VectorImpl::capacity() const
107{
108 if (mStorage) {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700109 return SharedBuffer::bufferFromData(mStorage)->size() / mItemSize;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800110 }
111 return 0;
112}
113
114ssize_t VectorImpl::insertVectorAt(const VectorImpl& vector, size_t index)
115{
Jeff Brown9efaaa42010-06-16 01:53:36 -0700116 return insertArrayAt(vector.arrayImpl(), index, vector.size());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800117}
118
119ssize_t VectorImpl::appendVector(const VectorImpl& vector)
120{
121 return insertVectorAt(vector, size());
122}
123
Jeff Brown9efaaa42010-06-16 01:53:36 -0700124ssize_t VectorImpl::insertArrayAt(const void* array, size_t index, size_t length)
125{
126 if (index > size())
127 return BAD_INDEX;
128 void* where = _grow(index, length);
129 if (where) {
130 _do_copy(where, array, length);
131 }
132 return where ? index : (ssize_t)NO_MEMORY;
133}
134
135ssize_t VectorImpl::appendArray(const void* array, size_t length)
136{
137 return insertArrayAt(array, size(), length);
138}
139
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800140ssize_t VectorImpl::insertAt(size_t index, size_t numItems)
141{
Yi Konge1731a42018-07-16 18:11:34 -0700142 return insertAt(nullptr, index, numItems);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800143}
144
145ssize_t VectorImpl::insertAt(const void* item, size_t index, size_t numItems)
146{
147 if (index > size())
148 return BAD_INDEX;
149 void* where = _grow(index, numItems);
150 if (where) {
151 if (item) {
152 _do_splat(where, item, numItems);
153 } else {
154 _do_construct(where, numItems);
155 }
156 }
157 return where ? index : (ssize_t)NO_MEMORY;
158}
159
160static int sortProxy(const void* lhs, const void* rhs, void* func)
161{
162 return (*(VectorImpl::compar_t)func)(lhs, rhs);
163}
164
165status_t VectorImpl::sort(VectorImpl::compar_t cmp)
166{
167 return sort(sortProxy, (void*)cmp);
168}
169
170status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state)
171{
172 // the sort must be stable. we're using insertion sort which
173 // is well suited for small and already sorted arrays
174 // for big arrays, it could be better to use mergesort
175 const ssize_t count = size();
176 if (count > 1) {
177 void* array = const_cast<void*>(arrayImpl());
Yi Konge1731a42018-07-16 18:11:34 -0700178 void* temp = nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800179 ssize_t i = 1;
180 while (i < count) {
181 void* item = reinterpret_cast<char*>(array) + mItemSize*(i);
182 void* curr = reinterpret_cast<char*>(array) + mItemSize*(i-1);
183 if (cmp(curr, item, state) > 0) {
184
185 if (!temp) {
186 // we're going to have to modify the array...
187 array = editArrayImpl();
188 if (!array) return NO_MEMORY;
189 temp = malloc(mItemSize);
190 if (!temp) return NO_MEMORY;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800191 item = reinterpret_cast<char*>(array) + mItemSize*(i);
192 curr = reinterpret_cast<char*>(array) + mItemSize*(i-1);
Mathias Agopian15d0edc2010-03-29 13:45:18 -0700193 } else {
194 _do_destroy(temp, 1);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800195 }
196
197 _do_copy(temp, item, 1);
198
199 ssize_t j = i-1;
Elliott Hughes643268f2018-10-08 11:10:11 -0700200 void* next = reinterpret_cast<char*>(array) + mItemSize*(i);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800201 do {
Mathias Agopian15d0edc2010-03-29 13:45:18 -0700202 _do_destroy(next, 1);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800203 _do_copy(next, curr, 1);
204 next = curr;
205 --j;
Yi Konge1731a42018-07-16 18:11:34 -0700206 curr = nullptr;
Nick Kralevichc76698f2015-08-28 06:40:23 -0700207 if (j >= 0) {
208 curr = reinterpret_cast<char*>(array) + mItemSize*(j);
209 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800210 } while (j>=0 && (cmp(curr, temp, state) > 0));
211
Mathias Agopian15d0edc2010-03-29 13:45:18 -0700212 _do_destroy(next, 1);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800213 _do_copy(next, temp, 1);
214 }
215 i++;
216 }
Elliott Hughes643268f2018-10-08 11:10:11 -0700217
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800218 if (temp) {
219 _do_destroy(temp, 1);
220 free(temp);
221 }
222 }
Elliott Hughes643268f2018-10-08 11:10:11 -0700223 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800224}
225
226void VectorImpl::pop()
227{
228 if (size())
229 removeItemsAt(size()-1, 1);
230}
231
232void VectorImpl::push()
233{
Yi Konge1731a42018-07-16 18:11:34 -0700234 push(nullptr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800235}
236
237void VectorImpl::push(const void* item)
238{
239 insertAt(item, size());
240}
241
242ssize_t VectorImpl::add()
243{
Yi Konge1731a42018-07-16 18:11:34 -0700244 return add(nullptr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800245}
246
Jeff Brown9efaaa42010-06-16 01:53:36 -0700247ssize_t VectorImpl::add(const void* item)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800248{
Jeff Brown9efaaa42010-06-16 01:53:36 -0700249 return insertAt(item, size());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800250}
251
252ssize_t VectorImpl::replaceAt(size_t index)
253{
Yi Konge1731a42018-07-16 18:11:34 -0700254 return replaceAt(nullptr, index);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800255}
256
257ssize_t VectorImpl::replaceAt(const void* prototype, size_t index)
258{
Steve Blockae074452012-01-09 18:35:44 +0000259 ALOG_ASSERT(index<size(),
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800260 "[%p] replace: index=%d, size=%d", this, (int)index, (int)size());
261
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700262 if (index >= size()) {
263 return BAD_INDEX;
264 }
265
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800266 void* item = editItemLocation(index);
Jeff Brownbbbd7612011-07-14 00:29:49 -0700267 if (item != prototype) {
Yi Konge1731a42018-07-16 18:11:34 -0700268 if (item == nullptr)
Jeff Brownbbbd7612011-07-14 00:29:49 -0700269 return NO_MEMORY;
270 _do_destroy(item, 1);
Yi Konge1731a42018-07-16 18:11:34 -0700271 if (prototype == nullptr) {
Jeff Brownbbbd7612011-07-14 00:29:49 -0700272 _do_construct(item, 1);
273 } else {
274 _do_copy(item, prototype, 1);
275 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800276 }
277 return ssize_t(index);
278}
279
280ssize_t VectorImpl::removeItemsAt(size_t index, size_t count)
281{
Elliott Hughesa5f2e4d2022-04-27 13:59:49 -0700282 size_t end;
283 LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(index, count, &end), "overflow: index=%zu count=%zu",
284 index, count);
285 if (end > size()) return BAD_VALUE;
286 _shrink(index, count);
287 return index;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800288}
289
290void VectorImpl::finish_vector()
291{
292 release_storage();
Yi Konge1731a42018-07-16 18:11:34 -0700293 mStorage = nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800294 mCount = 0;
295}
296
297void VectorImpl::clear()
298{
299 _shrink(0, mCount);
300}
301
302void* VectorImpl::editItemLocation(size_t index)
303{
Steve Blockae074452012-01-09 18:35:44 +0000304 ALOG_ASSERT(index<capacity(),
Mathias Agopian266a7d62011-07-11 16:26:18 -0700305 "[%p] editItemLocation: index=%d, capacity=%d, count=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800306 this, (int)index, (int)capacity(), (int)mCount);
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700307
308 if (index < capacity()) {
309 void* buffer = editArrayImpl();
310 if (buffer) {
311 return reinterpret_cast<char*>(buffer) + index*mItemSize;
312 }
313 }
Yi Konge1731a42018-07-16 18:11:34 -0700314 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800315}
316
317const void* VectorImpl::itemLocation(size_t index) const
318{
Steve Blockae074452012-01-09 18:35:44 +0000319 ALOG_ASSERT(index<capacity(),
Mathias Agopian266a7d62011-07-11 16:26:18 -0700320 "[%p] itemLocation: index=%d, capacity=%d, count=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800321 this, (int)index, (int)capacity(), (int)mCount);
322
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700323 if (index < capacity()) {
324 const void* buffer = arrayImpl();
325 if (buffer) {
326 return reinterpret_cast<const char*>(buffer) + index*mItemSize;
327 }
328 }
Yi Konge1731a42018-07-16 18:11:34 -0700329 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800330}
331
332ssize_t VectorImpl::setCapacity(size_t new_capacity)
333{
Narayan Kamathc609c312015-08-28 12:59:48 +0100334 // The capacity must always be greater than or equal to the size
335 // of this vector.
336 if (new_capacity <= size()) {
337 return capacity();
338 }
339
340 size_t new_allocation_size = 0;
Elliott Hughes85528e82018-08-28 13:38:45 -0700341 LOG_ALWAYS_FATAL_IF(__builtin_mul_overflow(new_capacity, mItemSize, &new_allocation_size));
Narayan Kamathc609c312015-08-28 12:59:48 +0100342 SharedBuffer* sb = SharedBuffer::alloc(new_allocation_size);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800343 if (sb) {
344 void* array = sb->data();
345 _do_copy(array, mStorage, size());
346 release_storage();
347 mStorage = const_cast<void*>(array);
348 } else {
349 return NO_MEMORY;
350 }
351 return new_capacity;
352}
353
Jesse Hallb73559d2013-03-11 10:16:48 -0700354ssize_t VectorImpl::resize(size_t size) {
Elliott Hughes643268f2018-10-08 11:10:11 -0700355 ssize_t result = OK;
Jesse Hallb73559d2013-03-11 10:16:48 -0700356 if (size > mCount) {
357 result = insertAt(mCount, size - mCount);
358 } else if (size < mCount) {
359 result = removeItemsAt(size, mCount - size);
360 }
361 return result < 0 ? result : size;
362}
363
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800364void VectorImpl::release_storage()
365{
366 if (mStorage) {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700367 const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800368 if (sb->release(SharedBuffer::eKeepStorage) == 1) {
369 _do_destroy(mStorage, mCount);
370 SharedBuffer::dealloc(sb);
Elliott Hughes643268f2018-10-08 11:10:11 -0700371 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800372 }
373}
374
375void* VectorImpl::_grow(size_t where, size_t amount)
376{
Steve Blockb37fbe92011-10-20 11:56:00 +0100377// ALOGV("_grow(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800378// this, (int)where, (int)amount, (int)mCount, (int)capacity());
379
Steve Blockae074452012-01-09 18:35:44 +0000380 ALOG_ASSERT(where <= mCount,
Jeff Brownaa5daed2011-07-13 22:22:02 -0700381 "[%p] _grow: where=%d, amount=%d, count=%d",
382 this, (int)where, (int)amount, (int)mCount); // caller already checked
383
Narayan Kamathc609c312015-08-28 12:59:48 +0100384 size_t new_size;
Elliott Hughes85528e82018-08-28 13:38:45 -0700385 LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(mCount, amount, &new_size), "new_size overflow");
Narayan Kamathc609c312015-08-28 12:59:48 +0100386
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800387 if (capacity() < new_size) {
Narayan Kamathc609c312015-08-28 12:59:48 +0100388 // NOTE: This implementation used to resize vectors as per ((3*x + 1) / 2)
389 // (sigh..). Also note, the " + 1" was necessary to handle the special case
390 // where x == 1, where the resized_capacity will be equal to the old
391 // capacity without the +1. The old calculation wouldn't work properly
392 // if x was zero.
393 //
394 // This approximates the old calculation, using (x + (x/2) + 1) instead.
395 size_t new_capacity = 0;
Elliott Hughes85528e82018-08-28 13:38:45 -0700396 LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(new_size, (new_size / 2), &new_capacity),
Narayan Kamathc609c312015-08-28 12:59:48 +0100397 "new_capacity overflow");
Elliott Hughes85528e82018-08-28 13:38:45 -0700398 LOG_ALWAYS_FATAL_IF(
399 __builtin_add_overflow(new_capacity, static_cast<size_t>(1u), &new_capacity),
400 "new_capacity overflow");
Narayan Kamathc609c312015-08-28 12:59:48 +0100401 new_capacity = max(kMinVectorCapacity, new_capacity);
402
403 size_t new_alloc_size = 0;
Elliott Hughes85528e82018-08-28 13:38:45 -0700404 LOG_ALWAYS_FATAL_IF(__builtin_mul_overflow(new_capacity, mItemSize, &new_alloc_size),
Narayan Kamathc609c312015-08-28 12:59:48 +0100405 "new_alloc_size overflow");
406
Elliott Hughes85528e82018-08-28 13:38:45 -0700407 // ALOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800408 if ((mStorage) &&
409 (mCount==where) &&
410 (mFlags & HAS_TRIVIAL_COPY) &&
411 (mFlags & HAS_TRIVIAL_DTOR))
412 {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700413 const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
Narayan Kamathc609c312015-08-28 12:59:48 +0100414 SharedBuffer* sb = cur_sb->editResize(new_alloc_size);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800415 if (sb) {
416 mStorage = sb->data();
417 } else {
Yi Konge1731a42018-07-16 18:11:34 -0700418 return nullptr;
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800419 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800420 } else {
Narayan Kamathc609c312015-08-28 12:59:48 +0100421 SharedBuffer* sb = SharedBuffer::alloc(new_alloc_size);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800422 if (sb) {
423 void* array = sb->data();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700424 if (where != 0) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800425 _do_copy(array, mStorage, where);
426 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700427 if (where != mCount) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800428 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + where*mItemSize;
429 void* dest = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
430 _do_copy(dest, from, mCount-where);
431 }
432 release_storage();
433 mStorage = const_cast<void*>(array);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800434 } else {
Yi Konge1731a42018-07-16 18:11:34 -0700435 return nullptr;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800436 }
437 }
438 } else {
Mathias Agopian03b168a2012-05-17 16:52:21 -0700439 void* array = editArrayImpl();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700440 if (where != mCount) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800441 const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700442 void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
443 _do_move_forward(to, from, mCount - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800444 }
445 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700446 mCount = new_size;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800447 void* free_space = const_cast<void*>(itemLocation(where));
448 return free_space;
449}
450
451void VectorImpl::_shrink(size_t where, size_t amount)
452{
453 if (!mStorage)
454 return;
455
Steve Blockb37fbe92011-10-20 11:56:00 +0100456// ALOGV("_shrink(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800457// this, (int)where, (int)amount, (int)mCount, (int)capacity());
458
Steve Blockae074452012-01-09 18:35:44 +0000459 ALOG_ASSERT(where + amount <= mCount,
Jeff Brownaa5daed2011-07-13 22:22:02 -0700460 "[%p] _shrink: where=%d, amount=%d, count=%d",
461 this, (int)where, (int)amount, (int)mCount); // caller already checked
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800462
Narayan Kamathc609c312015-08-28 12:59:48 +0100463 size_t new_size;
Elliott Hughes85528e82018-08-28 13:38:45 -0700464 LOG_ALWAYS_FATAL_IF(__builtin_sub_overflow(mCount, amount, &new_size));
Narayan Kamathc609c312015-08-28 12:59:48 +0100465
466 if (new_size < (capacity() / 2)) {
467 // NOTE: (new_size * 2) is safe because capacity didn't overflow and
468 // new_size < (capacity / 2)).
469 const size_t new_capacity = max(kMinVectorCapacity, new_size * 2);
470
471 // NOTE: (new_capacity * mItemSize), (where * mItemSize) and
472 // ((where + amount) * mItemSize) beyond this point are safe because
473 // we are always reducing the capacity of the underlying SharedBuffer.
474 // In other words, (old_capacity * mItemSize) did not overflow, and
475 // where < (where + amount) < new_capacity < old_capacity.
Jeff Brownbbbd7612011-07-14 00:29:49 -0700476 if ((where == new_size) &&
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800477 (mFlags & HAS_TRIVIAL_COPY) &&
478 (mFlags & HAS_TRIVIAL_DTOR))
479 {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700480 const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800481 SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800482 if (sb) {
483 mStorage = sb->data();
484 } else {
485 return;
486 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800487 } else {
488 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
489 if (sb) {
490 void* array = sb->data();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700491 if (where != 0) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800492 _do_copy(array, mStorage, where);
493 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700494 if (where != new_size) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800495 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + (where+amount)*mItemSize;
496 void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700497 _do_copy(dest, from, new_size - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800498 }
499 release_storage();
500 mStorage = const_cast<void*>(array);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800501 } else{
502 return;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800503 }
504 }
505 } else {
Jeff Brownbbbd7612011-07-14 00:29:49 -0700506 void* array = editArrayImpl();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800507 void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
508 _do_destroy(to, amount);
Jeff Brownbbbd7612011-07-14 00:29:49 -0700509 if (where != new_size) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800510 const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700511 _do_move_backward(to, from, new_size - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800512 }
513 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700514 mCount = new_size;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800515}
516
517size_t VectorImpl::itemSize() const {
518 return mItemSize;
519}
520
521void VectorImpl::_do_construct(void* storage, size_t num) const
522{
523 if (!(mFlags & HAS_TRIVIAL_CTOR)) {
524 do_construct(storage, num);
525 }
526}
527
528void VectorImpl::_do_destroy(void* storage, size_t num) const
529{
530 if (!(mFlags & HAS_TRIVIAL_DTOR)) {
531 do_destroy(storage, num);
532 }
533}
534
535void VectorImpl::_do_copy(void* dest, const void* from, size_t num) const
536{
537 if (!(mFlags & HAS_TRIVIAL_COPY)) {
538 do_copy(dest, from, num);
539 } else {
540 memcpy(dest, from, num*itemSize());
541 }
542}
543
544void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const {
545 do_splat(dest, item, num);
546}
547
548void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const {
549 do_move_forward(dest, from, num);
550}
551
552void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) const {
553 do_move_backward(dest, from, num);
554}
555
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800556/*****************************************************************************/
557
558SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags)
559 : VectorImpl(itemSize, flags)
560{
561}
562
563SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs)
564: VectorImpl(rhs)
565{
566}
567
568SortedVectorImpl::~SortedVectorImpl()
569{
570}
571
572SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs)
573{
574 return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) );
575}
576
577ssize_t SortedVectorImpl::indexOf(const void* item) const
578{
579 return _indexOrderOf(item);
580}
581
582size_t SortedVectorImpl::orderOf(const void* item) const
583{
584 size_t o;
585 _indexOrderOf(item, &o);
586 return o;
587}
588
589ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const
590{
Nick Kralevich1f286982015-08-22 14:27:03 -0700591 if (order) *order = 0;
592 if (isEmpty()) {
593 return NAME_NOT_FOUND;
594 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800595 // binary search
596 ssize_t err = NAME_NOT_FOUND;
597 ssize_t l = 0;
598 ssize_t h = size()-1;
599 ssize_t mid;
600 const void* a = arrayImpl();
601 const size_t s = itemSize();
602 while (l <= h) {
603 mid = l + (h - l)/2;
604 const void* const curr = reinterpret_cast<const char *>(a) + (mid*s);
605 const int c = do_compare(curr, item);
606 if (c == 0) {
607 err = l = mid;
608 break;
609 } else if (c < 0) {
610 l = mid + 1;
611 } else {
612 h = mid - 1;
613 }
614 }
615 if (order) *order = l;
616 return err;
617}
618
619ssize_t SortedVectorImpl::add(const void* item)
620{
621 size_t order;
622 ssize_t index = _indexOrderOf(item, &order);
623 if (index < 0) {
624 index = VectorImpl::insertAt(item, order, 1);
625 } else {
626 index = VectorImpl::replaceAt(item, index);
627 }
628 return index;
629}
630
631ssize_t SortedVectorImpl::merge(const VectorImpl& vector)
632{
633 // naive merge...
634 if (!vector.isEmpty()) {
635 const void* buffer = vector.arrayImpl();
636 const size_t is = itemSize();
637 size_t s = vector.size();
638 for (size_t i=0 ; i<s ; i++) {
639 ssize_t err = add( reinterpret_cast<const char*>(buffer) + i*is );
640 if (err<0) {
641 return err;
642 }
643 }
644 }
Elliott Hughes643268f2018-10-08 11:10:11 -0700645 return OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800646}
647
648ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector)
649{
650 // we've merging a sorted vector... nice!
Elliott Hughes643268f2018-10-08 11:10:11 -0700651 ssize_t err = OK;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800652 if (!vector.isEmpty()) {
653 // first take care of the case where the vectors are sorted together
654 if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) {
655 err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&>(vector), 0);
656 } else if (do_compare(vector.arrayImpl(), itemLocation(size()-1)) >= 0) {
657 err = VectorImpl::appendVector(static_cast<const VectorImpl&>(vector));
658 } else {
659 // this could be made a little better
660 err = merge(static_cast<const VectorImpl&>(vector));
661 }
662 }
663 return err;
664}
665
666ssize_t SortedVectorImpl::remove(const void* item)
667{
668 ssize_t i = indexOf(item);
669 if (i>=0) {
670 VectorImpl::removeItemsAt(i, 1);
671 }
672 return i;
673}
674
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800675/*****************************************************************************/
676
677}; // namespace android