blob: 506f00ab4df69e35b44c78521e4f234c5e154cef [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
Ian Rogers169c9a72011-11-13 20:13:17 -080020#include "logging.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070021#include "macros.h"
Ian Rogers169c9a72011-11-13 20:13:17 -080022
23namespace art {
Elliott Hughes68e76522011-10-05 13:22:16 -070024
25class Object;
Ian Rogers1f539342012-10-03 21:09:42 -070026class Thread;
Elliott Hughes68e76522011-10-05 13:22:16 -070027
Brian Carlstrom40381fb2011-10-19 14:13:40 -070028// 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 Hughes68e76522011-10-05 13:22:16 -070031class StackIndirectReferenceTable {
Elliott Hughesff17f1f2012-01-24 18:12:29 -080032 public:
Ian Rogers1f539342012-10-03 21:09:42 -070033 explicit StackIndirectReferenceTable(Object* object) : number_of_references_(1), link_(NULL) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070034 references_[0] = object;
Brian Carlstrom40381fb2011-10-19 14:13:40 -070035 }
36
Ian Rogers1f539342012-10-03 21:09:42 -070037 ~StackIndirectReferenceTable() {}
Brian Carlstrom40381fb2011-10-19 14:13:40 -070038
Elliott Hughes68e76522011-10-05 13:22:16 -070039 // Number of references contained within this SIRT
Brian Carlstrom40381fb2011-10-19 14:13:40 -070040 size_t NumberOfReferences() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070041 return number_of_references_;
42 }
43
44 // Link to previous SIRT or NULL
Brian Carlstrom40381fb2011-10-19 14:13:40 -070045 StackIndirectReferenceTable* GetLink() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070046 return link_;
47 }
48
Brian Carlstrom40381fb2011-10-19 14:13:40 -070049 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 Hughes68e76522011-10-05 13:22:16 -070071 }
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 Hughesff17f1f2012-01-24 18:12:29 -080083 private:
Elliott Hughes68e76522011-10-05 13:22:16 -070084 StackIndirectReferenceTable() {}
85
Shih-wei Liao3ad8b9d2012-03-04 23:41:35 -080086 size_t number_of_references_;
Elliott Hughes68e76522011-10-05 13:22:16 -070087 StackIndirectReferenceTable* link_;
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -080088
Brian Carlstrom40381fb2011-10-19 14:13:40 -070089 // number_of_references_ are available if this is allocated and filled in by jni_compiler.
90 Object* references_[1];
Elliott Hughes68e76522011-10-05 13:22:16 -070091
92 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
93};
94
Elliott Hughes68e76522011-10-05 13:22:16 -070095} // namespace art
96
97#endif // ART_SRC_STACK_INDIRECT_REFERENCE_TABLE_H_