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/growableArray.hpp"
  34 
  35 #if INCLUDE_CDS_JAVA_HEAP
  36 // A dump time sub-graph info for Klass _k. It includes the entry points
  37 // (static fields in _k's mirror) of the archived sub-graphs reachable
  38 // from _k's mirror. It also contains a list of Klasses of the objects
  39 // within the sub-graphs.
  40 class KlassSubGraphInfo: public CHeapObj<mtClass> {
  41  private:
  42   KlassSubGraphInfo* _next;
  43   // The class that contains the static field(s) as the entry point(s)
  44   // of archived object sub-graph(s).
  45   Klass* _k;
  46   // A list of classes need to be loaded and initialized before the archived
  47   // object sub-graphs can be accessed at runtime.
  48   GrowableArray<Klass*>* _subgraph_object_klasses;
  49   // A list of _k's static fields as the entry points of archived sub-graphs.
  50   // For each entry field, it is a pair of field_offset and field_value.
  51   GrowableArray<juint>*  _subgraph_entry_fields;
  52 
  53  public:
  54   KlassSubGraphInfo(Klass* k, KlassSubGraphInfo* next) :
  55     _next(next), _k(k),  _subgraph_object_klasses(NULL),
  56     _subgraph_entry_fields(NULL) {}
  57   ~KlassSubGraphInfo() {
  58     if (_subgraph_object_klasses != NULL) {
  59       delete _subgraph_object_klasses;
  60     }
  61     if (_subgraph_entry_fields != NULL) {
  62       delete _subgraph_entry_fields;
  63     }
  64   };
  65 
  66   KlassSubGraphInfo* next() { return _next; }
  67   Klass* klass()            { return _k; }
  68   GrowableArray<Klass*>* subgraph_object_klasses() {
  69     return _subgraph_object_klasses;
  70   }
  71   GrowableArray<juint>*  subgraph_entry_fields() {
  72     return _subgraph_entry_fields;
  73   }
  74   void add_subgraph_entry_field(int static_field_offset, oop v);
  75   void add_subgraph_object_klass(Klass *orig_k, Klass *relocated_k);
  76 };
  77 
  78 // An archived record of object sub-graphs reachable from static
  79 // fields within _k's mirror. The record is reloaded from the archive
  80 // at runtime.
  81 class ArchivedKlassSubGraphInfoRecord {
  82  private:
  83   ArchivedKlassSubGraphInfoRecord* _next;
  84   Klass* _k;
  85 
  86   // contains pairs of field offset and value for each subgraph entry field
  87   Array<juint>* _entry_field_records;
  88 
  89   // klasses of objects in archived sub-graphs referenced from the entry points
  90   // (static fields) in the containing class
  91   Array<Klass*>* _subgraph_klasses;
  92  public:
  93   ArchivedKlassSubGraphInfoRecord() :
  94     _next(NULL), _k(NULL), _entry_field_records(NULL), _subgraph_klasses(NULL) {}
  95   void init(KlassSubGraphInfo* info);
  96   Klass* klass() { return _k; }
  97   ArchivedKlassSubGraphInfoRecord* next() { return _next; }
  98   void set_next(ArchivedKlassSubGraphInfoRecord* next) { _next = next; }
  99   Array<juint>*  entry_field_records() { return _entry_field_records; }
 100   Array<Klass*>* subgraph_klasses() { return _subgraph_klasses; }
 101 };
 102 #endif // INCLUDE_CDS_JAVA_HEAP
 103 
 104 class HeapShared: AllStatic {
 105  private:
 106 #if INCLUDE_CDS_JAVA_HEAP
 107   // This is a list of subgraph infos built at dump time while
 108   // archiving object subgraphs.
 109   static KlassSubGraphInfo* _subgraph_info_list;
 110 
 111   // Contains a list of ArchivedKlassSubGraphInfoRecords that is stored
 112   // in the archive file and reloaded at runtime.
 113   static int _num_archived_subgraph_info_records;
 114   static Array<ArchivedKlassSubGraphInfoRecord>* _archived_subgraph_info_records;
 115 
 116   // Archive object sub-graph starting from the given static field
 117   // in Klass k's mirror.
 118   static void archive_reachable_objects_from_static_field(
 119     Klass* k, int field_ofset, BasicType field_type, TRAPS);
 120 
 121   static KlassSubGraphInfo* find_subgraph_info(Klass *k);
 122   static KlassSubGraphInfo* get_subgraph_info(Klass *k);
 123   static int num_of_subgraph_infos();
 124 
 125   static size_t build_archived_subgraph_info_records(int num_records);
 126 #endif // INCLUDE_CDS_JAVA_HEAP
 127  public:
 128   static char* read_archived_subgraph_infos(char* buffer) NOT_CDS_JAVA_HEAP_RETURN_(buffer);
 129   static void write_archived_subgraph_infos() NOT_CDS_JAVA_HEAP_RETURN;
 130   static void initialize_from_archived_subgraph(Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
 131 
 132   static void archive_module_graph_objects(Thread* THREAD) NOT_CDS_JAVA_HEAP_RETURN;
 133 };
 134 #endif // SHARE_VM_MEMORY_HEAPSHARED_HPP