blob: b113129db5fbff6f324d2da62e7e0fbf636f90ff [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
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070047 // We have versions with and without explicit pointer size of the following. The first two are
48 // used at runtime, so OFFSETOF_MEMBER computes the right offsets automatically. The last one
49 // takes the pointer size explicitly so that at compile time we can cross-compile correctly.
50
Andreas Gampebf6b92a2014-03-05 16:11:04 -080051 // Returns the size of a StackIndirectReferenceTable containing num_references sirts.
52 static size_t SizeOf(uint32_t num_references) {
53 size_t header_size = OFFSETOF_MEMBER(StackIndirectReferenceTable, references_);
54 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
55 return header_size + data_size;
56 }
57
Andreas Gampe36fea8d2014-03-10 13:37:40 -070058 // Get the size of the SIRT for the number of entries, with padding added for potential alignment.
59 static size_t GetAlignedSirtSize(uint32_t num_references) {
60 size_t sirt_size = SizeOf(num_references);
61 return RoundUp(sirt_size, 8);
62 }
63
Andreas Gampe242947d2014-04-03 13:31:32 -070064 // Get the size of the SIRT for the number of entries, with padding added for potential alignment.
65 static size_t GetAlignedSirtSizeTarget(size_t pointer_size, uint32_t num_references) {
66 // Assume that the layout is packed.
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070067 size_t header_size = pointer_size + sizeof(number_of_references_);
Andreas Gampe242947d2014-04-03 13:31:32 -070068 // This assumes there is no layout change between 32 and 64b.
69 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
70 size_t sirt_size = header_size + data_size;
71 return RoundUp(sirt_size, 8);
72 }
73
Andreas Gampe36fea8d2014-03-10 13:37:40 -070074 // Link to previous SIRT or NULL.
Brian Carlstrom40381fb2011-10-19 14:13:40 -070075 StackIndirectReferenceTable* GetLink() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070076 return link_;
77 }
78
Brian Carlstrom40381fb2011-10-19 14:13:40 -070079 void SetLink(StackIndirectReferenceTable* sirt) {
80 DCHECK_NE(this, sirt);
81 link_ = sirt;
82 }
83
Andreas Gampebf6b92a2014-03-05 16:11:04 -080084 // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will
85 // not resize anything.
86 void SetNumberOfReferences(uint32_t num_references) {
87 number_of_references_ = num_references;
88 }
89
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070091 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 return references_[i].AsMirrorPtr();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070093 }
94
Andreas Gampe36fea8d2014-03-10 13:37:40 -070095 StackReference<mirror::Object>* GetStackReference(size_t i)
96 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
97 DCHECK_LT(i, number_of_references_);
98 return &references_[i];
99 }
100
Ian Rogersef7d42f2014-01-06 12:55:46 -0800101 void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700102 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103 references_[i].Assign(object);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700104 }
105
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 bool Contains(StackReference<mirror::Object>* sirt_entry) const {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700107 // A SIRT should always contain something. One created by the
108 // jni_compiler should have a jobject/jclass as a native method is
109 // passed in a this pointer or a class
110 DCHECK_GT(number_of_references_, 0U);
111 return ((&references_[0] <= sirt_entry)
112 && (sirt_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -0700113 }
114
Dmitry Petrochenkof0513c52014-03-31 09:12:31 +0700115 // Offset of link within SIRT, used by generated code
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700116 static size_t LinkOffset(size_t pointer_size) {
117 return 0;
Dmitry Petrochenkof0513c52014-03-31 09:12:31 +0700118 }
119
Elliott Hughes68e76522011-10-05 13:22:16 -0700120 // Offset of length within SIRT, used by generated code
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700121 static size_t NumberOfReferencesOffset(size_t pointer_size) {
122 return pointer_size;
Elliott Hughes68e76522011-10-05 13:22:16 -0700123 }
124
125 // Offset of link within SIRT, used by generated code
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700126 static size_t ReferencesOffset(size_t pointer_size) {
127 return pointer_size + sizeof(number_of_references_);
Elliott Hughes68e76522011-10-05 13:22:16 -0700128 }
129
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800130 private:
Elliott Hughes68e76522011-10-05 13:22:16 -0700131 StackIndirectReferenceTable() {}
132
Elliott Hughes68e76522011-10-05 13:22:16 -0700133 StackIndirectReferenceTable* link_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800134 uint32_t number_of_references_;
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800135
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700136 // number_of_references_ are available if this is allocated and filled in by jni_compiler.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137 StackReference<mirror::Object> references_[1];
Elliott Hughes68e76522011-10-05 13:22:16 -0700138
139 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
140};
141
Elliott Hughes68e76522011-10-05 13:22:16 -0700142} // namespace art
143
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700144#endif // ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_