1 /*
2  * Copyright (c) 2020, 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 "oops/weakHandle.hpp"
29 #include "utilities/hashtable.hpp"
30 
31 class JvmtiEnv;
32 
33 // Hashtable to record oops used for JvmtiTagMap
34 class JvmtiTagMapEntryClosure;
35 
36 class JvmtiTagMapEntry : public HashtableEntry<WeakHandle, mtServiceability> {
37   jlong _tag;                           // the tag
38  public:
next() const39   JvmtiTagMapEntry* next() const {
40     return (JvmtiTagMapEntry*)HashtableEntry<WeakHandle, mtServiceability>::next();
41   }
42 
next_addr()43   JvmtiTagMapEntry** next_addr() {
44     return (JvmtiTagMapEntry**)HashtableEntry<WeakHandle, mtServiceability>::next_addr();
45   }
46 
47   oop object();
48   oop object_no_keepalive();
tag() const49   jlong tag() const       { return _tag; }
set_tag(jlong tag)50   void set_tag(jlong tag) { _tag = tag; }
51 };
52 
53 class JvmtiTagMapTable : public Hashtable<WeakHandle, mtServiceability> {
54   enum Constants {
55     _table_size  = 1007
56   };
57 
58 private:
bucket(int i)59   JvmtiTagMapEntry* bucket(int i) {
60     return (JvmtiTagMapEntry*) Hashtable<WeakHandle, mtServiceability>::bucket(i);
61   }
62 
bucket_addr(int i)63   JvmtiTagMapEntry** bucket_addr(int i) {
64     return (JvmtiTagMapEntry**) Hashtable<WeakHandle, mtServiceability>::bucket_addr(i);
65   }
66 
67   JvmtiTagMapEntry* new_entry(unsigned int hash, WeakHandle w, jlong tag);
68   void free_entry(JvmtiTagMapEntry* entry);
69 
70   unsigned int compute_hash(oop obj);
71 
72   JvmtiTagMapEntry* find(int index, unsigned int hash, oop obj);
73 
74   void resize_if_needed();
75 
76 public:
77   JvmtiTagMapTable();
78   ~JvmtiTagMapTable();
79 
80   JvmtiTagMapEntry* find(oop obj);
81   JvmtiTagMapEntry* add(oop obj, jlong tag);
82 
83   void remove(oop obj);
84 
85   // iterate over all entries in the hashmap
86   void entry_iterate(JvmtiTagMapEntryClosure* closure);
87 
is_empty() const88   bool is_empty() const { return number_of_entries() == 0; }
89 
90   // Cleanup cleared entries and post
91   void remove_dead_entries(JvmtiEnv* env, bool post_object_free);
92   void rehash();
93   void clear();
94 };
95 
96 // A supporting class for iterating over all entries in Hashmap
97 class JvmtiTagMapEntryClosure {
98  public:
99   virtual void do_entry(JvmtiTagMapEntry* entry) = 0;
100 };
101 
102 #endif // SHARE_VM_PRIMS_TAGMAPTABLE_HPP
103