1 /*
   2  * Copyright (c) 1999, 2007, 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 // ciObjectFactory
  26 //
  27 // This class handles requests for the creation of new instances
  28 // of ciObject and its subclasses.  It contains a caching mechanism
  29 // which ensures that for each oop, at most one ciObject is created.
  30 // This invariant allows efficient implementation of ciObject.
  31 class ciObjectFactory : public ResourceObj {
  32 private:
  33   static volatile bool _initialized;
  34   static GrowableArray<ciObject*>* _shared_ci_objects;
  35   static ciSymbol*                 _shared_ci_symbols[];
  36   static int                       _shared_ident_limit;
  37 
  38   Arena*                    _arena;
  39   GrowableArray<ciObject*>* _ci_objects;
  40   GrowableArray<ciMethod*>* _unloaded_methods;
  41   GrowableArray<ciKlass*>* _unloaded_klasses;
  42   GrowableArray<ciInstance*>* _unloaded_instances;
  43   GrowableArray<ciReturnAddress*>* _return_addresses;
  44   int                       _next_ident;
  45 
  46 public:
  47   struct NonPermObject : public ResourceObj {
  48     ciObject*      _object;
  49     NonPermObject* _next;
  50 
  51     inline NonPermObject(NonPermObject* &bucket, oop key, ciObject* object);
  52     ciObject*     object()  { return _object; }
  53     NonPermObject* &next()  { return _next; }
  54   };
  55 private:
  56   enum { NON_PERM_BUCKETS = 61 };
  57   NonPermObject* _non_perm_bucket[NON_PERM_BUCKETS];
  58   int _non_perm_count;
  59 
  60   int find(oop key, GrowableArray<ciObject*>* objects);
  61   bool is_found_at(int index, oop key, GrowableArray<ciObject*>* objects);
  62   void insert(int index, ciObject* obj, GrowableArray<ciObject*>* objects);
  63   ciObject* create_new_object(oop o);
  64   static bool is_equal(NonPermObject* p, oop key) {
  65     return p->object()->get_oop() == key;
  66   }
  67 
  68   NonPermObject* &find_non_perm(oop key);
  69   void insert_non_perm(NonPermObject* &where, oop key, ciObject* obj);
  70 
  71   void init_ident_of(ciObject* obj);
  72 
  73   Arena* arena() { return _arena; }
  74 
  75   void print_contents_impl();
  76 
  77   ciInstance* get_unloaded_instance(ciInstanceKlass* klass);
  78 
  79 public:
  80   static bool is_initialized() { return _initialized; }
  81 
  82   static void initialize();
  83   void init_shared_objects();
  84 
  85   ciObjectFactory(Arena* arena, int expected_size);
  86 
  87 
  88   // Get the ciObject corresponding to some oop.
  89   ciObject* get(oop key);
  90 
  91   // Get the ciSymbol corresponding to one of the vmSymbols.
  92   static ciSymbol* vm_symbol_at(int index);
  93 
  94   // Get the ciMethod representing an unloaded/unfound method.
  95   ciMethod* get_unloaded_method(ciInstanceKlass* holder,
  96                                 ciSymbol*        name,
  97                                 ciSymbol*        signature);
  98 
  99   // Get a ciKlass representing an unloaded klass.
 100   ciKlass* get_unloaded_klass(ciKlass* accessing_klass,
 101                               ciSymbol* name,
 102                               bool create_if_not_found);
 103 
 104   // Get a ciInstance representing an unresolved klass mirror.
 105   ciInstance* get_unloaded_klass_mirror(ciKlass* type);
 106 
 107   // Get a ciInstance representing an unresolved method handle constant.
 108   ciInstance* get_unloaded_method_handle_constant(ciKlass*  holder,
 109                                                   ciSymbol* name,
 110                                                   ciSymbol* signature,
 111                                                   int       ref_kind);
 112 
 113   // Get a ciInstance representing an unresolved method type constant.
 114   ciInstance* get_unloaded_method_type_constant(ciSymbol* signature);
 115 
 116 
 117   // Get the ciMethodData representing the methodData for a method
 118   // with none.
 119   ciMethodData* get_empty_methodData();
 120 
 121   ciReturnAddress* get_return_address(int bci);
 122 
 123   void print_contents();
 124   void print();
 125 };