• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   template<typename Visitor>
154   void VisitReferences(const Visitor& visitor,
155                        MemberOffset begin,
156                        MemberOffset end) NO_THREAD_SAFETY_ANALYSIS;
157 
158   friend class Object;  // For VisitReferences
159   DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
160 };
161 
162 // Everything is NO_THREAD_SAFETY_ANALYSIS to work-around STL incompat with thread-annotations.
163 // Everything should have REQUIRES_SHARED(Locks::mutator_lock_).
164 template <typename T, typename Container>
165 class ArrayIter : public std::iterator<std::forward_iterator_tag, ObjPtr<T>> {
166  private:
167   using Iter = ArrayIter<T, Container>;
168 
169  public:
ArrayIter(Container array,int32_t idx)170   ArrayIter(Container array, int32_t idx) NO_THREAD_SAFETY_ANALYSIS : array_(array), idx_(idx) {
171     CheckIdx();
172   }
173 
174   ArrayIter(const Iter& other) = default;  // NOLINT(runtime/explicit)
175   Iter& operator=(const Iter& other) = default;
176 
177   bool operator!=(const Iter& other) const NO_THREAD_SAFETY_ANALYSIS {
178     CheckIdx();
179     return !(*this == other);
180   }
181   bool operator==(const Iter& other) const NO_THREAD_SAFETY_ANALYSIS {
182     return Ptr(other.array_) == Ptr(array_) && other.idx_ == idx_;
183   }
184   Iter& operator++() NO_THREAD_SAFETY_ANALYSIS {
185     idx_++;
186     CheckIdx();
187     return *this;
188   }
189   Iter operator++(int) NO_THREAD_SAFETY_ANALYSIS {
190     Iter res(this);
191     idx_++;
192     CheckIdx();
193     return res;
194   }
195   ObjPtr<T> operator->() const NO_THREAD_SAFETY_ANALYSIS {
196     CheckIdx();
197     return array_->GetWithoutChecks(idx_);
198   }
199   ObjPtr<T> operator*() const NO_THREAD_SAFETY_ANALYSIS {
200     CheckIdx();
201     return array_->GetWithoutChecks(idx_);
202   }
203 
204  private:
205   // Checks current index and that locks are properly held.
206   void CheckIdx() const REQUIRES_SHARED(Locks::mutator_lock_);
207 
Ptr(const Handle<ObjectArray<T>> & p)208   static ObjectArray<T>* Ptr(const Handle<ObjectArray<T>>& p)
209       REQUIRES_SHARED(Locks::mutator_lock_) {
210     return p.Get();
211   }
Ptr(const ObjPtr<ObjectArray<T>> & p)212   static ObjectArray<T>* Ptr(const ObjPtr<ObjectArray<T>>& p)
213       REQUIRES_SHARED(Locks::mutator_lock_) {
214     return p.Ptr();
215   }
216 
217   Container array_;
218   int32_t idx_;
219 };
220 
221 }  // namespace mirror
222 }  // namespace art
223 
224 #endif  // ART_RUNTIME_MIRROR_OBJECT_ARRAY_H_
225