1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_MIRROR_OBJECT_ARRAY_H_
18 #define ART_RUNTIME_MIRROR_OBJECT_ARRAY_H_
19 
20 #include <iterator>
21 #include "array.h"
22 #include "base/iteration_range.h"
23 #include "obj_ptr.h"
24 
25 namespace art {
26 namespace mirror {
27 
28 template<typename T, typename Container> class ArrayIter;
29 template <typename T> using ConstObjPtrArrayIter = ArrayIter<T, const ObjPtr<ObjectArray<T>>>;
30 template <typename T> using ConstHandleArrayIter = ArrayIter<T, const Handle<ObjectArray<T>>>;
31 template <typename T> using ObjPtrArrayIter = ArrayIter<T, ObjPtr<ObjectArray<T>>>;
32 template <typename T> using HandleArrayIter = ArrayIter<T, Handle<ObjectArray<T>>>;
33 
34 template<class T>
35 class MANAGED ObjectArray: public Array {
36  public:
37   MIRROR_CLASS("[Ljava/lang/Object;");
38 
39   // The size of Object[].class.
ClassSize(PointerSize pointer_size)40   static uint32_t ClassSize(PointerSize pointer_size) {
41     return Array::ClassSize(pointer_size);
42   }
43 
44   static ObjPtr<ObjectArray<T>> Alloc(Thread* self,
45                                       ObjPtr<Class> object_array_class,
46                                       int32_t length,
47                                       gc::AllocatorType allocator_type)
48       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
49 
50   static ObjPtr<ObjectArray<T>> Alloc(Thread* self,
51                                       ObjPtr<Class> object_array_class,
52                                       int32_t length)
53       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
54 
55   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
56            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
57   ALWAYS_INLINE ObjPtr<T> Get(int32_t i) REQUIRES_SHARED(Locks::mutator_lock_);
58 
59   // Returns true if the object can be stored into the array. If not, throws
60   // an ArrayStoreException and returns false.
61   // TODO fix thread safety analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
62   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
63   bool CheckAssignable(ObjPtr<T> object) NO_THREAD_SAFETY_ANALYSIS;
64 
65   ALWAYS_INLINE void Set(int32_t i, ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_);
66   // TODO fix thread safety analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
67   template<bool kTransactionActive, bool kCheckTransaction = true,
68       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
69   ALWAYS_INLINE void Set(int32_t i, ObjPtr<T> object) NO_THREAD_SAFETY_ANALYSIS;
70 
71   // Set element without bound and element type checks, to be used in limited
72   // circumstances, such as during boot image writing.
73   // TODO fix thread safety analysis broken by the use of template. This should be
74   // REQUIRES_SHARED(Locks::mutator_lock_).
75   template<bool kTransactionActive, bool kCheckTransaction = true,
76       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
77   ALWAYS_INLINE void SetWithoutChecks(int32_t i, ObjPtr<T> object) NO_THREAD_SAFETY_ANALYSIS;
78   // TODO fix thread safety analysis broken by the use of template. This should be
79   // REQUIRES_SHARED(Locks::mutator_lock_).
80   template<bool kTransactionActive, bool kCheckTransaction = true,
81       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
82   ALWAYS_INLINE void SetWithoutChecksAndWriteBarrier(int32_t i, ObjPtr<T> object)
83       NO_THREAD_SAFETY_ANALYSIS;
84 
85   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
86            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
87   ALWAYS_INLINE ObjPtr<T> GetWithoutChecks(int32_t i) REQUIRES_SHARED(Locks::mutator_lock_);
88 
89   // Copy src into this array (dealing with overlaps as memmove does) without assignability checks.
90   void AssignableMemmove(int32_t dst_pos,
91                          ObjPtr<ObjectArray<T>> src,
92                          int32_t src_pos,
93                          int32_t count)
94       REQUIRES_SHARED(Locks::mutator_lock_);
95 
96   // Copy src into this array assuming no overlap and without assignability checks.
97   void AssignableMemcpy(int32_t dst_pos,
98                         ObjPtr<ObjectArray<T>> src,
99                         int32_t src_pos,
100                         int32_t count)
101       REQUIRES_SHARED(Locks::mutator_lock_);
102 
103   // Copy src into this array with assignability checks.
104   template<bool kTransactionActive>
105   void AssignableCheckingMemcpy(int32_t dst_pos,
106                                 ObjPtr<ObjectArray<T>> src,
107                                 int32_t src_pos,
108                                 int32_t count,
109                                 bool throw_exception)
110       REQUIRES_SHARED(Locks::mutator_lock_);
111 
112   static ObjPtr<ObjectArray<T>> CopyOf(Handle<ObjectArray<T>> h_this,
113                                        Thread* self,
114                                        int32_t new_length)
115       REQUIRES_SHARED(Locks::mutator_lock_)
116       REQUIRES(!Roles::uninterruptible_);
117 
118   static MemberOffset OffsetOfElement(int32_t i);
119 
120   inline ConstObjPtrArrayIter<T> cbegin() const REQUIRES_SHARED(Locks::mutator_lock_);
121   inline ConstObjPtrArrayIter<T> cend() const REQUIRES_SHARED(Locks::mutator_lock_);
ConstIterate()122   inline IterationRange<ConstObjPtrArrayIter<T>> ConstIterate() const REQUIRES_SHARED(Locks::mutator_lock_) {
123     return IterationRange(cbegin(), cend());
124   }
125   inline ObjPtrArrayIter<T> begin() REQUIRES_SHARED(Locks::mutator_lock_);
126   inline ObjPtrArrayIter<T> end() REQUIRES_SHARED(Locks::mutator_lock_);
Iterate()127   inline IterationRange<ObjPtrArrayIter<T>> Iterate() REQUIRES_SHARED(Locks::mutator_lock_) {
128     return IterationRange(begin(), end());
129   }
130 
131   static inline ConstHandleArrayIter<T> cbegin(const Handle<ObjectArray<T>>& h_this)
132       REQUIRES_SHARED(Locks::mutator_lock_);
133   static inline ConstHandleArrayIter<T> cend(const Handle<ObjectArray<T>>& h_this)
134       REQUIRES_SHARED(Locks::mutator_lock_);
ConstIterate(const Handle<ObjectArray<T>> & h_this)135   static inline IterationRange<ConstHandleArrayIter<T>> ConstIterate(
136       const Handle<ObjectArray<T>>& h_this) REQUIRES_SHARED(Locks::mutator_lock_) {
137     return IterationRange(cbegin(h_this), cend(h_this));
138   }
139   static inline HandleArrayIter<T> begin(Handle<ObjectArray<T>>& h_this)
140       REQUIRES_SHARED(Locks::mutator_lock_);
141   static inline HandleArrayIter<T> end(Handle<ObjectArray<T>>& h_this)
142       REQUIRES_SHARED(Locks::mutator_lock_);
Iterate(Handle<ObjectArray<T>> & h_this)143   static inline IterationRange<HandleArrayIter<T>> Iterate(Handle<ObjectArray<T>>& h_this)
144       REQUIRES_SHARED(Locks::mutator_lock_) {
145     return IterationRange(begin(h_this), end(h_this));
146   }
147 
148  private:
149   // TODO fix thread safety analysis broken by the use of template. This should be
150   // REQUIRES_SHARED(Locks::mutator_lock_).
151   template<typename Visitor>
152   void VisitReferences(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS;
153 
154   friend class Object;  // For VisitReferences
155   DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
156 };
157 
158 // Everything is NO_THREAD_SAFETY_ANALYSIS to work-around STL incompat with thread-annotations.
159 // Everything should have REQUIRES_SHARED(Locks::mutator_lock_).
160 template <typename T, typename Container>
161 class ArrayIter : public std::iterator<std::forward_iterator_tag, ObjPtr<T>> {
162  private:
163   using Iter = ArrayIter<T, Container>;
164 
165  public:
ArrayIter(Container array,int32_t idx)166   ArrayIter(Container array, int32_t idx) NO_THREAD_SAFETY_ANALYSIS : array_(array), idx_(idx) {
167     CheckIdx();
168   }
169 
170   ArrayIter(const Iter& other) = default;  // NOLINT(runtime/explicit)
171   Iter& operator=(const Iter& other) = default;
172 
173   bool operator!=(const Iter& other) const NO_THREAD_SAFETY_ANALYSIS {
174     CheckIdx();
175     return !(*this == other);
176   }
177   bool operator==(const Iter& other) const NO_THREAD_SAFETY_ANALYSIS {
178     return Ptr(other.array_) == Ptr(array_) && other.idx_ == idx_;
179   }
180   Iter& operator++() NO_THREAD_SAFETY_ANALYSIS {
181     idx_++;
182     CheckIdx();
183     return *this;
184   }
185   Iter operator++(int) NO_THREAD_SAFETY_ANALYSIS {
186     Iter res(this);
187     idx_++;
188     CheckIdx();
189     return res;
190   }
191   ObjPtr<T> operator->() const NO_THREAD_SAFETY_ANALYSIS {
192     CheckIdx();
193     return array_->GetWithoutChecks(idx_);
194   }
195   ObjPtr<T> operator*() const NO_THREAD_SAFETY_ANALYSIS {
196     CheckIdx();
197     return array_->GetWithoutChecks(idx_);
198   }
199 
200  private:
201   // Checks current index and that locks are properly held.
202   void CheckIdx() const REQUIRES_SHARED(Locks::mutator_lock_);
203 
Ptr(const Handle<ObjectArray<T>> & p)204   static ObjectArray<T>* Ptr(const Handle<ObjectArray<T>>& p)
205       REQUIRES_SHARED(Locks::mutator_lock_) {
206     return p.Get();
207   }
Ptr(const ObjPtr<ObjectArray<T>> & p)208   static ObjectArray<T>* Ptr(const ObjPtr<ObjectArray<T>>& p)
209       REQUIRES_SHARED(Locks::mutator_lock_) {
210     return p.Ptr();
211   }
212 
213   Container array_;
214   int32_t idx_;
215 };
216 
217 }  // namespace mirror
218 }  // namespace art
219 
220 #endif  // ART_RUNTIME_MIRROR_OBJECT_ARRAY_H_
221