dex_cache.h revision b99b8d6cffe08d8c9d30175c936e5c88d3101802
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_DEX_CACHE_H_
18#define ART_RUNTIME_MIRROR_DEX_CACHE_H_
19
20#include "art_method.h"
21#include "object.h"
22#include "object_array.h"
23
24namespace art {
25
26struct DexCacheOffsets;
27class DexFile;
28class ImageWriter;
29union JValue;
30
31namespace mirror {
32
33class ArtField;
34class ArtMethod;
35class Class;
36class String;
37
38// C++ mirror of java.lang.DexCache.
39class MANAGED DexCache FINAL : public Object {
40 public:
41  // Size of java.lang.DexCache.class.
42  static uint32_t ClassSize();
43
44  // Size of an instance of java.lang.DexCache not including referenced values.
45  static constexpr uint32_t InstanceSize() {
46    return sizeof(DexCache);
47  }
48
49  void Init(const DexFile* dex_file,
50            String* location,
51            ObjectArray<String>* strings,
52            ObjectArray<Class>* types,
53            ObjectArray<ArtMethod>* methods,
54            ObjectArray<ArtField>* fields)
55      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
56
57  void Fixup(ArtMethod* trampoline) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
58
59  String* GetLocation() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
60    return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_));
61  }
62
63  static MemberOffset StringsOffset() {
64    return OFFSET_OF_OBJECT_MEMBER(DexCache, strings_);
65  }
66
67  static MemberOffset ResolvedFieldsOffset() {
68    return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_fields_);
69  }
70
71  static MemberOffset ResolvedMethodsOffset() {
72    return OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_methods_);
73  }
74
75  size_t NumStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
76    return GetStrings()->GetLength();
77  }
78
79  size_t NumResolvedTypes() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
80    return GetResolvedTypes()->GetLength();
81  }
82
83  size_t NumResolvedMethods() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
84    return GetResolvedMethods()->GetLength();
85  }
86
87  size_t NumResolvedFields() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
88    return GetResolvedFields()->GetLength();
89  }
90
91  String* GetResolvedString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
92    return GetStrings()->Get(string_idx);
93  }
94
95  void SetResolvedString(uint32_t string_idx, String* resolved) ALWAYS_INLINE
96      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
97    // TODO default transaction support.
98    GetStrings()->Set(string_idx, resolved);
99  }
100
101  Class* GetResolvedType(uint32_t type_idx) ALWAYS_INLINE
102      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
103    return GetResolvedTypes()->Get(type_idx);
104  }
105
106  void SetResolvedType(uint32_t type_idx, Class* resolved)
107      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108
109  ArtMethod* GetResolvedMethod(uint32_t method_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
110
111  void SetResolvedMethod(uint32_t method_idx, ArtMethod* resolved) ALWAYS_INLINE
112      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
113    GetResolvedMethods()->Set(method_idx, resolved);
114  }
115
116  ArtField* GetResolvedField(uint32_t field_idx) ALWAYS_INLINE
117      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
118    return GetResolvedFields()->Get(field_idx);
119  }
120
121  void SetResolvedField(uint32_t field_idx, ArtField* resolved) ALWAYS_INLINE
122      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
123    GetResolvedFields()->Set(field_idx, resolved);
124  }
125
126  ObjectArray<String>* GetStrings() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
127    return GetFieldObject< ObjectArray<String>>(StringsOffset());
128  }
129
130  ObjectArray<Class>* GetResolvedTypes() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
131    return GetFieldObject<ObjectArray<Class>>(
132        OFFSET_OF_OBJECT_MEMBER(DexCache, resolved_types_));
133  }
134
135  ObjectArray<ArtMethod>* GetResolvedMethods() ALWAYS_INLINE
136      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
137    return GetFieldObject< ObjectArray<ArtMethod>>(ResolvedMethodsOffset());
138  }
139
140  ObjectArray<ArtField>* GetResolvedFields() ALWAYS_INLINE
141      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
142    return GetFieldObject<ObjectArray<ArtField>>(ResolvedFieldsOffset());
143  }
144
145  const DexFile* GetDexFile() ALWAYS_INLINE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
146    return GetFieldPtr<const DexFile*>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_));
147  }
148
149  void SetDexFile(const DexFile* dex_file) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
150      ALWAYS_INLINE {
151    return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, dex_file_), dex_file);
152  }
153
154 private:
155  HeapReference<Object> dex_;
156  HeapReference<String> location_;
157  HeapReference<ObjectArray<ArtField>> resolved_fields_;
158  HeapReference<ObjectArray<ArtMethod>> resolved_methods_;
159  HeapReference<ObjectArray<Class>> resolved_types_;
160  HeapReference<ObjectArray<String>> strings_;
161  uint64_t dex_file_;
162
163  friend struct art::DexCacheOffsets;  // for verifying offset information
164  DISALLOW_IMPLICIT_CONSTRUCTORS(DexCache);
165};
166
167}  // namespace mirror
168}  // namespace art
169
170#endif  // ART_RUNTIME_MIRROR_DEX_CACHE_H_
171