1 /*
  2  * Copyright (c) 1999, 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_CI_CIOBJECTFACTORY_HPP
 26 #define SHARE_CI_CIOBJECTFACTORY_HPP
 27 
 28 #include "ci/ciClassList.hpp"
 29 #include "ci/ciObject.hpp"
 30 #include "utilities/growableArray.hpp"
 31 #include "utilities/vmEnums.hpp"
 32 
 33 // ciObjectFactory
 34 //
 35 // This class handles requests for the creation of new instances
 36 // of ciObject and its subclasses.  It contains a caching mechanism
 37 // which ensures that for each oop, at most one ciObject is created.
 38 // This invariant allows efficient implementation of ciObject.
 39 class ciObjectFactory : public ArenaObj {
 40   friend class VMStructs;
 41   friend class ciEnv;
 42 
 43 private:
 44   static volatile bool _initialized;
 45   static GrowableArray<ciMetadata*>* _shared_ci_metadata;
 46   static ciSymbol*                 _shared_ci_symbols[];
 47   static int                       _shared_ident_limit;
 48 
 49   Arena*                           _arena;
 50   GrowableArray<ciMetadata*>       _ci_metadata;
 51   GrowableArray<ciMethod*>         _unloaded_methods;
 52   GrowableArray<ciKlass*>          _unloaded_klasses;
 53   GrowableArray<ciInstance*>       _unloaded_instances;
 54   GrowableArray<ciReturnAddress*>  _return_addresses;
 55   GrowableArray<ciSymbol*>         _symbols;  // keep list of symbols created
 56   int                              _next_ident;
 57 
 58 public:
 59   struct NonPermObject : public ArenaObj {
 60     ciObject*      _object;
 61     NonPermObject* _next;
 62 
 63     inline NonPermObject(NonPermObject* &bucket, oop key, ciObject* object);
 64     ciObject*     object()  { return _object; }
 65     NonPermObject* &next()  { return _next; }
 66   };
 67 private:
 68   enum { NON_PERM_BUCKETS = 61 };
 69   NonPermObject* _non_perm_bucket[NON_PERM_BUCKETS];
 70   int _non_perm_count;
 71 
 72   static int metadata_compare(Metadata* const& key, ciMetadata* const& elt);
 73 
 74   ciObject* create_new_object(oop o);
 75   ciMetadata* create_new_metadata(Metadata* o);
 76 
 77   static bool is_equal(NonPermObject* p, oop key) {
 78     return p->object()->get_oop() == key;
 79   }
 80 
 81   NonPermObject* &find_non_perm(oop key);
 82   void insert_non_perm(NonPermObject* &where, oop key, ciObject* obj);
 83 
 84   void init_ident_of(ciBaseObject* obj);
 85 
 86   Arena* arena() { return _arena; }
 87 
 88   void print_contents_impl();
 89 
 90   ciInstance* get_unloaded_instance(ciInstanceKlass* klass);
 91 
 92 public:
 93   static bool is_initialized() { return _initialized; }
 94 
 95   static void initialize();
 96   void init_shared_objects();
 97   void remove_symbols();
 98 
 99   ciObjectFactory(Arena* arena, int expected_size);
100 
101   // Get the ciObject corresponding to some oop.
102   ciObject* get(oop key);
103   ciMetadata* get_metadata(Metadata* key);
104   ciMetadata* cached_metadata(Metadata* key);
105   ciSymbol* get_symbol(Symbol* key);
106 
107   // Get the ciSymbol corresponding to one of the vmSymbols.
108   static ciSymbol* vm_symbol_at(vmSymbolID index);
109 
110   // Get the ciMethod representing an unloaded/unfound method.
111   ciMethod* get_unloaded_method(ciInstanceKlass* holder,
112                                 ciSymbol*        name,
113                                 ciSymbol*        signature,
114                                 ciInstanceKlass* accessor);
115 
116   // Get a ciKlass representing an unloaded klass.
117   ciKlass* get_unloaded_klass(ciKlass* accessing_klass,
118                               ciSymbol* name,
119                               bool create_if_not_found);
120 
121   // Get a ciInstance representing an unresolved klass mirror.
122   ciInstance* get_unloaded_klass_mirror(ciKlass* type);
123 
124   // Get a ciInstance representing an unresolved method handle constant.
125   ciInstance* get_unloaded_method_handle_constant(ciKlass*  holder,
126                                                   ciSymbol* name,
127                                                   ciSymbol* signature,
128                                                   int       ref_kind);
129 
130   // Get a ciInstance representing an unresolved method type constant.
131   ciInstance* get_unloaded_method_type_constant(ciSymbol* signature);
132 
133 
134   ciInstance* get_unloaded_object_constant();
135 
136   // Get the ciMethodData representing the methodData for a method
137   // with none.
138   ciMethodData* get_empty_methodData();
139 
140   ciReturnAddress* get_return_address(int bci);
141 
142   GrowableArray<ciMetadata*>* get_ci_metadata() { return &_ci_metadata; }
143   // RedefineClasses support
144   void metadata_do(MetadataClosure* f);
145 
146   void print_contents();
147   void print();
148 };
149 
150 #endif // SHARE_CI_CIOBJECTFACTORY_HPP