1 /*
 2  * Copyright (c) 2024, 2025, 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_CDS_FINALIMAGERECIPES_HPP
26 #define SHARE_CDS_FINALIMAGERECIPES_HPP
27 
28 #include "oops/oopsHierarchy.hpp"
29 #include "utilities/exceptions.hpp"
30 
31 class InstanceKlass;
32 class Klass;
33 
34 template <typename T> class GrowableArray;
35 template <typename T> class Array;
36 
37 // This class is used for transferring information from the AOTConfiguration file (aka the "preimage")
38 // to the JVM that creates the AOTCache (aka the "final image").
39 //   - The recipes are recorded when CDSConfig::is_dumping_preimage_static_archive() is true.
40 //   - The recipes are applied when CDSConfig::is_dumping_final_static_archive() is true.
41 // The following information are recorded:
42 //   - The list of all classes that are stored in the AOTConfiguration file.
43 //   - The list of all classes that require AOT resolution of invokedynamic call sites.
44 class FinalImageRecipes {
45   static constexpr int HAS_CLASS            = 0x1;
46   static constexpr int HAS_FIELD_AND_METHOD = 0x2;
47   static constexpr int HAS_INDY             = 0x4;
48 
49   // A list of all the archived classes from the preimage. We want to transfer all of these
50   // into the final image.
51   Array<Klass*>* _all_klasses;
52 
53   // For each klass k _all_klasses->at(i), _cp_recipes->at(i) lists all the {klass,field,method,indy}
54   // cp indices that were resolved for k during the training run.
55   Array<Array<int>*>* _cp_recipes;
56   Array<int>* _cp_flags;
57 
58   FinalImageRecipes() : _all_klasses(nullptr), _cp_recipes(nullptr), _cp_flags(nullptr) {}
59 
60   void* operator new(size_t size) throw();
61 
62   // Called when dumping preimage
63   void record_all_classes();
64   void record_recipes_for_constantpool();
65 
66   // Called when dumping final image
67   void apply_recipes_impl(TRAPS);
68   void load_all_classes(TRAPS);
69   void apply_recipes_for_constantpool(JavaThread* current);
70 
71 public:
72   static void serialize(SerializeClosure* soc);
73 
74   // Called when dumping preimage
75   static void record_recipes();
76 
77   // Called when dumping final image
78   static void apply_recipes(TRAPS);
79 };
80 
81 #endif // SHARE_CDS_FINALIMAGERECIPES_HPP