blob: c2d6a595fb747cbc37edc27e41f4276e4f6dd77e [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) :
Andreas Gampebf6b92a2014-03-05 16:11:04 -080036 link_(NULL), number_of_references_(1) {
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
Andreas Gampebf6b92a2014-03-05 16:11:04 -080043 uint32_t NumberOfReferences() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070044 return number_of_references_;
45 }
46
Andreas Gampebf6b92a2014-03-05 16:11:04 -080047 // Returns the size of a StackIndirectReferenceTable containing num_references sirts.
48 static size_t SizeOf(uint32_t num_references) {
49 size_t header_size = OFFSETOF_MEMBER(StackIndirectReferenceTable, references_);
50 size_t data_size = sizeof(StackReference<mirror::Object>) * num_references;
51 return header_size + data_size;
52 }
53
Elliott Hughes68e76522011-10-05 13:22:16 -070054 // Link to previous SIRT or NULL
Brian Carlstrom40381fb2011-10-19 14:13:40 -070055 StackIndirectReferenceTable* GetLink() const {
Elliott Hughes68e76522011-10-05 13:22:16 -070056 return link_;
57 }
58
Brian Carlstrom40381fb2011-10-19 14:13:40 -070059 void SetLink(StackIndirectReferenceTable* sirt) {
60 DCHECK_NE(this, sirt);
61 link_ = sirt;
62 }
63
Andreas Gampebf6b92a2014-03-05 16:11:04 -080064 // Sets the number_of_references_ field for constructing tables out of raw memory. Warning: will
65 // not resize anything.
66 void SetNumberOfReferences(uint32_t num_references) {
67 number_of_references_ = num_references;
68 }
69
Ian Rogersef7d42f2014-01-06 12:55:46 -080070 mirror::Object* GetReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070071 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 return references_[i].AsMirrorPtr();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070073 }
74
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 void SetReference(size_t i, mirror::Object* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070076 DCHECK_LT(i, number_of_references_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 references_[i].Assign(object);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070078 }
79
Ian Rogersef7d42f2014-01-06 12:55:46 -080080 bool Contains(StackReference<mirror::Object>* sirt_entry) const {
Brian Carlstrom40381fb2011-10-19 14:13:40 -070081 // A SIRT should always contain something. One created by the
82 // jni_compiler should have a jobject/jclass as a native method is
83 // passed in a this pointer or a class
84 DCHECK_GT(number_of_references_, 0U);
85 return ((&references_[0] <= sirt_entry)
86 && (sirt_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -070087 }
88
89 // Offset of length within SIRT, used by generated code
Andreas Gampebf6b92a2014-03-05 16:11:04 -080090 static uint32_t NumberOfReferencesOffset() {
Elliott Hughes68e76522011-10-05 13:22:16 -070091 return OFFSETOF_MEMBER(StackIndirectReferenceTable, number_of_references_);
92 }
93
94 // Offset of link within SIRT, used by generated code
95 static size_t LinkOffset() {
96 return OFFSETOF_MEMBER(StackIndirectReferenceTable, link_);
97 }
98
Elliott Hughesff17f1f2012-01-24 18:12:29 -080099 private:
Elliott Hughes68e76522011-10-05 13:22:16 -0700100 StackIndirectReferenceTable() {}
101
Elliott Hughes68e76522011-10-05 13:22:16 -0700102 StackIndirectReferenceTable* link_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800103 uint32_t number_of_references_;
Shih-wei Liaoa8a9c342012-03-03 22:35:16 -0800104
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700105 // number_of_references_ are available if this is allocated and filled in by jni_compiler.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800106 StackReference<mirror::Object> references_[1];
Elliott Hughes68e76522011-10-05 13:22:16 -0700107
108 DISALLOW_COPY_AND_ASSIGN(StackIndirectReferenceTable);
109};
110
Elliott Hughes68e76522011-10-05 13:22:16 -0700111} // namespace art
112
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700113#endif // ART_RUNTIME_STACK_INDIRECT_REFERENCE_TABLE_H_