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 "array.h"
21 #include "obj_ptr.h"
22 
23 namespace art {
24 namespace mirror {
25 
26 template<class T>
27 class MANAGED ObjectArray: public Array {
28  public:
29   // The size of Object[].class.
ClassSize(PointerSize pointer_size)30   static uint32_t ClassSize(PointerSize pointer_size) {
31     return Array::ClassSize(pointer_size);
32   }
33 
34   static ObjectArray<T>* Alloc(Thread* self,
35                                ObjPtr<Class> object_array_class,
36                                int32_t length,
37                                gc::AllocatorType allocator_type)
38       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
39 
40   static ObjectArray<T>* Alloc(Thread* self,
41                                ObjPtr<Class> object_array_class,
42                                int32_t length)
43       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
44 
45   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
46            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
47   ALWAYS_INLINE T* Get(int32_t i) REQUIRES_SHARED(Locks::mutator_lock_);
48 
49   // Returns true if the object can be stored into the array. If not, throws
50   // an ArrayStoreException and returns false.
51   // TODO fix thread safety analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
52   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
53   bool CheckAssignable(ObjPtr<T> object) NO_THREAD_SAFETY_ANALYSIS;
54 
55   ALWAYS_INLINE void Set(int32_t i, ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_);
56   // TODO fix thread safety analysis: should be REQUIRES_SHARED(Locks::mutator_lock_).
57   template<bool kTransactionActive, bool kCheckTransaction = true,
58       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
59   ALWAYS_INLINE void Set(int32_t i, ObjPtr<T> object) NO_THREAD_SAFETY_ANALYSIS;
60 
61   // Set element without bound and element type checks, to be used in limited
62   // circumstances, such as during boot image writing.
63   // TODO fix thread safety analysis broken by the use of template. This should be
64   // REQUIRES_SHARED(Locks::mutator_lock_).
65   template<bool kTransactionActive, bool kCheckTransaction = true,
66       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
67   ALWAYS_INLINE void SetWithoutChecks(int32_t i, ObjPtr<T> object) NO_THREAD_SAFETY_ANALYSIS;
68   // TODO fix thread safety analysis broken by the use of template. This should be
69   // REQUIRES_SHARED(Locks::mutator_lock_).
70   template<bool kTransactionActive, bool kCheckTransaction = true,
71       VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
72   ALWAYS_INLINE void SetWithoutChecksAndWriteBarrier(int32_t i, ObjPtr<T> object)
73       NO_THREAD_SAFETY_ANALYSIS;
74 
75   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags,
76            ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
77   ALWAYS_INLINE T* GetWithoutChecks(int32_t i) REQUIRES_SHARED(Locks::mutator_lock_);
78 
79   // Copy src into this array (dealing with overlaps as memmove does) without assignability checks.
80   void AssignableMemmove(int32_t dst_pos,
81                          ObjPtr<ObjectArray<T>> src,
82                          int32_t src_pos,
83                          int32_t count)
84       REQUIRES_SHARED(Locks::mutator_lock_);
85 
86   // Copy src into this array assuming no overlap and without assignability checks.
87   void AssignableMemcpy(int32_t dst_pos,
88                         ObjPtr<ObjectArray<T>> src,
89                         int32_t src_pos,
90                         int32_t count)
91       REQUIRES_SHARED(Locks::mutator_lock_);
92 
93   // Copy src into this array with assignability checks.
94   template<bool kTransactionActive>
95   void AssignableCheckingMemcpy(int32_t dst_pos,
96                                 ObjPtr<ObjectArray<T>> src,
97                                 int32_t src_pos,
98                                 int32_t count,
99                                 bool throw_exception)
100       REQUIRES_SHARED(Locks::mutator_lock_);
101 
102   ObjectArray<T>* CopyOf(Thread* self, int32_t new_length)
103       REQUIRES_SHARED(Locks::mutator_lock_)
104       REQUIRES(!Roles::uninterruptible_);
105 
106   static MemberOffset OffsetOfElement(int32_t i);
107 
108  private:
109   // TODO fix thread safety analysis broken by the use of template. This should be
110   // REQUIRES_SHARED(Locks::mutator_lock_).
111   template<typename Visitor>
112   void VisitReferences(const Visitor& visitor) NO_THREAD_SAFETY_ANALYSIS;
113 
114   friend class Object;  // For VisitReferences
115   DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
116 };
117 
118 }  // namespace mirror
119 }  // namespace art
120 
121 #endif  // ART_RUNTIME_MIRROR_OBJECT_ARRAY_H_
122