1 /*
   2  * Copyright (c) 2018, 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_MEMORY_HEAPSHARED_HPP
  26 #define SHARE_VM_MEMORY_HEAPSHARED_HPP
  27 
  28 #include "classfile/systemDictionary.hpp"
  29 #include "memory/universe.hpp"
  30 #include "oops/objArrayKlass.hpp"
  31 #include "oops/oop.hpp"
  32 #include "oops/typeArrayKlass.hpp"
  33 #include "utilities/bitMap.hpp"
  34 #include "utilities/growableArray.hpp"
  35 
  36 #if INCLUDE_CDS_JAVA_HEAP
  37 // A dump time sub-graph info for Klass _k. It includes the entry points
  38 // (static fields in _k's mirror) of the archived sub-graphs reachable
  39 // from _k's mirror. It also contains a list of Klasses of the objects
  40 // within the sub-graphs.
  41 class KlassSubGraphInfo: public CHeapObj<mtClass> {
  42  private:
  43   KlassSubGraphInfo* _next;
  44   // The class that contains the static field(s) as the entry point(s)
  45   // of archived object sub-graph(s).
  46   Klass* _k;
  47   // A list of classes need to be loaded and initialized before the archived
  48   // object sub-graphs can be accessed at runtime.
  49   GrowableArray<Klass*>* _subgraph_object_klasses;
  50   // A list of _k's static fields as the entry points of archived sub-graphs.
  51   // For each entry field, it is a pair of field_offset and field_value.
  52   GrowableArray<juint>*  _subgraph_entry_fields;
  53 
  54  public:
  55   KlassSubGraphInfo(Klass* k, KlassSubGraphInfo* next) :
  56     _next(next), _k(k),  _subgraph_object_klasses(NULL),
  57     _subgraph_entry_fields(NULL) {}
  58   ~KlassSubGraphInfo() {
  59     if (_subgraph_object_klasses != NULL) {
  60       delete _subgraph_object_klasses;
  61     }
  62     if (_subgraph_entry_fields != NULL) {
  63       delete _subgraph_entry_fields;
  64     }
  65   };
  66 
  67   KlassSubGraphInfo* next() { return _next; }
  68   Klass* klass()            { return _k; }
  69   GrowableArray<Klass*>* subgraph_object_klasses() {
  70     return _subgraph_object_klasses;
  71   }
  72   GrowableArray<juint>*  subgraph_entry_fields() {
  73     return _subgraph_entry_fields;
  74   }
  75   void add_subgraph_entry_field(int static_field_offset, oop v);
  76   void add_subgraph_object_klass(Klass *orig_k, Klass *relocated_k);
  77 };
  78 
  79 // An archived record of object sub-graphs reachable from static
  80 // fields within _k's mirror. The record is reloaded from the archive
  81 // at runtime.
  82 class ArchivedKlassSubGraphInfoRecord {
  83  private:
  84   ArchivedKlassSubGraphInfoRecord* _next;
  85   Klass* _k;
  86 
  87   // contains pairs of field offset and value for each subgraph entry field
  88   Array<juint>* _entry_field_records;
  89 
  90   // klasses of objects in archived sub-graphs referenced from the entry points
  91   // (static fields) in the containing class
  92   Array<Klass*>* _subgraph_klasses;
  93  public:
  94   ArchivedKlassSubGraphInfoRecord() :
  95     _next(NULL), _k(NULL), _entry_field_records(NULL), _subgraph_klasses(NULL) {}
  96   void init(KlassSubGraphInfo* info);
  97   Klass* klass() { return _k; }
  98   ArchivedKlassSubGraphInfoRecord* next() { return _next; }
  99   void set_next(ArchivedKlassSubGraphInfoRecord* next) { _next = next; }
 100   Array<juint>*  entry_field_records() { return _entry_field_records; }
 101   Array<Klass*>* subgraph_klasses() { return _subgraph_klasses; }
 102 };
 103 #endif // INCLUDE_CDS_JAVA_HEAP
 104 
 105 class HeapShared: AllStatic {
 106  private:
 107 #if INCLUDE_CDS_JAVA_HEAP
 108   // This is a list of subgraph infos built at dump time while
 109   // archiving object subgraphs.
 110   static KlassSubGraphInfo* _subgraph_info_list;
 111 
 112   // Contains a list of ArchivedKlassSubGraphInfoRecords that is stored
 113   // in the archive file and reloaded at runtime.
 114   static int _num_archived_subgraph_info_records;
 115   static Array<ArchivedKlassSubGraphInfoRecord>* _archived_subgraph_info_records;
 116 
 117   // Archive object sub-graph starting from the given static field
 118   // in Klass k's mirror.
 119   static void archive_reachable_objects_from_static_field(
 120     Klass* k, int field_ofset, BasicType field_type, TRAPS);
 121 
 122   static KlassSubGraphInfo* find_subgraph_info(Klass *k);
 123   static KlassSubGraphInfo* get_subgraph_info(Klass *k);
 124   static int num_of_subgraph_infos();
 125 
 126   static size_t build_archived_subgraph_info_records(int num_records);
 127 
 128   // Used by decode_with_archived_oop_encoding_mode
 129   static address _narrow_oop_base;
 130   static int     _narrow_oop_shift;
 131 
 132 #endif // INCLUDE_CDS_JAVA_HEAP
 133  public:
 134   static char* read_archived_subgraph_infos(char* buffer) NOT_CDS_JAVA_HEAP_RETURN_(buffer);
 135   static void write_archived_subgraph_infos() NOT_CDS_JAVA_HEAP_RETURN;
 136   static void initialize_from_archived_subgraph(Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
 137 
 138   // NarrowOops stored in the CDS archive may use a different encoding scheme
 139   // than Universe::narrow_oop_{base,shift} -- see FileMapInfo::map_heap_regions_impl.
 140   // To decode them, do not use CompressedOops::decode_not_null. Use this
 141   // function instead.
 142   inline static oop decode_with_archived_oop_encoding_mode(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(NULL);
 143 
 144   static void init_narrow_oop_decoding(address base, int shift) NOT_CDS_JAVA_HEAP_RETURN;
 145 
 146   static void patch_archived_heap_embedded_pointers(MemRegion mem, address  oopmap,
 147                                                     size_t oopmap_in_bits) NOT_CDS_JAVA_HEAP_RETURN;
 148 
 149   static void init_archivable_static_fields(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
 150   static void archive_module_graph_objects(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
 151 
 152 #if INCLUDE_CDS_JAVA_HEAP
 153   static ResourceBitMap calculate_oopmap(MemRegion region);
 154 #endif
 155 };
 156 #endif // SHARE_VM_MEMORY_HEAPSHARED_HPP