1 /*
2 * Copyright (c) 2020, 2023, 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/resizableHashTable.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 assert(entry._obj != nullptr, "must lookup obj to hash");
57 return (unsigned)entry._obj->identity_hash();
58 }
59
60 static bool equals(const JvmtiTagMapKey& lhs, const JvmtiTagMapKey& rhs) {
61 oop lhs_obj = lhs._obj != nullptr ? lhs._obj : lhs.object_no_keepalive();
62 oop rhs_obj = rhs._obj != nullptr ? rhs._obj : rhs.object_no_keepalive();
63 return lhs_obj == rhs_obj;
64 }
65 };
66
67 typedef
68 ResizeableHashTable <JvmtiTagMapKey, jlong,
69 AnyObj::C_HEAP, mtServiceability,
70 JvmtiTagMapKey::get_hash,
71 JvmtiTagMapKey::equals> ResizableHT;
72
73 class JvmtiTagMapTable : public CHeapObj<mtServiceability> {
74 private:
75 ResizableHT _table;
76
77 public:
78 JvmtiTagMapTable();
79 ~JvmtiTagMapTable();
80
81 jlong find(oop obj);
82 void add(oop obj, jlong tag);
83
84 void remove(oop obj);
85
86 // iterate over all entries in the hashmap
87 void entry_iterate(JvmtiTagMapKeyClosure* closure);
88
89 bool is_empty() const { return _table.number_of_entries() == 0; }
90
91 // Cleanup cleared entries and store dead object tags in objects array
92 void remove_dead_entries(GrowableArray<jlong>* objects);
93 void clear();
94 };
95
96 // A supporting class for iterating over all entries in Hashmap
97 class JvmtiTagMapKeyClosure {
98 public:
99 virtual bool do_entry(JvmtiTagMapKey& key, jlong& value) = 0;
100 };
101
102 #endif // SHARE_VM_PRIMS_TAGMAPTABLE_HPP