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/mutex.h"
27 #include "gc_root.h"
28 #include "obj_ptr.h"
29 #include "object_callbacks.h"
30 
31 namespace art {
32 namespace mirror {
33 class Object;
34 }  // namespace mirror
35 
36 // Maintain a table of references.  Used for JNI monitor references and
37 // JNI pinned array references.
38 //
39 // None of the functions are synchronized.
40 class ReferenceTable {
41  public:
42   ReferenceTable(const char* name, size_t initial_size, size_t max_size);
43   ~ReferenceTable();
44 
45   void Add(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
46 
47   void Remove(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
48 
49   size_t Size() const;
50 
51   void Dump(std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_);
52 
53   void VisitRoots(RootVisitor* visitor, const RootInfo& root_info)
54       REQUIRES_SHARED(Locks::mutator_lock_);
55 
56  private:
57   typedef std::vector<GcRoot<mirror::Object>,
58                       TrackingAllocator<GcRoot<mirror::Object>, kAllocatorTagReferenceTable>> Table;
59   static void Dump(std::ostream& os, Table& entries)
60       REQUIRES_SHARED(Locks::mutator_lock_);
61   friend class IndirectReferenceTable;  // For Dump.
62 
63   std::string name_;
64   Table entries_;
65   size_t max_size_;
66 };
67 
68 }  // namespace art
69 
70 #endif  // ART_RUNTIME_REFERENCE_TABLE_H_
71