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