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 | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 23 | |
| 24 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | namespace mirror { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 26 | class Object; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 28 | class Thread; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 29 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 30 | // 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 Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 33 | class StackIndirectReferenceTable { |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 34 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | explicit StackIndirectReferenceTable(mirror::Object* object) : |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame^] | 36 | link_(NULL), number_of_references_(1) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 37 | references_[0].Assign(object); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 40 | ~StackIndirectReferenceTable() {} |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 42 | // Number of references contained within this SIRT |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame^] | 43 | uint32_t NumberOfReferences() const { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 44 | return number_of_references_; |
| 45 | } |
| 46 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame^] | 47 | // 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 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 54 | // Link to previous SIRT or NULL |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 55 | StackIndirectReferenceTable* GetLink() const { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 56 | return link_; |
| 57 | } |
| 58 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 59 | void SetLink(StackIndirectReferenceTable* sirt) { |
| 60 | DCHECK_NE(this, sirt); |
| 61 | link_ = sirt; |
| 62 | } |
| 63 | |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame^] | 64 | // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will |
| 65 | // not resize anything. |
| 66 | void SetNumberOfReferences(uint32_t num_references) { |
| 67 | number_of_references_ = num_references; |
| 68 | } |
| 69 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 70 | 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] | 71 | DCHECK_LT(i, number_of_references_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 72 | return references_[i].AsMirrorPtr(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 75 | 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] | 76 | DCHECK_LT(i, number_of_references_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 77 | references_[i].Assign(object); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 80 | bool Contains(StackReference<mirror::Object>* sirt_entry) const { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 81 | // A SIRT should always contain something. One created by the |
| 82 | // jni_compiler should have a jobject/jclass as a native method is |
| 83 | // passed in a this pointer or a class |
| 84 | DCHECK_GT(number_of_references_, 0U); |
| 85 | return ((&references_[0] <= sirt_entry) |
| 86 | && (sirt_entry <= (&references_[number_of_references_ - 1]))); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Offset of length within SIRT, used by generated code |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame^] | 90 | static uint32_t NumberOfReferencesOffset() { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 91 | return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_); |
| 92 | } |
| 93 | |
| 94 | // Offset of link within SIRT, used by generated code |
| 95 | static size_t LinkOffset() { |
| 96 | return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_); |
| 97 | } |
| 98 | |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 99 | private: |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 100 | StackIndirectReferenceTable() {} |
| 101 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 102 | StackIndirectReferenceTable* link_; |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame^] | 103 | uint32_t number_of_references_; |
Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 104 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 105 | // 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] | 106 | StackReference<mirror::Object> references_[1]; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 107 | |
| 108 | DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable); |
| 109 | }; |
| 110 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 111 | } // namespace art |
| 112 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 113 | #endif // ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_ |