diff --git a/include/utils/KeyedVector.h b/include/utils/KeyedVector.h index 47c2c56de..c4faae0b7 100644 --- a/include/utils/KeyedVector.h +++ b/include/utils/KeyedVector.h @@ -21,6 +21,8 @@ #include #include +#include + #include #include #include @@ -50,11 +52,11 @@ public: //! returns number of items in the vector inline size_t size() const { return mVector.size(); } - //! returns wether or not the vector is empty + //! returns whether or not the vector is empty inline bool isEmpty() const { return mVector.isEmpty(); } //! returns how many items can be stored without reallocating the backing store inline size_t capacity() const { return mVector.capacity(); } - //! setst the capacity. capacity can never be reduced less than size() + //! sets the capacity. capacity can never be reduced less than size() inline ssize_t setCapacity(size_t size) { return mVector.setCapacity(size); } // returns true if the arguments is known to be identical to this vector @@ -139,7 +141,7 @@ ssize_t KeyedVector::indexOfKey(const KEY& key) const { template inline const VALUE& KeyedVector::valueFor(const KEY& key) const { ssize_t i = this->indexOfKey(key); - assert(i>=0); + LOG_ALWAYS_FATAL_IF(i<0, "%s: key not found", __PRETTY_FUNCTION__); return mVector.itemAt(i).value; } @@ -161,7 +163,7 @@ const KEY& KeyedVector::keyAt(size_t index) const { template inline VALUE& KeyedVector::editValueFor(const KEY& key) { ssize_t i = this->indexOfKey(key); - assert(i>=0); + LOG_ALWAYS_FATAL_IF(i<0, "%s: key not found", __PRETTY_FUNCTION__); return mVector.editItemAt(i).value; } diff --git a/include/utils/SortedVector.h b/include/utils/SortedVector.h index 24455253c..fd1cb8274 100644 --- a/include/utils/SortedVector.h +++ b/include/utils/SortedVector.h @@ -21,6 +21,8 @@ #include #include +#include + #include #include #include @@ -61,11 +63,11 @@ public: //! returns number of items in the vector inline size_t size() const { return VectorImpl::size(); } - //! returns wether or not the vector is empty + //! returns whether or not the vector is empty inline bool isEmpty() const { return VectorImpl::isEmpty(); } //! returns how many items can be stored without reallocating the backing store inline size_t capacity() const { return VectorImpl::capacity(); } - //! setst the capacity. capacity can never be reduced less than size() + //! sets the capacity. capacity can never be reduced less than size() inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); } /*! @@ -76,7 +78,7 @@ public: inline const TYPE* array() const; //! read-write C-style access. BE VERY CAREFUL when modifying the array - //! you ust keep it sorted! You usually don't use this function. + //! you must keep it sorted! You usually don't use this function. TYPE* editArray(); //! finds the index of an item @@ -100,7 +102,7 @@ public: const TYPE& mirrorItemAt(ssize_t index) const; /*! - * modifing the array + * modifying the array */ //! add an item in the right place (and replace the one that is there) @@ -186,7 +188,9 @@ TYPE* SortedVector::editArray() { template inline const TYPE& SortedVector::operator[](size_t index) const { - assert( index=size(), + "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__, + int(index), int(size())); return *(array() + index); } @@ -197,8 +201,11 @@ const TYPE& SortedVector::itemAt(size_t index) const { template inline const TYPE& SortedVector::mirrorItemAt(ssize_t index) const { - assert( (index>0 ? index : -index)0 ? index : -index; + LOG_FATAL_IF(index>=size(), + "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__, + int(index), int(size())); + return *(array() + i); } template inline diff --git a/include/utils/Vector.h b/include/utils/Vector.h index a89393ff2..506acae14 100644 --- a/include/utils/Vector.h +++ b/include/utils/Vector.h @@ -21,7 +21,8 @@ #include #include -#include +#include + #include #include @@ -271,8 +272,9 @@ TYPE* Vector::editArray() { template inline const TYPE& Vector::operator[](size_t index) const { - LOG_FATAL_IF( index>=size(), - "itemAt: index %d is past size %d", (int)index, (int)size() ); + LOG_FATAL_IF(index>=size(), + "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__, + int(index), int(size())); return *(array() + index); } @@ -283,10 +285,11 @@ const TYPE& Vector::itemAt(size_t index) const { template inline const TYPE& Vector::mirrorItemAt(ssize_t index) const { - LOG_FATAL_IF( (index>0 ? index : -index)>=size(), - "mirrorItemAt: index %d is past size %d", - (int)index, (int)size() ); - return *(array() + ((index<0) ? (size()-index) : index)); + const size_t i = index>0 ? index : -index; + LOG_FATAL_IF(index>=size(), + "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__, + int(index), int(size())); + return *(array() + i); } template inline diff --git a/include/utils/VectorImpl.h b/include/utils/VectorImpl.h index c4ec2ff97..b1224c65d 100644 --- a/include/utils/VectorImpl.h +++ b/include/utils/VectorImpl.h @@ -104,16 +104,6 @@ protected: virtual void do_splat(void* dest, const void* item, size_t num) const = 0; virtual void do_move_forward(void* dest, const void* from, size_t num) const = 0; virtual void do_move_backward(void* dest, const void* from, size_t num) const = 0; - - // take care of FBC... - virtual void reservedVectorImpl1(); - virtual void reservedVectorImpl2(); - virtual void reservedVectorImpl3(); - virtual void reservedVectorImpl4(); - virtual void reservedVectorImpl5(); - virtual void reservedVectorImpl6(); - virtual void reservedVectorImpl7(); - virtual void reservedVectorImpl8(); private: void* _grow(size_t where, size_t amount); @@ -165,16 +155,6 @@ public: protected: virtual int do_compare(const void* lhs, const void* rhs) const = 0; - // take care of FBC... - virtual void reservedSortedVectorImpl1(); - virtual void reservedSortedVectorImpl2(); - virtual void reservedSortedVectorImpl3(); - virtual void reservedSortedVectorImpl4(); - virtual void reservedSortedVectorImpl5(); - virtual void reservedSortedVectorImpl6(); - virtual void reservedSortedVectorImpl7(); - virtual void reservedSortedVectorImpl8(); - private: ssize_t _indexOrderOf(const void* item, size_t* order = 0) const; diff --git a/libs/utils/VectorImpl.cpp b/libs/utils/VectorImpl.cpp index e78faa820..020ec15c9 100644 --- a/libs/utils/VectorImpl.cpp +++ b/libs/utils/VectorImpl.cpp @@ -20,7 +20,8 @@ #include #include -#include +#include + #include #include #include @@ -56,9 +57,8 @@ VectorImpl::VectorImpl(const VectorImpl& rhs) VectorImpl::~VectorImpl() { - ALOG_ASSERT(!mCount, - "[%p] " - "subclasses of VectorImpl must call finish_vector()" + ALOGW_IF(mCount, + "[%p] subclasses of VectorImpl must call finish_vector()" " in their destructor. Leaking %d bytes.", this, (int)(mCount*mItemSize)); // We can't call _do_destroy() here because the vtable is already gone. @@ -66,7 +66,7 @@ VectorImpl::~VectorImpl() VectorImpl& VectorImpl::operator = (const VectorImpl& rhs) { - ALOG_ASSERT(mItemSize == rhs.mItemSize, + LOG_ALWAYS_FATAL_IF(mItemSize != rhs.mItemSize, "Vector<> have different types (this=%p, rhs=%p)", this, &rhs); if (this != &rhs) { release_storage(); @@ -251,6 +251,10 @@ ssize_t VectorImpl::replaceAt(const void* prototype, size_t index) ALOG_ASSERT(index= size()) { + return BAD_INDEX; + } + void* item = editItemLocation(index); if (item != prototype) { if (item == 0) @@ -294,10 +298,13 @@ void* VectorImpl::editItemLocation(size_t index) ALOG_ASSERT(index(buffer) + index*mItemSize; + + if (index < capacity()) { + void* buffer = editArrayImpl(); + if (buffer) { + return reinterpret_cast(buffer) + index*mItemSize; + } + } return 0; } @@ -307,9 +314,12 @@ const void* VectorImpl::itemLocation(size_t index) const "[%p] itemLocation: index=%d, capacity=%d, count=%d", this, (int)index, (int)capacity(), (int)mCount); - const void* buffer = arrayImpl(); - if (buffer) - return reinterpret_cast(buffer) + index*mItemSize; + if (index < capacity()) { + const void* buffer = arrayImpl(); + if (buffer) { + return reinterpret_cast(buffer) + index*mItemSize; + } + } return 0; } @@ -484,15 +494,6 @@ void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) con do_move_backward(dest, from, num); } -void VectorImpl::reservedVectorImpl1() { } -void VectorImpl::reservedVectorImpl2() { } -void VectorImpl::reservedVectorImpl3() { } -void VectorImpl::reservedVectorImpl4() { } -void VectorImpl::reservedVectorImpl5() { } -void VectorImpl::reservedVectorImpl6() { } -void VectorImpl::reservedVectorImpl7() { } -void VectorImpl::reservedVectorImpl8() { } - /*****************************************************************************/ SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags) @@ -608,16 +609,6 @@ ssize_t SortedVectorImpl::remove(const void* item) return i; } -void SortedVectorImpl::reservedSortedVectorImpl1() { }; -void SortedVectorImpl::reservedSortedVectorImpl2() { }; -void SortedVectorImpl::reservedSortedVectorImpl3() { }; -void SortedVectorImpl::reservedSortedVectorImpl4() { }; -void SortedVectorImpl::reservedSortedVectorImpl5() { }; -void SortedVectorImpl::reservedSortedVectorImpl6() { }; -void SortedVectorImpl::reservedSortedVectorImpl7() { }; -void SortedVectorImpl::reservedSortedVectorImpl8() { }; - - /*****************************************************************************/ }; // namespace android