blob: 3b632e7806ae7aaea97a6330433feb798844f48a [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 Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "utils.h"
Ian Rogers169c9a72011-11-13 20:13:17 -080024
25namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026namespace mirror {
Elliott Hughes68e76522011-10-05 13:22:16 -070027class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028}
Ian Rogers1f539342012-10-03 21:09:42 -070029class Thread;
Elliott Hughes68e76522011-10-05 13:22:16 -070030
Brian Carlstrom40381fb2011-10-19 14:13:40 -070031// Stack allocated indirect reference table. It can allocated within
32// the bridge frame between managed and native code backed by stack
33// storage or manually allocated by SirtRef to hold one reference.
Elliott Hughes68e76522011-10-05 13:22:16 -070034class StackIndirectReferenceTable {
Elliott Hughesff17f1f2012-01-24 18:12:29 -080035 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 explicit StackIndirectReferenceTable(mirror::Object* object) :
Andreas Gampebf6b92a2014-03-05 16:11:04 -080037 link_(NULL), number_of_references_(1) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080038 references_[0].Assign(object);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070039 }
40
Ian Rogers1f539342012-10-03 21:09:42 -070041 ~StackIndirectReferenceTable() {}
Brian Carlstrom40381fb2011-10-19 14:13:40 -070042
Andreas Gampe36fea8d2014-03-10 13:37:40 -070043 // Number of references contained within this SIRT.
Andreas Gampebf6b92a2014-03-05 16:11:04 -080044 uint32_t NumberOfReferences() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070045 return number_of_references_;
46 }
47
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070048 // We have versions with and without explicit pointer size of the following. The first two are
49 // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one
50 // takes the pointer size explicitly so that at compile time we can cross-compile correctly.
51
Andreas Gampebf6b92a2014-03-05 16:11:04 -080052 // Returns the size of a StackIndirectReferenceTable containing num_references sirts.
53 static size_t SizeOf(uint32_t num_references) {
54 size_t header_size = OFFSETOF_MEMBER(StackIndirectReferenceTable, references_);
55 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
56 return header_size + data_size;
57 }
58
Andreas Gampe36fea8d2014-03-10 13:37:40 -070059 // Get the size of the SIRT for the number of entries, with padding added for potential alignment.
60 static size_t GetAlignedSirtSize(uint32_t num_references) {
61 size_t sirt_size = SizeOf(num_references);
62 return RoundUp(sirt_size, 8);
63 }
64
Andreas Gampe242947d2014-04-03 13:31:32 -070065 // Get the size of the SIRT for the number of entries, with padding added for potential alignment.
66 static size_t GetAlignedSirtSizeTarget(size_t pointer_size, uint32_t num_references) {
67 // Assume that the layout is packed.
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070068 size_t header_size = pointer_size + sizeof(number_of_references_);
Andreas Gampe242947d2014-04-03 13:31:32 -070069 // This assumes there is no layout change between 32 and 64b.
70 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
71 size_t sirt_size = header_size + data_size;
72 return RoundUp(sirt_size, 8);
73 }
74
Andreas Gampe36fea8d2014-03-10 13:37:40 -070075 // Link to previous SIRT or NULL.
Brian Carlstrom40381fb2011-10-19 14:13:40 -070076 StackIndirectReferenceTable* GetLink() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070077 return link_;
78 }
79
Brian Carlstrom40381fb2011-10-19 14:13:40 -070080 void SetLink(StackIndirectReferenceTable* sirt) {
81 DCHECK_NE(this, sirt);
82 link_ = sirt;
83 }
84
Andreas Gampebf6b92a2014-03-05 16:11:04 -080085 // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will
86 // not resize anything.
87 void SetNumberOfReferences(uint32_t num_references) {
88 number_of_references_ = num_references;
89 }
90
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070092 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 return references_[i].AsMirrorPtr();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070094 }
95
Andreas Gampe36fea8d2014-03-10 13:37:40 -070096 StackReference<mirror::Object>* GetStackReference(size_t i)
97 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
98 DCHECK_LT(i, number_of_references_);
99 return &references_[i];
100 }
101
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102 void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700103 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104 references_[i].Assign(object);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700105 }
106
Ian Rogersef7d42f2014-01-06 12:55:46 -0800107 bool Contains(StackReference<mirror::Object>* sirt_entry) const {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700108 // A SIRT should always contain something. One created by the
109 // jni_compiler should have a jobject/jclass as a native method is
110 // passed in a this pointer or a class
111 DCHECK_GT(number_of_references_, 0U);
112 return ((&references_[0] <= sirt_entry)
113 && (sirt_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -0700114 }
115
Dmitry Petrochenkof0513c52014-03-31 09:12:31 +0700116 // Offset of link within SIRT, used by generated code
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700117 static size_t LinkOffset(size_t pointer_size) {
118 return 0;
Dmitry Petrochenkof0513c52014-03-31 09:12:31 +0700119 }
120
Elliott Hughes68e76522011-10-05 13:22:16 -0700121 // Offset of length within SIRT, used by generated code
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700122 static size_t NumberOfReferencesOffset(size_t pointer_size) {
123 return pointer_size;
Elliott Hughes68e76522011-10-05 13:22:16 -0700124 }
125
126 // Offset of link within SIRT, used by generated code
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700127 static size_t ReferencesOffset(size_t pointer_size) {
128 return pointer_size + sizeof(number_of_references_);
Elliott Hughes68e76522011-10-05 13:22:16 -0700129 }
130
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800131 private:
Elliott Hughes68e76522011-10-05 13:22:16 -0700132 StackIndirectReferenceTable() {}
133
Elliott Hughes68e76522011-10-05 13:22:16 -0700134 StackIndirectReferenceTable* link_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800135 uint32_t number_of_references_;
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800136
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700137 // number_of_references_ are available if this is allocated and filled in by jni_compiler.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138 StackReference<mirror::Object> references_[1];
Elliott Hughes68e76522011-10-05 13:22:16 -0700139
140 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
141};
142
Elliott Hughes68e76522011-10-05 13:22:16 -0700143} // namespace art
144
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700145#endif // ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_