blob: d22650b6f22305423da5f2950ffccb7e4c3fc904 [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 Rogersef7d42f2014-01-06 12:55:46 -080022#include "stack.h"
Ian Rogers169c9a72011-11-13 20:13:17 -080023
24namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025namespace mirror {
Elliott Hughes68e76522011-10-05 13:22:16 -070026class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027}
Ian Rogers1f539342012-10-03 21:09:42 -070028class Thread;
Elliott Hughes68e76522011-10-05 13:22:16 -070029
Brian Carlstrom40381fb2011-10-19 14:13:40 -070030// Stack allocated indirect reference table. It can allocated within
31// the bridge frame between managed and native code backed by stack
32// storage or manually allocated by SirtRef to hold one reference.
Elliott Hughes68e76522011-10-05 13:22:16 -070033class StackIndirectReferenceTable {
Elliott Hughesff17f1f2012-01-24 18:12:29 -080034 public:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 explicit StackIndirectReferenceTable(mirror::Object* object) :
36 number_of_references_(1), link_(NULL) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080037 references_[0].Assign(object);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070038 }
39
Ian Rogers1f539342012-10-03 21:09:42 -070040 ~StackIndirectReferenceTable() {}
Brian Carlstrom40381fb2011-10-19 14:13:40 -070041
Elliott Hughes68e76522011-10-05 13:22:16 -070042 // Number of references contained within this SIRT
Brian Carlstrom40381fb2011-10-19 14:13:40 -070043 size_t NumberOfReferences() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070044 return number_of_references_;
45 }
46
47 // Link to previous SIRT or NULL
Brian Carlstrom40381fb2011-10-19 14:13:40 -070048 StackIndirectReferenceTable* GetLink() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070049 return link_;
50 }
51
Brian Carlstrom40381fb2011-10-19 14:13:40 -070052 void SetLink(StackIndirectReferenceTable* sirt) {
53 DCHECK_NE(this, sirt);
54 link_ = sirt;
55 }
56
Ian Rogersef7d42f2014-01-06 12:55:46 -080057 mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070058 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080059 return references_[i].AsMirrorPtr();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070060 }
61
Ian Rogersef7d42f2014-01-06 12:55:46 -080062 void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070063 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080064 references_[i].Assign(object);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070065 }
66
Ian Rogersef7d42f2014-01-06 12:55:46 -080067 bool Contains(StackReference<mirror::Object>* sirt_entry) const {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070068 // A SIRT should always contain something. One created by the
69 // jni_compiler should have a jobject/jclass as a native method is
70 // passed in a this pointer or a class
71 DCHECK_GT(number_of_references_, 0U);
72 return ((&references_[0] <= sirt_entry)
73 && (sirt_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -070074 }
75
76 // Offset of length within SIRT, used by generated code
77 static size_t NumberOfReferencesOffset() {
78 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
79 }
80
81 // Offset of link within SIRT, used by generated code
82 static size_t LinkOffset() {
83 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
84 }
85
Elliott Hughesff17f1f2012-01-24 18:12:29 -080086 private:
Elliott Hughes68e76522011-10-05 13:22:16 -070087 StackIndirectReferenceTable() {}
88
Shih-wei Liao3ad8b9d2012-03-04 23:41:35 -080089 size_t number_of_references_;
Elliott Hughes68e76522011-10-05 13:22:16 -070090 StackIndirectReferenceTable* link_;
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -080091
Brian Carlstrom40381fb2011-10-19 14:13:40 -070092 // number_of_references_ are available if this is allocated and filled in by jni_compiler.
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 StackReference<mirror::Object> references_[1];
Elliott Hughes68e76522011-10-05 13:22:16 -070094
95 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
96};
97
Elliott Hughes68e76522011-10-05 13:22:16 -070098} // namespace art
99
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700100#endif // ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_