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