1 /*
   2  * Copyright (c) 1997, 2010, 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 #include "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "memory/gcLocker.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "oops/symbolKlass.hpp"
  30 #include "oops/symbolOop.hpp"
  31 #include "runtime/handles.inline.hpp"
  32 
  33 symbolOop symbolKlass::allocate_symbol(u1* name, int len, TRAPS) {
  34   // Don't allow symbol oops to be created which cannot fit in a symbolOop.
  35   if (len > symbolOopDesc::max_length()) {
  36     THROW_MSG_0(vmSymbols::java_lang_InternalError(),
  37                 "name is too long to represent");
  38   }
  39   int size = symbolOopDesc::object_size(len);
  40   symbolKlassHandle h_k(THREAD, as_klassOop());
  41   symbolOop sym = (symbolOop)
  42     CollectedHeap::permanent_obj_allocate(h_k, size, CHECK_NULL);
  43   assert(!sym->is_parsable(), "not expecting parsability yet.");
  44   No_Safepoint_Verifier no_safepoint;
  45   sym->set_utf8_length(len);
  46   for (int i = 0; i < len; i++) {
  47     sym->byte_at_put(i, name[i]);
  48   }
  49   // Let the first emptySymbol be created and
  50   // ensure only one is ever created.
  51   assert(sym->is_parsable() || Universe::emptySymbol() == NULL,
  52          "should be parsable here.");
  53   return sym;
  54 }
  55 
  56 bool symbolKlass::allocate_symbols(int names_count, const char** names,
  57                                    int* lengths, symbolOop* sym_oops, TRAPS) {
  58   if (UseConcMarkSweepGC || UseParallelGC) {
  59     // Concurrent GC needs to mark all the allocated symbol oops after
  60     // the remark phase which isn't done below (except the first symbol oop).
  61     // So return false which will let the symbols be allocated one by one.
  62     // The parallel collector uses an object start array to find the
  63     // start of objects on a dirty card.  The object start array is not
  64     // updated for the start of each symbol so is not precise.  During
  65     // object array verification this causes a verification failure.
  66     // In a product build this causes extra searching for the start of
  67     // a symbol.  As with the concurrent collector a return of false will
  68     // cause each symbol to be allocated separately and in the case
  69     // of the parallel collector will cause the object
  70     // start array to be updated.
  71     return false;
  72   }
  73 
  74   assert(names_count > 0, "can't allocate 0 symbols");
  75 
  76   int total_size = 0;
  77   int i, sizes[SymbolTable::symbol_alloc_batch_size];
  78   for (i=0; i<names_count; i++) {
  79     int len = lengths[i];
  80     if (len > symbolOopDesc::max_length()) {
  81       return false;
  82     }
  83     int sz = symbolOopDesc::object_size(len);
  84     sizes[i] = sz * HeapWordSize;
  85     total_size += sz;
  86   }
  87   symbolKlassHandle h_k(THREAD, as_klassOop());
  88   HeapWord* base = Universe::heap()->permanent_mem_allocate(total_size);
  89   if (base == NULL) {
  90     return false;
  91   }
  92 
  93   // CAN'T take any safepoint during the initialization of the symbol oops !
  94   No_Safepoint_Verifier nosafepoint;
  95 
  96   klassOop sk = h_k();
  97   int pos = 0;
  98   for (i=0; i<names_count; i++) {
  99     symbolOop s = (symbolOop) (((char*)base) + pos);
 100     s->set_mark(markOopDesc::prototype());
 101     s->set_klass(sk);
 102     s->set_utf8_length(lengths[i]);
 103     const char* name = names[i];
 104     for (int j=0; j<lengths[i]; j++) {
 105       s->byte_at_put(j, name[j]);
 106     }
 107 
 108     assert(s->is_parsable(), "should be parsable here.");
 109 
 110     sym_oops[i] = s;
 111     pos += sizes[i];
 112   }
 113   return true;
 114 }
 115 
 116 klassOop symbolKlass::create_klass(TRAPS) {
 117   symbolKlass o;
 118   KlassHandle h_this_klass(THREAD, Universe::klassKlassObj());
 119   KlassHandle k = base_create_klass(h_this_klass, header_size(), o.vtbl_value(), CHECK_NULL);
 120   // Make sure size calculation is right
 121   assert(k()->size() == align_object_size(header_size()), "wrong size for object");
 122 //  java_lang_Class::create_mirror(k, CHECK_NULL); // Allocate mirror
 123   return k();
 124 }
 125 
 126 int symbolKlass::oop_size(oop obj) const {
 127   assert(obj->is_symbol(),"must be a symbol");
 128   symbolOop s = symbolOop(obj);
 129   int size = s->object_size();
 130   return size;
 131 }
 132 
 133 bool symbolKlass::oop_is_parsable(oop obj) const {
 134   assert(obj->is_symbol(),"must be a symbol");
 135   symbolOop s = symbolOop(obj);
 136   return s->object_is_parsable();
 137 }
 138 
 139 void symbolKlass::oop_follow_contents(oop obj) {
 140   assert (obj->is_symbol(), "object must be symbol");
 141   // Performance tweak: We skip iterating over the klass pointer since we
 142   // know that Universe::symbolKlassObj never moves.
 143   // Note: do not follow next link here (see SymbolTable::follow_contents)
 144 }
 145 
 146 #ifndef SERIALGC
 147 void symbolKlass::oop_follow_contents(ParCompactionManager* cm, oop obj) {
 148   assert (obj->is_symbol(), "object must be symbol");
 149   // Performance tweak: We skip iterating over the klass pointer since we
 150   // know that Universe::symbolKlassObj never moves.
 151   // Note: do not follow next link here (see SymbolTable::follow_contents)
 152 }
 153 #endif // SERIALGC
 154 
 155 int symbolKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
 156   assert(obj->is_symbol(), "object must be symbol");
 157   symbolOop s = symbolOop(obj);
 158   // Get size before changing pointers.
 159   // Don't call size() or oop_size() since that is a virtual call.
 160   int size = s->object_size();
 161   // Performance tweak: We skip iterating over the klass pointer since we
 162   // know that Universe::symbolKlassObj never moves.
 163   return size;
 164 }
 165 
 166 
 167 int symbolKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
 168   assert(obj->is_symbol(), "object must be symbol");
 169   symbolOop s = symbolOop(obj);
 170   // Get size before changing pointers.
 171   // Don't call size() or oop_size() since that is a virtual call.
 172   int size = s->object_size();
 173   // Performance tweak: We skip iterating over the klass pointer since we
 174   // know that Universe::symbolKlassObj never moves.
 175   return size;
 176 }
 177 
 178 
 179 int symbolKlass::oop_adjust_pointers(oop obj) {
 180   assert(obj->is_symbol(), "should be symbol");
 181   symbolOop s = symbolOop(obj);
 182   // Get size before changing pointers.
 183   // Don't call size() or oop_size() since that is a virtual call.
 184   int size = s->object_size();
 185   // Performance tweak: We skip iterating over the klass pointer since we
 186   // know that Universe::symbolKlassObj never moves.
 187   return size;
 188 }
 189 
 190 
 191 #ifndef SERIALGC
 192 void symbolKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
 193   assert(obj->is_symbol(), "should be symbol");
 194 }
 195 
 196 int symbolKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
 197   assert(obj->is_symbol(), "should be symbol");
 198   return symbolOop(obj)->object_size();
 199 }
 200 
 201 int symbolKlass::oop_update_pointers(ParCompactionManager* cm, oop obj,
 202                                      HeapWord* beg_addr, HeapWord* end_addr) {
 203   assert(obj->is_symbol(), "should be symbol");
 204   return symbolOop(obj)->object_size();
 205 }
 206 #endif // SERIALGC
 207 
 208 #ifndef PRODUCT
 209 // Printing
 210 
 211 void symbolKlass::oop_print_on(oop obj, outputStream* st) {
 212   st->print("Symbol: '");
 213   symbolOop(obj)->print_symbol_on(st);
 214   st->print("'");
 215 }
 216 
 217 #endif //PRODUCT
 218 
 219 void symbolKlass::oop_print_value_on(oop obj, outputStream* st) {
 220   symbolOop sym = symbolOop(obj);
 221   st->print("'");
 222   for (int i = 0; i < sym->utf8_length(); i++) {
 223     st->print("%c", sym->byte_at(i));
 224   }
 225   st->print("'");
 226 }
 227 
 228 const char* symbolKlass::internal_name() const {
 229   return "{symbol}";
 230 }