blob: 6049e062a6321dcd8b68c55d4998b623892bb389 [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
Andreas Gampe242947d2014-04-03 13:31:32 -070060 // Get the size of the SIRT for the number of entries, with padding added for potential alignment.
61 static size_t GetAlignedSirtSizeTarget(size_t pointer_size, uint32_t num_references) {
62 // Assume that the layout is packed.
63 size_t header_size = pointer_size + sizeof(uint32_t);
64 // This assumes there is no layout change between 32 and 64b.
65 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
66 size_t sirt_size = header_size + data_size;
67 return RoundUp(sirt_size, 8);
68 }
69
Andreas Gampe36fea8d2014-03-10 13:37:40 -070070 // Link to previous SIRT or NULL.
Brian Carlstrom40381fb2011-10-19 14:13:40 -070071 StackIndirectReferenceTable* GetLink() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070072 return link_;
73 }
74
Brian Carlstrom40381fb2011-10-19 14:13:40 -070075 void SetLink(StackIndirectReferenceTable* sirt) {
76 DCHECK_NE(this, sirt);
77 link_ = sirt;
78 }
79
Andreas Gampebf6b92a2014-03-05 16:11:04 -080080 // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will
81 // not resize anything.
82 void SetNumberOfReferences(uint32_t num_references) {
83 number_of_references_ = num_references;
84 }
85
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070087 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080088 return references_[i].AsMirrorPtr();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070089 }
90
Andreas Gampe36fea8d2014-03-10 13:37:40 -070091 StackReference<mirror::Object>* GetStackReference(size_t i)
92 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
93 DCHECK_LT(i, number_of_references_);
94 return &references_[i];
95 }
96
Ian Rogersef7d42f2014-01-06 12:55:46 -080097 void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070098 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080099 references_[i].Assign(object);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700100 }
101
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102 bool Contains(StackReference<mirror::Object>* sirt_entry) const {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700103 // A SIRT should always contain something. One created by the
104 // jni_compiler should have a jobject/jclass as a native method is
105 // passed in a this pointer or a class
106 DCHECK_GT(number_of_references_, 0U);
107 return ((&references_[0] <= sirt_entry)
108 && (sirt_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -0700109 }
110
Dmitry Petrochenkof0513c52014-03-31 09:12:31 +0700111 // Offset of link within SIRT, used by generated code
112 static size_t LinkOffset() {
113 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
114 }
115
Elliott Hughes68e76522011-10-05 13:22:16 -0700116 // Offset of length within SIRT, used by generated code
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800117 static uint32_t NumberOfReferencesOffset() {
Elliott Hughes68e76522011-10-05 13:22:16 -0700118 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
119 }
120
121 // Offset of link within SIRT, used by generated code
Dmitry Petrochenkof0513c52014-03-31 09:12:31 +0700122 static size_t ReferencesOffset() {
123 return OFFSETOF_MEMBER(StackIndirectReferenceTable, references_);
Elliott Hughes68e76522011-10-05 13:22:16 -0700124 }
125
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800126 private:
Elliott Hughes68e76522011-10-05 13:22:16 -0700127 StackIndirectReferenceTable() {}
128
Elliott Hughes68e76522011-10-05 13:22:16 -0700129 StackIndirectReferenceTable* link_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800130 uint32_t number_of_references_;
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800131
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700132 // number_of_references_ are available if this is allocated and filled in by jni_compiler.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 StackReference<mirror::Object> references_[1];
Elliott Hughes68e76522011-10-05 13:22:16 -0700134
135 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
136};
137
Elliott Hughes68e76522011-10-05 13:22:16 -0700138} // namespace art
139
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700140#endif // ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_