| 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 |  | 
 | 23 | namespace art { | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 24 |  | 
 | 25 | class Object; | 
| Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 26 | class Thread; | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 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: | 
| Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 33 |   explicit StackIndirectReferenceTable(Object* object) : number_of_references_(1), link_(NULL) { | 
| Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 34 |     references_[0] = object; | 
| Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 35 |   } | 
 | 36 |  | 
| Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 37 |   ~StackIndirectReferenceTable() {} | 
| Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 38 |  | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 39 |   // Number of references contained within this SIRT | 
| Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 40 |   size_t NumberOfReferences() const { | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 41 |     return number_of_references_; | 
 | 42 |   } | 
 | 43 |  | 
 | 44 |   // Link to previous SIRT or NULL | 
| Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 45 |   StackIndirectReferenceTable* GetLink() const { | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 46 |     return link_; | 
 | 47 |   } | 
 | 48 |  | 
| Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 49 |   void SetLink(StackIndirectReferenceTable* sirt) { | 
 | 50 |     DCHECK_NE(this, sirt); | 
 | 51 |     link_ = sirt; | 
 | 52 |   } | 
 | 53 |  | 
 | 54 |   Object* GetReference(size_t i) const { | 
 | 55 |     DCHECK_LT(i, number_of_references_); | 
 | 56 |     return references_[i]; | 
 | 57 |   } | 
 | 58 |  | 
 | 59 |   void SetReference(size_t i, Object* object) { | 
 | 60 |     DCHECK_LT(i, number_of_references_); | 
 | 61 |     references_[i] = object; | 
 | 62 |   } | 
 | 63 |  | 
 | 64 |   bool Contains(Object** sirt_entry) const { | 
 | 65 |     // A SIRT should always contain something. One created by the | 
 | 66 |     // jni_compiler should have a jobject/jclass as a native method is | 
 | 67 |     // passed in a this pointer or a class | 
 | 68 |     DCHECK_GT(number_of_references_, 0U); | 
 | 69 |     return ((&references_[0] <= sirt_entry) | 
 | 70 |             && (sirt_entry <= (&references_[number_of_references_ - 1]))); | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 71 |   } | 
 | 72 |  | 
 | 73 |   // Offset of length within SIRT, used by generated code | 
 | 74 |   static size_t NumberOfReferencesOffset() { | 
 | 75 |     return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_); | 
 | 76 |   } | 
 | 77 |  | 
 | 78 |   // Offset of link within SIRT, used by generated code | 
 | 79 |   static size_t LinkOffset() { | 
 | 80 |     return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_); | 
 | 81 |   } | 
 | 82 |  | 
| Elliott Hughes | ff17f1f | 2012-01-24 18:12:29 -0800 | [diff] [blame] | 83 |  private: | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 84 |   StackIndirectReferenceTable() {} | 
 | 85 |  | 
| Shih-wei Liao | 3ad8b9d | 2012-03-04 23:41:35 -0800 | [diff] [blame] | 86 |   size_t number_of_references_; | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 87 |   StackIndirectReferenceTable* link_; | 
| Shih-wei Liao | a8a9c34 | 2012-03-03 22:35:16 -0800 | [diff] [blame] | 88 |  | 
| Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 89 |   // number_of_references_ are available if this is allocated and filled in by jni_compiler. | 
 | 90 |   Object* references_[1]; | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 91 |  | 
 | 92 |   DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable); | 
 | 93 | }; | 
 | 94 |  | 
| Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 95 | }  // namespace art | 
 | 96 |  | 
 | 97 | #endif  // ART_SRC_STACK_INDIRECT_REFERENCE_TABLE_H_ |