Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_ |
| 18 | #define ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_ |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 20 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 21 | #include "base/macros.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 22 | #include "stack.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame^] | 23 | #include "utils.h" |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | namespace mirror { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 27 | class Object; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 29 | class Thread; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 31 | // 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 Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 34 | class StackIndirectReferenceTable { |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 35 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | explicit StackIndirectReferenceTable(mirror::Object* object) : |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 37 | link_(NULL), number_of_references_(1) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 38 | references_[0].Assign(object); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 41 | ~StackIndirectReferenceTable() {} |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 42 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 43 | // Number of references contained within this SIRT. |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 44 | uint32_t NumberOfReferences() const { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 45 | return number_of_references_; |
| 46 | } |
| 47 | |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 48 | // 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 Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 52 | // 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 Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 59 | // 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 Gampe | 242947d | 2014-04-03 13:31:32 -0700 | [diff] [blame] | 65 | // 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 Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 68 | size_t header_size = pointer_size + sizeof(number_of_references_); |
Andreas Gampe | 242947d | 2014-04-03 13:31:32 -0700 | [diff] [blame] | 69 | // 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 Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 75 | // Link to previous SIRT or NULL. |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 76 | StackIndirectReferenceTable* GetLink() const { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 77 | return link_; |
| 78 | } |
| 79 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 80 | void SetLink(StackIndirectReferenceTable* sirt) { |
| 81 | DCHECK_NE(this, sirt); |
| 82 | link_ = sirt; |
| 83 | } |
| 84 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 85 | // 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 Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 91 | mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 92 | DCHECK_LT(i, number_of_references_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 93 | return references_[i].AsMirrorPtr(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 96 | 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 Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 102 | void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 103 | DCHECK_LT(i, number_of_references_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 104 | references_[i].Assign(object); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 107 | bool Contains(StackReference<mirror::Object>* sirt_entry) const { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 108 | // 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 Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Dmitry Petrochenko | f0513c5 | 2014-03-31 09:12:31 +0700 | [diff] [blame] | 116 | // Offset of link within SIRT, used by generated code |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 117 | static size_t LinkOffset(size_t pointer_size) { |
| 118 | return 0; |
Dmitry Petrochenko | f0513c5 | 2014-03-31 09:12:31 +0700 | [diff] [blame] | 119 | } |
| 120 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 121 | // Offset of length within SIRT, used by generated code |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 122 | static size_t NumberOfReferencesOffset(size_t pointer_size) { |
| 123 | return pointer_size; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Offset of link within SIRT, used by generated code |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 127 | static size_t ReferencesOffset(size_t pointer_size) { |
| 128 | return pointer_size + sizeof(number_of_references_); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 131 | private: |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 132 | StackIndirectReferenceTable() {} |
| 133 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 134 | StackIndirectReferenceTable* link_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 135 | uint32_t number_of_references_; |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 136 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 137 | // number_of_references_ are available if this is allocated and filled in by jni_compiler. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 138 | StackReference<mirror::Object> references_[1]; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 139 | |
| 140 | DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable); |
| 141 | }; |
| 142 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 143 | } // namespace art |
| 144 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 145 | #endif // ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_ |