Add DCHECKs to ArenaVector and ScopedArenaVector.

Implement dchecked_vector<> template that DCHECK()s element
access and insert()/emplace()/erase() positions. Change the
ArenaVector<> and ScopedArenaVector<> aliases to use the new
template instead of std::vector<>. Remove DCHECK()s that
have now become unnecessary from the Optimizing compiler.

Change-Id: Ib8506bd30d223f68f52bd4476c76d9991acacadc
diff --git a/runtime/base/arena_containers.h b/runtime/base/arena_containers.h
index e7ea09d..66b5289 100644
--- a/runtime/base/arena_containers.h
+++ b/runtime/base/arena_containers.h
@@ -20,9 +20,9 @@
 #include <deque>
 #include <queue>
 #include <set>
-#include <vector>
 
 #include "arena_allocator.h"
+#include "base/dchecked_vector.h"
 #include "safe_map.h"
 
 namespace art {
@@ -48,7 +48,7 @@
 using ArenaQueue = std::queue<T, ArenaDeque<T>>;
 
 template <typename T>
-using ArenaVector = std::vector<T, ArenaAllocatorAdapter<T>>;
+using ArenaVector = dchecked_vector<T, ArenaAllocatorAdapter<T>>;
 
 template <typename T, typename Comparator = std::less<T>>
 using ArenaSet = std::set<T, Comparator, ArenaAllocatorAdapter<T>>;
diff --git a/runtime/base/dchecked_vector.h b/runtime/base/dchecked_vector.h
new file mode 100644
index 0000000..6ec573a
--- /dev/null
+++ b/runtime/base/dchecked_vector.h
@@ -0,0 +1,228 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_RUNTIME_BASE_DCHECKED_VECTOR_H_
+#define ART_RUNTIME_BASE_DCHECKED_VECTOR_H_
+
+#include <algorithm>
+#include <type_traits>
+#include <vector>
+
+#include "base/logging.h"
+
+namespace art {
+
+// Template class serving as a replacement for std::vector<> but adding
+// DCHECK()s for the subscript operator, front(), back(), pop_back(),
+// and for insert()/emplace()/erase() positions.
+//
+// Note: The element accessor at() is specified as throwing std::out_of_range
+// but we do not use exceptions, so this accessor is deliberately hidden.
+// Note: The common pattern &v[0] used to retrieve pointer to the data is not
+// valid for an empty dchecked_vector<>. Use data() to avoid checking empty().
+template <typename T, typename Alloc>
+class dchecked_vector : private std::vector<T, Alloc> {
+ private:
+  // std::vector<> has a slightly different specialization for bool. We don't provide that.
+  static_assert(!std::is_same<T, bool>::value, "Not implemented for bool.");
+  using Base = std::vector<T, Alloc>;
+
+ public:
+  using typename Base::value_type;
+  using typename Base::allocator_type;
+  using typename Base::reference;
+  using typename Base::const_reference;
+  using typename Base::pointer;
+  using typename Base::const_pointer;
+  using typename Base::iterator;
+  using typename Base::const_iterator;
+  using typename Base::reverse_iterator;
+  using typename Base::const_reverse_iterator;
+  using typename Base::size_type;
+  using typename Base::difference_type;
+
+  // Construct/copy/destroy.
+  dchecked_vector()
+      : Base() { }
+  explicit dchecked_vector(const allocator_type& alloc)
+      : Base(alloc) { }
+  explicit dchecked_vector(size_type n, const allocator_type& alloc = allocator_type())
+      : Base(n, alloc) { }
+  dchecked_vector(size_type n,
+                  const value_type& value,
+                  const allocator_type& alloc = allocator_type())
+      : Base(n, value, alloc) { }
+  template <typename InputIterator>
+  dchecked_vector(InputIterator first,
+                  InputIterator last,
+                  const allocator_type& alloc = allocator_type())
+      : Base(first, last, alloc) { }
+  dchecked_vector(const dchecked_vector& src)
+      : Base(src) { }
+  dchecked_vector(const dchecked_vector& src, const allocator_type& alloc)
+      : Base(src, alloc) { }
+  dchecked_vector(dchecked_vector&& src)
+      : Base(std::move(src)) { }
+  dchecked_vector(dchecked_vector&& src, const allocator_type& alloc)
+      : Base(std::move(src), alloc) { }
+  dchecked_vector(std::initializer_list<value_type> il,
+                  const allocator_type& alloc = allocator_type())
+      : Base(il, alloc) { }
+  ~dchecked_vector() = default;
+  dchecked_vector& operator=(const dchecked_vector& src) {
+    Base::operator=(src);
+    return *this;
+  }
+  dchecked_vector& operator=(dchecked_vector&& src) {
+    Base::operator=(std::move(src));
+    return *this;
+  }
+  dchecked_vector& operator=(std::initializer_list<value_type> il) {
+    Base::operator=(il);
+    return *this;
+  }
+
+  // Iterators.
+  using Base::begin;
+  using Base::end;
+  using Base::rbegin;
+  using Base::rend;
+  using Base::cbegin;
+  using Base::cend;
+  using Base::crbegin;
+  using Base::crend;
+
+  // Capacity.
+  using Base::size;
+  using Base::max_size;
+  using Base::resize;
+  using Base::capacity;
+  using Base::empty;
+  using Base::reserve;
+  using Base::shrink_to_fit;
+
+  // Element access: inherited.
+  // Note: Deliberately not providing at().
+  using Base::data;
+
+  // Element access: subscript operator. Check index.
+  reference operator[](size_type n) {
+    DCHECK_LT(n, size());
+    return Base::operator[](n);
+  }
+  const_reference operator[](size_type n) const {
+    DCHECK_LT(n, size());
+    return Base::operator[](n);
+  }
+
+  // Element access: front(), back(). Check not empty.
+  reference front() { DCHECK(!empty()); return Base::front(); }
+  const_reference front() const { DCHECK(!empty()); return Base::front(); }
+  reference back() { DCHECK(!empty()); return Base::back(); }
+  const_reference back() const { DCHECK(!empty()); return Base::back(); }
+
+  // Modifiers: inherited.
+  using Base::assign;
+  using Base::push_back;
+  using Base::clear;
+  using Base::emplace_back;
+
+  // Modifiers: pop_back(). Check not empty.
+  void pop_back() { DCHECK(!empty()); Base::pop_back(); }
+
+  // Modifiers: swap(). Swap only with another dchecked_vector instead of a plain vector.
+  void swap(dchecked_vector& other) { Base::swap(other); }
+
+  // Modifiers: insert(). Check position.
+  iterator insert(const_iterator position, const value_type& value) {
+    DCHECK(cbegin() <= position && position <= cend());
+    return Base::insert(position, value);
+  }
+  iterator insert(const_iterator position, size_type n, const value_type& value) {
+    DCHECK(cbegin() <= position && position <= cend());
+    return Base::insert(position, n, value);
+  }
+  template <typename InputIterator>
+  iterator insert(const_iterator position, InputIterator first, InputIterator last) {
+    DCHECK(cbegin() <= position && position <= cend());
+    return Base::insert(position, first, last);
+  }
+  iterator insert(const_iterator position, value_type&& value) {
+    DCHECK(cbegin() <= position && position <= cend());
+    return Base::insert(position, std::move(value));
+  }
+  iterator insert(const_iterator position, std::initializer_list<value_type> il) {
+    DCHECK(cbegin() <= position && position <= cend());
+    return Base::insert(position, il);
+  }
+
+  // Modifiers: erase(). Check position.
+  iterator erase(const_iterator position) {
+    DCHECK(cbegin() <= position && position < cend());
+    return Base::erase(position);
+  }
+  iterator erase(const_iterator first, const_iterator last) {
+    DCHECK(cbegin() <= first && first <= cend());
+    DCHECK(first <= last && last <= cend());
+    return Base::erase(first, last);
+  }
+
+  // Modifiers: emplace(). Check position.
+  template <typename... Args>
+  iterator emplace(const_iterator position, Args&&... args) {
+    DCHECK(cbegin() <= position && position <= cend());
+    Base::emplace(position, std::forward(args...));
+  }
+
+  // Allocator.
+  using Base::get_allocator;
+};
+
+// Non-member swap(), found by argument-dependent lookup for an unqualified call.
+template <typename T, typename Alloc>
+void swap(dchecked_vector<T, Alloc>& lhs, dchecked_vector<T, Alloc>& rhs) {
+  lhs.swap(rhs);
+}
+
+// Non-member relational operators.
+template <typename T, typename Alloc>
+bool operator==(const dchecked_vector<T, Alloc>& lhs, const dchecked_vector<T, Alloc>& rhs) {
+  return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
+}
+template <typename T, typename Alloc>
+bool operator!=(const dchecked_vector<T, Alloc>& lhs, const dchecked_vector<T, Alloc>& rhs) {
+  return !(lhs == rhs);
+}
+template <typename T, typename Alloc>
+bool operator<(const dchecked_vector<T, Alloc>& lhs, const dchecked_vector<T, Alloc>& rhs) {
+  return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
+}
+template <typename T, typename Alloc>
+bool operator<=(const dchecked_vector<T, Alloc>& lhs, const dchecked_vector<T, Alloc>& rhs) {
+  return !(rhs < lhs);
+}
+template <typename T, typename Alloc>
+bool operator>(const dchecked_vector<T, Alloc>& lhs, const dchecked_vector<T, Alloc>& rhs) {
+  return rhs < lhs;
+}
+template <typename T, typename Alloc>
+bool operator>=(const dchecked_vector<T, Alloc>& lhs, const dchecked_vector<T, Alloc>& rhs) {
+  return !(lhs < rhs);
+}
+
+}  // namespace art
+
+#endif  // ART_RUNTIME_BASE_DCHECKED_VECTOR_H_
diff --git a/runtime/base/scoped_arena_containers.h b/runtime/base/scoped_arena_containers.h
index eecc55f..380ed11 100644
--- a/runtime/base/scoped_arena_containers.h
+++ b/runtime/base/scoped_arena_containers.h
@@ -21,9 +21,9 @@
 #include <queue>
 #include <set>
 #include <unordered_map>
-#include <vector>
 
 #include "arena_containers.h"  // For ArenaAllocatorAdapterKind.
+#include "base/dchecked_vector.h"
 #include "scoped_arena_allocator.h"
 #include "safe_map.h"
 
@@ -47,7 +47,7 @@
 using ScopedArenaQueue = std::queue<T, ScopedArenaDeque<T>>;
 
 template <typename T>
-using ScopedArenaVector = std::vector<T, ScopedArenaAllocatorAdapter<T>>;
+using ScopedArenaVector = dchecked_vector<T, ScopedArenaAllocatorAdapter<T>>;
 
 template <typename T, typename Comparator = std::less<T>>
 using ScopedArenaSet = std::set<T, Comparator, ScopedArenaAllocatorAdapter<T>>;
diff --git a/runtime/leb128.h b/runtime/leb128.h
index baf9da2..74934ae 100644
--- a/runtime/leb128.h
+++ b/runtime/leb128.h
@@ -127,8 +127,9 @@
   return dest;
 }
 
-template<typename Allocator>
-static inline void EncodeUnsignedLeb128(std::vector<uint8_t, Allocator>* dest, uint32_t value) {
+template <typename Vector>
+static inline void EncodeUnsignedLeb128(Vector* dest, uint32_t value) {
+  static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
   uint8_t out = value & 0x7f;
   value >>= 7;
   while (value != 0) {
@@ -165,8 +166,9 @@
   return dest;
 }
 
-template<typename Allocator>
-static inline void EncodeSignedLeb128(std::vector<uint8_t, Allocator>* dest, int32_t value) {
+template<typename Vector>
+static inline void EncodeSignedLeb128(Vector* dest, int32_t value) {
+  static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
   uint32_t extra_bits = static_cast<uint32_t>(value ^ (value >> 31)) >> 6;
   uint8_t out = value & 0x7f;
   while (extra_bits != 0u) {
@@ -179,10 +181,12 @@
 }
 
 // An encoder that pushes int32_t/uint32_t data onto the given std::vector.
-template <typename Allocator = std::allocator<uint8_t>>
+template <typename Vector = std::vector<uint8_t>>
 class Leb128Encoder {
+  static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
+
  public:
-  explicit Leb128Encoder(std::vector<uint8_t, Allocator>* data) : data_(data) {
+  explicit Leb128Encoder(Vector* data) : data_(data) {
     DCHECK(data != nullptr);
   }
 
@@ -212,27 +216,29 @@
     }
   }
 
-  const std::vector<uint8_t, Allocator>& GetData() const {
+  const Vector& GetData() const {
     return *data_;
   }
 
  protected:
-  std::vector<uint8_t, Allocator>* const data_;
+  Vector* const data_;
 
  private:
   DISALLOW_COPY_AND_ASSIGN(Leb128Encoder);
 };
 
 // An encoder with an API similar to vector<uint32_t> where the data is captured in ULEB128 format.
-template <typename Allocator = std::allocator<uint8_t>>
-class Leb128EncodingVector FINAL : private std::vector<uint8_t, Allocator>,
-                                   public Leb128Encoder<Allocator> {
- public:
-  Leb128EncodingVector() : Leb128Encoder<Allocator>(this) { }
+template <typename Vector = std::vector<uint8_t>>
+class Leb128EncodingVector FINAL : private Vector,
+                                   public Leb128Encoder<Vector> {
+  static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
 
-  explicit Leb128EncodingVector(const Allocator& alloc)
-    : std::vector<uint8_t, Allocator>(alloc),
-      Leb128Encoder<Allocator>(this) { }
+ public:
+  Leb128EncodingVector() : Leb128Encoder<Vector>(this) { }
+
+  explicit Leb128EncodingVector(const typename Vector::allocator_type& alloc)
+    : Vector(alloc),
+      Leb128Encoder<Vector>(this) { }
 
  private:
   DISALLOW_COPY_AND_ASSIGN(Leb128EncodingVector);
diff --git a/runtime/utils.h b/runtime/utils.h
index 3e61824..19cc462 100644
--- a/runtime/utils.h
+++ b/runtime/utils.h
@@ -286,8 +286,9 @@
   }
 };
 
-template <typename Alloc>
-void Push32(std::vector<uint8_t, Alloc>* buf, int32_t data) {
+template <typename Vector>
+void Push32(Vector* buf, int32_t data) {
+  static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
   buf->push_back(data & 0xff);
   buf->push_back((data >> 8) & 0xff);
   buf->push_back((data >> 16) & 0xff);