blob: 638894462b205ff034861f28cc19023df0427c00 [file] [log] [blame]
Elliott Hughes11e45072011-08-16 17:40:46 -07001/*
2 * Copyright (C) 2008 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_REFERENCE_TABLE_H_
18#define ART_RUNTIME_REFERENCE_TABLE_H_
Elliott Hughes11e45072011-08-16 17:40:46 -070019
20#include <cstddef>
21#include <iosfwd>
22#include <string>
23#include <vector>
24
Mathieu Chartierbad02672014-08-25 13:08:22 -070025#include "base/allocator.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080026#include "base/locks.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070027#include "gc_root.h"
Mathieu Chartierc4f39252016-10-05 18:32:08 -070028#include "obj_ptr.h"
Elliott Hughes410c0c82011-09-01 17:58:25 -070029
Elliott Hughes11e45072011-08-16 17:40:46 -070030namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031namespace mirror {
Elliott Hughes11e45072011-08-16 17:40:46 -070032class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033} // namespace mirror
Elliott Hughes11e45072011-08-16 17:40:46 -070034
Elliott Hughes6c1a3942011-08-17 15:00:06 -070035// Maintain a table of references. Used for JNI monitor references and
36// JNI pinned array references.
Carl Shapiro5b1982d2011-08-16 18:35:19 -070037//
38// None of the functions are synchronized.
Elliott Hughes11e45072011-08-16 17:40:46 -070039class ReferenceTable {
40 public:
41 ReferenceTable(const char* name, size_t initial_size, size_t max_size);
Elliott Hughesc1674ed2011-08-25 18:09:09 -070042 ~ReferenceTable();
Elliott Hughes11e45072011-08-16 17:40:46 -070043
Mathieu Chartierc4f39252016-10-05 18:32:08 -070044 void Add(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes11e45072011-08-16 17:40:46 -070045
Mathieu Chartierc4f39252016-10-05 18:32:08 -070046 void Remove(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes11e45072011-08-16 17:40:46 -070047
48 size_t Size() const;
49
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -070050 void Dump(std::ostream& os)
51 REQUIRES_SHARED(Locks::mutator_lock_)
52 REQUIRES(!Locks::alloc_tracker_lock_);
Elliott Hughes11e45072011-08-16 17:40:46 -070053
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070054 void VisitRoots(RootVisitor* visitor, const RootInfo& root_info)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070055 REQUIRES_SHARED(Locks::mutator_lock_);
Elliott Hughes410c0c82011-09-01 17:58:25 -070056
Elliott Hughes11e45072011-08-16 17:40:46 -070057 private:
Mathieu Chartierbad02672014-08-25 13:08:22 -070058 typedef std::vector<GcRoot<mirror::Object>,
59 TrackingAllocator<GcRoot<mirror::Object>, kAllocatorTagReferenceTable>> Table;
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070060 static void Dump(std::ostream& os, Table& entries)
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -070061 REQUIRES_SHARED(Locks::mutator_lock_)
62 REQUIRES(!Locks::alloc_tracker_lock_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063 friend class IndirectReferenceTable; // For Dump.
Elliott Hughes6c1a3942011-08-17 15:00:06 -070064
Elliott Hughes11e45072011-08-16 17:40:46 -070065 std::string name_;
Elliott Hughes410c0c82011-09-01 17:58:25 -070066 Table entries_;
Elliott Hughes11e45072011-08-16 17:40:46 -070067 size_t max_size_;
68};
69
70} // namespace art
71
Brian Carlstromfc0e3212013-07-17 14:40:12 -070072#endif // ART_RUNTIME_REFERENCE_TABLE_H_