blob: daef3ff14079cde6445f70a52da19562cac5ad85 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_
18#define ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_
Elliott Hughes68e76522011-10-05 13:22:16 -070019
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080021#include "base/macros.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080022#include "stack.h"
Ian Rogers169c9a72011-11-13 20:13:17 -080023
24namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025namespace mirror {
Elliott Hughes68e76522011-10-05 13:22:16 -070026class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027}
Ian Rogers1f539342012-10-03 21:09:42 -070028class Thread;
Elliott Hughes68e76522011-10-05 13:22:16 -070029
Brian Carlstrom40381fb2011-10-19 14:13:40 -070030// Stack allocated indirect reference table. It can allocated within
31// the bridge frame between managed and native code backed by stack
32// storage or manually allocated by SirtRef to hold one reference.
Elliott Hughes68e76522011-10-05 13:22:16 -070033class StackIndirectReferenceTable {
Elliott Hughesff17f1f2012-01-24 18:12:29 -080034 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 explicit StackIndirectReferenceTable(mirror::Object* object) :
Andreas Gampebf6b92a2014-03-05 16:11:04 -080036 link_(NULL), number_of_references_(1) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080037 references_[0].Assign(object);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070038 }
39
Ian Rogers1f539342012-10-03 21:09:42 -070040 ~StackIndirectReferenceTable() {}
Brian Carlstrom40381fb2011-10-19 14:13:40 -070041
Andreas Gampe36fea8d2014-03-10 13:37:40 -070042 // Number of references contained within this SIRT.
Andreas Gampebf6b92a2014-03-05 16:11:04 -080043 uint32_t NumberOfReferences() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070044 return number_of_references_;
45 }
46
Andreas Gampebf6b92a2014-03-05 16:11:04 -080047 // Returns the size of a StackIndirectReferenceTable containing num_references sirts.
48 static size_t SizeOf(uint32_t num_references) {
49 size_t header_size = OFFSETOF_MEMBER(StackIndirectReferenceTable, references_);
50 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
51 return header_size + data_size;
52 }
53
Andreas Gampe36fea8d2014-03-10 13:37:40 -070054 // Get the size of the SIRT for the number of entries, with padding added for potential alignment.
55 static size_t GetAlignedSirtSize(uint32_t num_references) {
56 size_t sirt_size = SizeOf(num_references);
57 return RoundUp(sirt_size, 8);
58 }
59
60 // Link to previous SIRT or NULL.
Brian Carlstrom40381fb2011-10-19 14:13:40 -070061 StackIndirectReferenceTable* GetLink() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070062 return link_;
63 }
64
Brian Carlstrom40381fb2011-10-19 14:13:40 -070065 void SetLink(StackIndirectReferenceTable* sirt) {
66 DCHECK_NE(this, sirt);
67 link_ = sirt;
68 }
69
Andreas Gampebf6b92a2014-03-05 16:11:04 -080070 // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will
71 // not resize anything.
72 void SetNumberOfReferences(uint32_t num_references) {
73 number_of_references_ = num_references;
74 }
75
Ian Rogersef7d42f2014-01-06 12:55:46 -080076 mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070077 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080078 return references_[i].AsMirrorPtr();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070079 }
80
Andreas Gampe36fea8d2014-03-10 13:37:40 -070081 StackReference<mirror::Object>* GetStackReference(size_t i)
82 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
83 DCHECK_LT(i, number_of_references_);
84 return &references_[i];
85 }
86
Ian Rogersef7d42f2014-01-06 12:55:46 -080087 void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070088 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080089 references_[i].Assign(object);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070090 }
91
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 bool Contains(StackReference<mirror::Object>* sirt_entry) const {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070093 // A SIRT should always contain something. One created by the
94 // jni_compiler should have a jobject/jclass as a native method is
95 // passed in a this pointer or a class
96 DCHECK_GT(number_of_references_, 0U);
97 return ((&references_[0] <= sirt_entry)
98 && (sirt_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -070099 }
100
Dmitry Petrochenkof0513c52014-03-31 09:12:31 +0700101 // Offset of link within SIRT, used by generated code
102 static size_t LinkOffset() {
103 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
104 }
105
Elliott Hughes68e76522011-10-05 13:22:16 -0700106 // Offset of length within SIRT, used by generated code
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800107 static uint32_t NumberOfReferencesOffset() {
Elliott Hughes68e76522011-10-05 13:22:16 -0700108 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
109 }
110
111 // Offset of link within SIRT, used by generated code
Dmitry Petrochenkof0513c52014-03-31 09:12:31 +0700112 static size_t ReferencesOffset() {
113 return OFFSETOF_MEMBER(StackIndirectReferenceTable, references_);
Elliott Hughes68e76522011-10-05 13:22:16 -0700114 }
115
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800116 private:
Elliott Hughes68e76522011-10-05 13:22:16 -0700117 StackIndirectReferenceTable() {}
118
Elliott Hughes68e76522011-10-05 13:22:16 -0700119 StackIndirectReferenceTable* link_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800120 uint32_t number_of_references_;
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800121
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700122 // number_of_references_ are available if this is allocated and filled in by jni_compiler.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123 StackReference<mirror::Object> references_[1];
Elliott Hughes68e76522011-10-05 13:22:16 -0700124
125 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
126};
127
Elliott Hughes68e76522011-10-05 13:22:16 -0700128} // namespace art
129
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700130#endif // ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_