improve Vector<> safety checks
- make errors that will always cause a memory corruption always fatal
(for eg: KeyedVector<>::editValue{For|At}() failure)
- make other errors fatal in debug mode, those that can be caught by
the caller.
- fix typos
Change-Id: I65cc7d81035c37ce2906fc4500c50e5d5b5c49eb
diff --git a/include/utils/KeyedVector.h b/include/utils/KeyedVector.h
index 47c2c56..c4faae0 100644
--- a/include/utils/KeyedVector.h
+++ b/include/utils/KeyedVector.h
@@ -21,6 +21,8 @@
#include <stdint.h>
#include <sys/types.h>
+#include <cutils/log.h>
+
#include <utils/SortedVector.h>
#include <utils/TypeHelpers.h>
#include <utils/Errors.h>
@@ -50,11 +52,11 @@
//! 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 @@
template<typename KEY, typename VALUE> inline
const VALUE& KeyedVector<KEY,VALUE>::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 @@
template<typename KEY, typename VALUE> inline
VALUE& KeyedVector<KEY,VALUE>::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;
}