1 /*
 2  * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef SHARE_VM_PRIMS_TAGMAPTABLE_HPP
26 #define SHARE_VM_PRIMS_TAGMAPTABLE_HPP
27 
28 #include "gc/shared/collectedHeap.hpp"
29 #include "memory/allocation.hpp"
30 #include "oops/weakHandle.hpp"
31 #include "utilities/resizeableResourceHash.hpp"
32 
33 class JvmtiEnv;
34 class JvmtiTagMapKeyClosure;
35 
36 // The oop is needed for lookup rather than creating a WeakHandle during
37 // lookup because the HeapWalker may walk soon to be dead objects and
38 // creating a WeakHandle for an otherwise dead object makes G1 unhappy.
39 //
40 // This class is the Key type for inserting in ResizeableResourceHashTable
41 // Its get_hash() and equals() methods are also used for getting the hash
42 // value of a Key and comparing two Keys, respectively.
43 class JvmtiTagMapKey : public CHeapObj<mtServiceability> {
44   WeakHandle _wh;
45   oop _obj; // temporarily hold obj while searching
46  public:
47   JvmtiTagMapKey(oop obj);
48   JvmtiTagMapKey(const JvmtiTagMapKey& src);
49   JvmtiTagMapKey& operator=(const JvmtiTagMapKey&) = delete;
50 
51   oop object() const;
52   oop object_no_keepalive() const;
53   void release_weak_handle();
54 
55   static unsigned get_hash(const JvmtiTagMapKey& entry);
56   static bool equals(const JvmtiTagMapKey& lhs, const JvmtiTagMapKey& rhs) {
57     oop lhs_obj = lhs._obj != nullptr ? lhs._obj : lhs.object_no_keepalive();
58     oop rhs_obj = rhs._obj != nullptr ? rhs._obj : rhs.object_no_keepalive();
59     return lhs_obj == rhs_obj;
60   }
61 };
62 
63 typedef
64 ResizeableResourceHashtable <JvmtiTagMapKey, jlong,
65                               AnyObj::C_HEAP, mtServiceability,
66                               JvmtiTagMapKey::get_hash,
67                               JvmtiTagMapKey::equals> ResizableResourceHT;
68 
69 class JvmtiTagMapTable : public CHeapObj<mtServiceability> {
70  private:
71   ResizableResourceHT _table;
72 
73  public:
74   JvmtiTagMapTable();
75   ~JvmtiTagMapTable();
76 
77   jlong find(oop obj);
78   void add(oop obj, jlong tag);
79 
80   void remove(oop obj);
81 
82   // iterate over all entries in the hashmap
83   void entry_iterate(JvmtiTagMapKeyClosure* closure);
84 
85   bool is_empty() const { return _table.number_of_entries() == 0; }
86 
87   // Cleanup cleared entries and store dead object tags in objects array
88   void remove_dead_entries(GrowableArray<jlong>* objects);
89   void clear();
90 };
91 
92 // A supporting class for iterating over all entries in Hashmap
93 class JvmtiTagMapKeyClosure {
94  public:
95   virtual bool do_entry(JvmtiTagMapKey& key, jlong& value) = 0;
96 };
97 
98 #endif // SHARE_VM_PRIMS_TAGMAPTABLE_HPP