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#include "dex_cache-inl.h"
18
19#include "art_method-inl.h"
20#include "base/logging.h"
21#include "class_linker.h"
22#include "gc/accounting/card_table-inl.h"
23#include "gc/heap.h"
24#include "globals.h"
25#include "object.h"
26#include "object-inl.h"
27#include "object_array-inl.h"
28#include "runtime.h"
29#include "string.h"
30
31namespace art {
32namespace mirror {
33
34void DexCache::Init(const DexFile* dex_file,
35                    String* location,
36                    GcRoot<String>* strings,
37                    uint32_t num_strings,
38                    GcRoot<Class>* resolved_types,
39                    uint32_t num_resolved_types,
40                    ArtMethod** resolved_methods,
41                    uint32_t num_resolved_methods,
42                    ArtField** resolved_fields,
43                    uint32_t num_resolved_fields,
44                    size_t pointer_size) {
45  CHECK(dex_file != nullptr);
46  CHECK(location != nullptr);
47  CHECK_EQ(num_strings != 0u, strings != nullptr);
48  CHECK_EQ(num_resolved_types != 0u, resolved_types != nullptr);
49  CHECK_EQ(num_resolved_methods != 0u, resolved_methods != nullptr);
50  CHECK_EQ(num_resolved_fields != 0u, resolved_fields != nullptr);
51
52  SetDexFile(dex_file);
53  SetLocation(location);
54  SetStrings(strings);
55  SetResolvedTypes(resolved_types);
56  SetResolvedMethods(resolved_methods);
57  SetResolvedFields(resolved_fields);
58  SetField32<false>(NumStringsOffset(), num_strings);
59  SetField32<false>(NumResolvedTypesOffset(), num_resolved_types);
60  SetField32<false>(NumResolvedMethodsOffset(), num_resolved_methods);
61  SetField32<false>(NumResolvedFieldsOffset(), num_resolved_fields);
62
63  Runtime* const runtime = Runtime::Current();
64  if (runtime->HasResolutionMethod()) {
65    // Initialize the resolve methods array to contain trampolines for resolution.
66    Fixup(runtime->GetResolutionMethod(), pointer_size);
67  }
68}
69
70void DexCache::Fixup(ArtMethod* trampoline, size_t pointer_size) {
71  // Fixup the resolve methods array to contain trampoline for resolution.
72  CHECK(trampoline != nullptr);
73  CHECK(trampoline->IsRuntimeMethod());
74  auto* resolved_methods = GetResolvedMethods();
75  for (size_t i = 0, length = NumResolvedMethods(); i < length; i++) {
76    if (GetElementPtrSize<ArtMethod*>(resolved_methods, i, pointer_size) == nullptr) {
77      SetElementPtrSize(resolved_methods, i, trampoline, pointer_size);
78    }
79  }
80}
81
82void DexCache::SetLocation(mirror::String* location) {
83  SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(DexCache, location_), location);
84}
85
86}  // namespace mirror
87}  // namespace art
88