/* * Copyright (C) 2011 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_MIRROR_OBJECT_ARRAY_H_ #define ART_RUNTIME_MIRROR_OBJECT_ARRAY_H_ #include #include "array.h" #include "base/iteration_range.h" #include "obj_ptr.h" namespace art { namespace mirror { template class ArrayIter; template using ConstObjPtrArrayIter = ArrayIter>>; template using ConstHandleArrayIter = ArrayIter>>; template using ObjPtrArrayIter = ArrayIter>>; template using HandleArrayIter = ArrayIter>>; template class MANAGED ObjectArray: public Array { public: MIRROR_CLASS("[Ljava/lang/Object;"); // The size of Object[].class. static uint32_t ClassSize(PointerSize pointer_size) { return Array::ClassSize(pointer_size); } static ObjPtr> Alloc(Thread* self, ObjPtr object_array_class, int32_t length, gc::AllocatorType allocator_type) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); static ObjPtr> Alloc(Thread* self, ObjPtr object_array_class, int32_t length) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); template ALWAYS_INLINE ObjPtr Get(int32_t i) REQUIRES_SHARED(Locks::mutator_lock_); // Returns true if the object can be stored into the array. If not, throws // an ArrayStoreException and returns false. // TODO fix thread safety analysis: should be REQUIRES_SHARED(Locks::mutator_lock_). template bool CheckAssignable(ObjPtr object) NO_THREAD_SAFETY_ANALYSIS; ALWAYS_INLINE void Set(int32_t i, ObjPtr object) REQUIRES_SHARED(Locks::mutator_lock_); // TODO fix thread safety analysis: should be REQUIRES_SHARED(Locks::mutator_lock_). template ALWAYS_INLINE void Set(int32_t i, ObjPtr object) NO_THREAD_SAFETY_ANALYSIS; // Set element without bound and element type checks, to be used in limited // circumstances, such as during boot image writing. // TODO fix thread safety analysis broken by the use of template. This should be // REQUIRES_SHARED(Locks::mutator_lock_). template ALWAYS_INLINE void SetWithoutChecks(int32_t i, ObjPtr object) NO_THREAD_SAFETY_ANALYSIS; // TODO fix thread safety analysis broken by the use of template. This should be // REQUIRES_SHARED(Locks::mutator_lock_). template ALWAYS_INLINE void SetWithoutChecksAndWriteBarrier(int32_t i, ObjPtr object) NO_THREAD_SAFETY_ANALYSIS; template ALWAYS_INLINE ObjPtr GetWithoutChecks(int32_t i) REQUIRES_SHARED(Locks::mutator_lock_); // Copy src into this array (dealing with overlaps as memmove does) without assignability checks. void AssignableMemmove(int32_t dst_pos, ObjPtr> src, int32_t src_pos, int32_t count) REQUIRES_SHARED(Locks::mutator_lock_); // Copy src into this array assuming no overlap and without assignability checks. void AssignableMemcpy(int32_t dst_pos, ObjPtr> src, int32_t src_pos, int32_t count) REQUIRES_SHARED(Locks::mutator_lock_); // Copy src into this array with assignability checks. template void AssignableCheckingMemcpy(int32_t dst_pos, ObjPtr> src, int32_t src_pos, int32_t count, bool throw_exception) REQUIRES_SHARED(Locks::mutator_lock_); static ObjPtr> CopyOf(Handle> h_this, Thread* self, int32_t new_length) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); static MemberOffset OffsetOfElement(int32_t i); inline ConstObjPtrArrayIter cbegin() const REQUIRES_SHARED(Locks::mutator_lock_); inline ConstObjPtrArrayIter cend() const REQUIRES_SHARED(Locks::mutator_lock_); inline IterationRange> ConstIterate() const REQUIRES_SHARED(Locks::mutator_lock_) { return IterationRange(cbegin(), cend()); } inline ObjPtrArrayIter begin() REQUIRES_SHARED(Locks::mutator_lock_); inline ObjPtrArrayIter end() REQUIRES_SHARED(Locks::mutator_lock_); inline IterationRange> Iterate() REQUIRES_SHARED(Locks::mutator_lock_) { return IterationRange(begin(), end()); } static inline ConstHandleArrayIter cbegin(const Handle>& h_this) REQUIRES_SHARED(Locks::mutator_lock_); static inline ConstHandleArrayIter cend(const Handle>& h_this) REQUIRES_SHARED(Locks::mutator_lock_); static inline IterationRange> ConstIterate( const Handle>& h_this) REQUIRES_SHARED(Locks::mutator_lock_) { return IterationRange(cbegin(h_this), cend(h_this)); } static inline HandleArrayIter begin(Handle>& h_this) REQUIRES_SHARED(Locks::mutator_lock_); static inline HandleArrayIter end(Handle>& h_this) REQUIRES_SHARED(Locks::mutator_lock_); static inline IterationRange> Iterate(Handle>& h_this) REQUIRES_SHARED(Locks::mutator_lock_) { return IterationRange(begin(h_this), end(h_this)); } private: // TODO fix thread safety analysis broken by the use of template. This should be // REQUIRES_SHARED(Locks::mutator_lock_). template void VisitReferences(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS; template void VisitReferences(const Visitor& visitor, MemberOffset begin, MemberOffset end) NO_THREAD_SAFETY_ANALYSIS; friend class Object; // For VisitReferences DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray); }; // Everything is NO_THREAD_SAFETY_ANALYSIS to work-around STL incompat with thread-annotations. // Everything should have REQUIRES_SHARED(Locks::mutator_lock_). template class ArrayIter : public std::iterator> { private: using Iter = ArrayIter; public: ArrayIter(Container array, int32_t idx) NO_THREAD_SAFETY_ANALYSIS : array_(array), idx_(idx) { CheckIdx(); } ArrayIter(const Iter& other) = default; // NOLINT(runtime/explicit) Iter& operator=(const Iter& other) = default; bool operator!=(const Iter& other) const NO_THREAD_SAFETY_ANALYSIS { CheckIdx(); return !(*this == other); } bool operator==(const Iter& other) const NO_THREAD_SAFETY_ANALYSIS { return Ptr(other.array_) == Ptr(array_) && other.idx_ == idx_; } Iter& operator++() NO_THREAD_SAFETY_ANALYSIS { idx_++; CheckIdx(); return *this; } Iter operator++(int) NO_THREAD_SAFETY_ANALYSIS { Iter res(this); idx_++; CheckIdx(); return res; } ObjPtr operator->() const NO_THREAD_SAFETY_ANALYSIS { CheckIdx(); return array_->GetWithoutChecks(idx_); } ObjPtr operator*() const NO_THREAD_SAFETY_ANALYSIS { CheckIdx(); return array_->GetWithoutChecks(idx_); } private: // Checks current index and that locks are properly held. void CheckIdx() const REQUIRES_SHARED(Locks::mutator_lock_); static ObjectArray* Ptr(const Handle>& p) REQUIRES_SHARED(Locks::mutator_lock_) { return p.Get(); } static ObjectArray* Ptr(const ObjPtr>& p) REQUIRES_SHARED(Locks::mutator_lock_) { return p.Ptr(); } Container array_; int32_t idx_; }; } // namespace mirror } // namespace art #endif // ART_RUNTIME_MIRROR_OBJECT_ARRAY_H_