1 /*
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 
17 #ifndef ART_RUNTIME_REFERENCE_TABLE_H_
18 #define ART_RUNTIME_REFERENCE_TABLE_H_
19 
20 #include <cstddef>
21 #include <iosfwd>
22 #include <string>
23 #include <vector>
24 
25 #include "base/allocator.h"
26 #include "base/locks.h"
27 #include "base/macros.h"
28 #include "gc_root.h"
29 #include "obj_ptr.h"
30 
31 namespace art HIDDEN {
32 namespace jni {
33 class LocalReferenceTable;
34 }  // namespace jni
35 namespace mirror {
36 class Object;
37 }  // namespace mirror
38 
39 // Maintain a table of references.  Used for JNI monitor references and
40 // JNI pinned array references.
41 //
42 // None of the functions are synchronized.
43 class ReferenceTable {
44  public:
45   ReferenceTable(const char* name, size_t initial_size, size_t max_size);
46   ~ReferenceTable();
47 
48   void Add(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
49 
50   void Remove(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
51 
52   size_t Size() const;
53 
54   void Dump(std::ostream& os)
55       REQUIRES_SHARED(Locks::mutator_lock_)
56       REQUIRES(!Locks::alloc_tracker_lock_);
57 
58   EXPORT void VisitRoots(RootVisitor* visitor, const RootInfo& root_info)
59       REQUIRES_SHARED(Locks::mutator_lock_);
60 
61  private:
62   using Table = std::vector<GcRoot<mirror::Object>,
63                             TrackingAllocator<GcRoot<mirror::Object>, kAllocatorTagReferenceTable>>;
64   static void Dump(std::ostream& os, Table& entries)
65       REQUIRES_SHARED(Locks::mutator_lock_)
66       REQUIRES(!Locks::alloc_tracker_lock_);
67   friend class IndirectReferenceTable;  // For Dump.
68   friend class jni::LocalReferenceTable;  // For Dump.
69 
70   std::string name_;
71   Table entries_;
72   size_t max_size_;
73 };
74 
75 }  // namespace art
76 
77 #endif  // ART_RUNTIME_REFERENCE_TABLE_H_
78