diff options
Diffstat (limited to 'libartbase/base/array_slice.h')
-rw-r--r-- | libartbase/base/array_slice.h | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/libartbase/base/array_slice.h b/libartbase/base/array_slice.h index fb3da6b476..4679146ca1 100644 --- a/libartbase/base/array_slice.h +++ b/libartbase/base/array_slice.h @@ -20,6 +20,7 @@ #include "bit_utils.h" #include "casts.h" #include "iteration_range.h" +#include "length_prefixed_array.h" #include "stride_iterator.h" namespace art { @@ -55,6 +56,14 @@ class ArraySlice { DCHECK(array_ != nullptr || length == 0); } + ArraySlice(LengthPrefixedArray<T>* lpa, + size_t element_size = sizeof(T), + size_t alignment = alignof(T)) + : ArraySlice( + lpa != nullptr && lpa->size() != 0 ? &lpa->At(0, element_size, alignment) : nullptr, + lpa != nullptr ? lpa->size() : 0, + element_size) {} + // Iterators. iterator begin() { return iterator(&AtUnchecked(0), element_size_); } const_iterator begin() const { return const_iterator(&AtUnchecked(0), element_size_); } @@ -130,7 +139,17 @@ class ArraySlice { } bool Contains(const T* element) const { - return &AtUnchecked(0) <= element && element < &AtUnchecked(size_); + return &AtUnchecked(0) <= element && element < &AtUnchecked(size_) && + ((reinterpret_cast<uintptr_t>(element) - + reinterpret_cast<uintptr_t>(&AtUnchecked(0))) % element_size_) == 0; + } + + size_t OffsetOf(const T* element) const { + DCHECK(Contains(element)); + // Since it's possible element_size_ != sizeof(T) we cannot just use pointer arithmatic + uintptr_t base_ptr = reinterpret_cast<uintptr_t>(&AtUnchecked(0)); + uintptr_t obj_ptr = reinterpret_cast<uintptr_t>(element); + return (obj_ptr - base_ptr) / element_size_; } private: |