summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNarayan Kamath2015-09-08 07:59:49 -0500
committerNarayan Kamath2015-09-08 07:59:49 -0500
commit51e740f802083cc250a2f6228222bcbbcec79887 (patch)
tree783b5fc2e126ed2d914f26e579426ebeb398be39 /libutils/VectorImpl.cpp
parentbeb81f48d8eacdb9e29e369a911c1195e5c7a76d (diff)
parent32d86587f32b5c35f9294ed1d9d27446d638de65 (diff)
downloadplatform-system-core-51e740f802083cc250a2f6228222bcbbcec79887.tar.gz
platform-system-core-51e740f802083cc250a2f6228222bcbbcec79887.tar.xz
platform-system-core-51e740f802083cc250a2f6228222bcbbcec79887.zip
resolved conflicts for 32d86587 to mnc-dr-dev-plus-aosp
Change-Id: I381133e7dd93f908bd3d2b42e8af83d954e428af
Diffstat (limited to 'libutils/VectorImpl.cpp')
-rw-r--r--libutils/VectorImpl.cpp82
1 files changed, 59 insertions, 23 deletions
diff --git a/libutils/VectorImpl.cpp b/libutils/VectorImpl.cpp
index 2f770f590..7c5347299 100644
--- a/libutils/VectorImpl.cpp
+++ b/libutils/VectorImpl.cpp
@@ -21,6 +21,7 @@
21#include <stdio.h> 21#include <stdio.h>
22 22
23#include <cutils/log.h> 23#include <cutils/log.h>
24#include <safe_iop.h>
24 25
25#include <utils/Errors.h> 26#include <utils/Errors.h>
26#include <utils/SharedBuffer.h> 27#include <utils/SharedBuffer.h>
@@ -85,14 +86,19 @@ VectorImpl& VectorImpl::operator = (const VectorImpl& rhs)
85void* VectorImpl::editArrayImpl() 86void* VectorImpl::editArrayImpl()
86{ 87{
87 if (mStorage) { 88 if (mStorage) {
88 SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage)->attemptEdit(); 89 const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage);
89 if (sb == 0) { 90 SharedBuffer* editable = sb->attemptEdit();
90 sb = SharedBuffer::alloc(capacity() * mItemSize); 91 if (editable == 0) {
91 if (sb) { 92 // If we're here, we're not the only owner of the buffer.
92 _do_copy(sb->data(), mStorage, mCount); 93 // We must make a copy of it.
93 release_storage(); 94 editable = SharedBuffer::alloc(sb->size());
94 mStorage = sb->data(); 95 // Fail instead of returning a pointer to storage that's not
95 } 96 // editable. Otherwise we'd be editing the contents of a buffer
97 // for which we're not the only owner, which is undefined behaviour.
98 LOG_ALWAYS_FATAL_IF(editable == NULL);
99 _do_copy(editable->data(), mStorage, mCount);
100 release_storage();
101 mStorage = editable->data();
96 } 102 }
97 } 103 }
98 return mStorage; 104 return mStorage;
@@ -328,13 +334,15 @@ const void* VectorImpl::itemLocation(size_t index) const
328 334
329ssize_t VectorImpl::setCapacity(size_t new_capacity) 335ssize_t VectorImpl::setCapacity(size_t new_capacity)
330{ 336{
331 size_t current_capacity = capacity(); 337 // The capacity must always be greater than or equal to the size
332 ssize_t amount = new_capacity - size(); 338 // of this vector.
333 if (amount <= 0) { 339 if (new_capacity <= size()) {
334 // we can't reduce the capacity 340 return capacity();
335 return current_capacity; 341 }
336 } 342
337 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); 343 size_t new_allocation_size = 0;
344 LOG_ALWAYS_FATAL_IF(!safe_mul(&new_allocation_size, new_capacity, mItemSize));
345 SharedBuffer* sb = SharedBuffer::alloc(new_allocation_size);
338 if (sb) { 346 if (sb) {
339 void* array = sb->data(); 347 void* array = sb->data();
340 _do_copy(array, mStorage, size()); 348 _do_copy(array, mStorage, size());
@@ -376,9 +384,28 @@ void* VectorImpl::_grow(size_t where, size_t amount)
376 "[%p] _grow: where=%d, amount=%d, count=%d", 384 "[%p] _grow: where=%d, amount=%d, count=%d",
377 this, (int)where, (int)amount, (int)mCount); // caller already checked 385 this, (int)where, (int)amount, (int)mCount); // caller already checked
378 386
379 const size_t new_size = mCount + amount; 387 size_t new_size;
388 LOG_ALWAYS_FATAL_IF(!safe_add(&new_size, mCount, amount), "new_size overflow");
389
380 if (capacity() < new_size) { 390 if (capacity() < new_size) {
381 const size_t new_capacity = max(kMinVectorCapacity, ((new_size*3)+1)/2); 391 // NOTE: This implementation used to resize vectors as per ((3*x + 1) / 2)
392 // (sigh..). Also note, the " + 1" was necessary to handle the special case
393 // where x == 1, where the resized_capacity will be equal to the old
394 // capacity without the +1. The old calculation wouldn't work properly
395 // if x was zero.
396 //
397 // This approximates the old calculation, using (x + (x/2) + 1) instead.
398 size_t new_capacity = 0;
399 LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_size, (new_size / 2)),
400 "new_capacity overflow");
401 LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_capacity, static_cast<size_t>(1u)),
402 "new_capacity overflow");
403 new_capacity = max(kMinVectorCapacity, new_capacity);
404
405 size_t new_alloc_size = 0;
406 LOG_ALWAYS_FATAL_IF(!safe_mul(&new_alloc_size, new_capacity, mItemSize),
407 "new_alloc_size overflow");
408
382// ALOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity); 409// ALOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity);
383 if ((mStorage) && 410 if ((mStorage) &&
384 (mCount==where) && 411 (mCount==where) &&
@@ -386,14 +413,14 @@ void* VectorImpl::_grow(size_t where, size_t amount)
386 (mFlags & HAS_TRIVIAL_DTOR)) 413 (mFlags & HAS_TRIVIAL_DTOR))
387 { 414 {
388 const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage); 415 const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
389 SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize); 416 SharedBuffer* sb = cur_sb->editResize(new_alloc_size);
390 if (sb) { 417 if (sb) {
391 mStorage = sb->data(); 418 mStorage = sb->data();
392 } else { 419 } else {
393 return NULL; 420 return NULL;
394 } 421 }
395 } else { 422 } else {
396 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); 423 SharedBuffer* sb = SharedBuffer::alloc(new_alloc_size);
397 if (sb) { 424 if (sb) {
398 void* array = sb->data(); 425 void* array = sb->data();
399 if (where != 0) { 426 if (where != 0) {
@@ -435,10 +462,19 @@ void VectorImpl::_shrink(size_t where, size_t amount)
435 "[%p] _shrink: where=%d, amount=%d, count=%d", 462 "[%p] _shrink: where=%d, amount=%d, count=%d",
436 this, (int)where, (int)amount, (int)mCount); // caller already checked 463 this, (int)where, (int)amount, (int)mCount); // caller already checked
437 464
438 const size_t new_size = mCount - amount; 465 size_t new_size;
439 if (new_size*3 < capacity()) { 466 LOG_ALWAYS_FATAL_IF(!safe_sub(&new_size, mCount, amount));
440 const size_t new_capacity = max(kMinVectorCapacity, new_size*2); 467
441// ALOGV("shrink vector %p, new_capacity=%d", this, (int)new_capacity); 468 if (new_size < (capacity() / 2)) {
469 // NOTE: (new_size * 2) is safe because capacity didn't overflow and
470 // new_size < (capacity / 2)).
471 const size_t new_capacity = max(kMinVectorCapacity, new_size * 2);
472
473 // NOTE: (new_capacity * mItemSize), (where * mItemSize) and
474 // ((where + amount) * mItemSize) beyond this point are safe because
475 // we are always reducing the capacity of the underlying SharedBuffer.
476 // In other words, (old_capacity * mItemSize) did not overflow, and
477 // where < (where + amount) < new_capacity < old_capacity.
442 if ((where == new_size) && 478 if ((where == new_size) &&
443 (mFlags & HAS_TRIVIAL_COPY) && 479 (mFlags & HAS_TRIVIAL_COPY) &&
444 (mFlags & HAS_TRIVIAL_DTOR)) 480 (mFlags & HAS_TRIVIAL_DTOR))