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 | |
| 17 | #ifndef ART_SRC_STACK_INDIRECT_REFERENCE_TABLE_H_ |
| 18 | #define ART_SRC_STACK_INDIRECT_REFERENCE_TABLE_H_ |
| 19 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 20 | #include "logging.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 21 | #include "macros.h" |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 22 | #include "thread.h" |
| 23 | |
| 24 | namespace art { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 25 | |
| 26 | class Object; |
| 27 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 28 | // Stack allocated indirect reference table. It can allocated within |
| 29 | // the bridge frame between managed and native code backed by stack |
| 30 | // storage or manually allocated by SirtRef to hold one reference. |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 31 | class StackIndirectReferenceTable { |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 32 | public: |
| 33 | explicit StackIndirectReferenceTable(Object* object) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 34 | number_of_references_ = 1; |
| 35 | references_[0] = object; |
| 36 | Thread::Current()->PushSirt(this); |
| 37 | } |
| 38 | |
| 39 | ~StackIndirectReferenceTable() { |
| 40 | StackIndirectReferenceTable* sirt = Thread::Current()->PopSirt(); |
| 41 | CHECK_EQ(this, sirt); |
| 42 | } |
| 43 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 44 | // Number of references contained within this SIRT |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 45 | size_t NumberOfReferences() const { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 46 | return number_of_references_; |
| 47 | } |
| 48 | |
| 49 | // Link to previous SIRT or NULL |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 50 | StackIndirectReferenceTable* GetLink() const { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 51 | return link_; |
| 52 | } |
| 53 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 54 | void SetLink(StackIndirectReferenceTable* sirt) { |
| 55 | DCHECK_NE(this, sirt); |
| 56 | link_ = sirt; |
| 57 | } |
| 58 | |
| 59 | Object* GetReference(size_t i) const { |
| 60 | DCHECK_LT(i, number_of_references_); |
| 61 | return references_[i]; |
| 62 | } |
| 63 | |
| 64 | void SetReference(size_t i, Object* object) { |
| 65 | DCHECK_LT(i, number_of_references_); |
| 66 | references_[i] = object; |
| 67 | } |
| 68 | |
| 69 | bool Contains(Object** sirt_entry) const { |
| 70 | // A SIRT should always contain something. One created by the |
| 71 | // jni_compiler should have a jobject/jclass as a native method is |
| 72 | // passed in a this pointer or a class |
| 73 | DCHECK_GT(number_of_references_, 0U); |
| 74 | return ((&references_[0] <= sirt_entry) |
| 75 | && (sirt_entry <= (&references_[number_of_references_ - 1]))); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Offset of length within SIRT, used by generated code |
| 79 | static size_t NumberOfReferencesOffset() { |
| 80 | return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_); |
| 81 | } |
| 82 | |
| 83 | // Offset of link within SIRT, used by generated code |
| 84 | static size_t LinkOffset() { |
| 85 | return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_); |
| 86 | } |
| 87 | |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 88 | private: |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 89 | StackIndirectReferenceTable() {} |
| 90 | |
| 91 | size_t number_of_references_; |
| 92 | StackIndirectReferenceTable* link_; |
| 93 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 94 | // number_of_references_ are available if this is allocated and filled in by jni_compiler. |
| 95 | Object* references_[1]; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable); |
| 98 | }; |
| 99 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 100 | template<class T> |
| 101 | class SirtRef { |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 102 | public: |
| 103 | explicit SirtRef(T* object) : sirt_(object) {} |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 104 | ~SirtRef() {} |
| 105 | |
| 106 | T& operator*() const { return *get(); } |
| 107 | T* operator->() const { return get(); } |
| 108 | T* get() const { |
| 109 | return down_cast<T*>(sirt_.GetReference(0)); |
| 110 | } |
| 111 | |
| 112 | void reset(T* object = NULL) { |
| 113 | sirt_.SetReference(0, object); |
| 114 | } |
| 115 | |
Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 116 | private: |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 117 | StackIndirectReferenceTable sirt_; |
| 118 | |
| 119 | DISALLOW_COPY_AND_ASSIGN(SirtRef); |
| 120 | }; |
| 121 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 122 | } // namespace art |
| 123 | |
| 124 | #endif // ART_SRC_STACK_INDIRECT_REFERENCE_TABLE_H_ |