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 //
 44 // Valhalla: Keep just one tag for all equal value objects including heap allocated value objects.
 45 // We have to keep a strong reference to each unique value object with a non-zero tag.
 46 class JvmtiTagMapKey : public CHeapObj<mtServiceability> {
 47   // All equal value objects should have the same tag.
 48   // Keep value objects alive (1 copy for each "value") until their tags are removed.
 49   union {
 50     WeakHandle _wh;
 51     OopHandle _h; // for value objects (_is_weak == false)
 52   };
 53   bool _is_weak;
 54   oop _obj; // temporarily hold obj while searching
 55  public:
 56   JvmtiTagMapKey(oop obj);
 57   JvmtiTagMapKey(const JvmtiTagMapKey& src);
 58   JvmtiTagMapKey& operator=(const JvmtiTagMapKey&) = delete;
 59 
 60   oop object() const;
 61   oop object_no_keepalive() const;
 62   void release_handle();
 63 
 64   static unsigned get_hash(const JvmtiTagMapKey& entry);
 65   static bool equals(const JvmtiTagMapKey& lhs, const JvmtiTagMapKey& rhs);
 66 };
 67 
 68 typedef
 69 ResizeableResourceHashtable <JvmtiTagMapKey, jlong,
 70                               AnyObj::C_HEAP, mtServiceability,
 71                               JvmtiTagMapKey::get_hash,
 72                               JvmtiTagMapKey::equals> ResizableResourceHT;
 73 
 74 class JvmtiTagMapTable : public CHeapObj<mtServiceability> {
 75  private:
 76   ResizableResourceHT _table;
 77 
 78  public:
 79   JvmtiTagMapTable();
 80   ~JvmtiTagMapTable();
 81 
 82   jlong find(oop obj);
 83   void add(oop obj, jlong tag);
 84 
 85   void remove(oop obj);
 86 
 87   // iterate over all entries in the hashmap
 88   void entry_iterate(JvmtiTagMapKeyClosure* closure);
 89 
 90   bool is_empty() const { return _table.number_of_entries() == 0; }
 91 
 92   // Cleanup cleared entries and store dead object tags in objects array
 93   void remove_dead_entries(GrowableArray<jlong>* objects);
 94   void clear();
 95 };
 96 
 97 // A supporting class for iterating over all entries in Hashmap
 98 class JvmtiTagMapKeyClosure {
 99  public:
100   virtual bool do_entry(JvmtiTagMapKey& key, jlong& value) = 0;
101 };
102 
103 #endif // SHARE_VM_PRIMS_TAGMAPTABLE_HPP