blob: 4c9b038423afd583b15890619eec89af099f9d1e [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_
18#define ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_
Elliott Hughes68e76522011-10-05 13:22:16 -070019
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080021#include "base/macros.h"
Ian Rogers169c9a72011-11-13 20:13:17 -080022
23namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024namespace mirror {
Elliott Hughes68e76522011-10-05 13:22:16 -070025class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026}
Ian Rogers1f539342012-10-03 21:09:42 -070027class Thread;
Elliott Hughes68e76522011-10-05 13:22:16 -070028
Brian Carlstrom40381fb2011-10-19 14:13:40 -070029// Stack allocated indirect reference table. It can allocated within
30// the bridge frame between managed and native code backed by stack
31// storage or manually allocated by SirtRef to hold one reference.
Elliott Hughes68e76522011-10-05 13:22:16 -070032class StackIndirectReferenceTable {
Elliott Hughesff17f1f2012-01-24 18:12:29 -080033 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034 explicit StackIndirectReferenceTable(mirror::Object* object) :
35 number_of_references_(1), link_(NULL) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070036 references_[0] = object;
Brian Carlstrom40381fb2011-10-19 14:13:40 -070037 }
38
Ian Rogers1f539342012-10-03 21:09:42 -070039 ~StackIndirectReferenceTable() {}
Brian Carlstrom40381fb2011-10-19 14:13:40 -070040
Elliott Hughes68e76522011-10-05 13:22:16 -070041 // Number of references contained within this SIRT
Brian Carlstrom40381fb2011-10-19 14:13:40 -070042 size_t NumberOfReferences() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070043 return number_of_references_;
44 }
45
46 // Link to previous SIRT or NULL
Brian Carlstrom40381fb2011-10-19 14:13:40 -070047 StackIndirectReferenceTable* GetLink() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070048 return link_;
49 }
50
Brian Carlstrom40381fb2011-10-19 14:13:40 -070051 void SetLink(StackIndirectReferenceTable* sirt) {
52 DCHECK_NE(this, sirt);
53 link_ = sirt;
54 }
55
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056 mirror::Object* GetReference(size_t i) const {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070057 DCHECK_LT(i, number_of_references_);
58 return references_[i];
59 }
60
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 void SetReference(size_t i, mirror::Object* object) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070062 DCHECK_LT(i, number_of_references_);
63 references_[i] = object;
64 }
65
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 bool Contains(mirror::Object** sirt_entry) const {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070067 // A SIRT should always contain something. One created by the
68 // jni_compiler should have a jobject/jclass as a native method is
69 // passed in a this pointer or a class
70 DCHECK_GT(number_of_references_, 0U);
71 return ((&references_[0] <= sirt_entry)
72 && (sirt_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -070073 }
74
75 // Offset of length within SIRT, used by generated code
76 static size_t NumberOfReferencesOffset() {
77 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
78 }
79
80 // Offset of link within SIRT, used by generated code
81 static size_t LinkOffset() {
82 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
83 }
84
Elliott Hughesff17f1f2012-01-24 18:12:29 -080085 private:
Elliott Hughes68e76522011-10-05 13:22:16 -070086 StackIndirectReferenceTable() {}
87
Shih-wei Liao3ad8b9d2012-03-04 23:41:35 -080088 size_t number_of_references_;
Elliott Hughes68e76522011-10-05 13:22:16 -070089 StackIndirectReferenceTable* link_;
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -080090
Brian Carlstrom40381fb2011-10-19 14:13:40 -070091 // number_of_references_ are available if this is allocated and filled in by jni_compiler.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 mirror::Object* references_[1];
Elliott Hughes68e76522011-10-05 13:22:16 -070093
94 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
95};
96
Elliott Hughes68e76522011-10-05 13:22:16 -070097} // namespace art
98
Brian Carlstromfc0e3212013-07-17 14:40:12 -070099#endif // ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_