1 /*
   2  * Copyright (c) 2012, 2014, 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/classLoaderData.hpp"
  27 #include "memory/heapInspection.hpp"
  28 #include "memory/metadataFactory.hpp"
  29 #include "memory/oopFactory.hpp"
  30 #include "oops/annotations.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "utilities/ostream.hpp"
  33 
  34 // Allocate annotations in metadata area
  35 Annotations* Annotations::allocate(ClassLoaderData* loader_data, TRAPS) {
  36   return new (loader_data, size(), true, MetaspaceObj::AnnotationType, THREAD) Annotations();
  37 }
  38 
  39 // helper
  40 void Annotations::free_contents(ClassLoaderData* loader_data, Array<AnnotationArray*>* p) {
  41   if (p != NULL) {
  42     for (int i = 0; i < p->length(); i++) {
  43       MetadataFactory::free_array<u1>(loader_data, p->at(i));
  44     }
  45     MetadataFactory::free_array<AnnotationArray*>(loader_data, p);
  46   }
  47 }
  48 
  49 void Annotations::deallocate_contents(ClassLoaderData* loader_data) {
  50   if (class_annotations() != NULL) {
  51     MetadataFactory::free_array<u1>(loader_data, class_annotations());
  52   }
  53   free_contents(loader_data, fields_annotations());
  54 
  55   if (class_type_annotations() != NULL) {
  56     MetadataFactory::free_array<u1>(loader_data, class_type_annotations());
  57   }
  58   free_contents(loader_data, fields_type_annotations());
  59 }
  60 
  61 // Copy annotations to JVM call or reflection to the java heap.
  62 // The alternative to creating this array and adding to Java heap pressure
  63 // is to have a hashtable of the already created typeArrayOops
  64 typeArrayOop Annotations::make_java_array(AnnotationArray* annotations, TRAPS) {
  65   if (annotations != NULL) {
  66     int length = annotations->length();
  67     typeArrayOop copy = oopFactory::new_byteArray(length, CHECK_NULL);
  68     for (int i = 0; i< length; i++) {
  69       copy->byte_at_put(i, annotations->at(i));
  70     }
  71     return copy;
  72   } else {
  73     return NULL;
  74   }
  75 }
  76 
  77 
  78 void Annotations::print_value_on(outputStream* st) const {
  79   st->print("Anotations(" INTPTR_FORMAT ")", p2i(this));
  80 }
  81 
  82 #if INCLUDE_SERVICES
  83 // Size Statistics
  84 
  85 julong Annotations::count_bytes(Array<AnnotationArray*>* p) {
  86   julong bytes = 0;
  87   if (p != NULL) {
  88     for (int i = 0; i < p->length(); i++) {
  89       bytes += KlassSizeStats::count_array(p->at(i));
  90     }
  91     bytes += KlassSizeStats::count_array(p);
  92   }
  93   return bytes;
  94 }
  95 
  96 void Annotations::collect_statistics(KlassSizeStats *sz) const {
  97   sz->_annotations_bytes = sz->count(this);
  98   sz->_class_annotations_bytes = sz->count(class_annotations());
  99   sz->_class_type_annotations_bytes = sz->count(class_type_annotations());
 100   sz->_fields_annotations_bytes = count_bytes(fields_annotations());
 101   sz->_fields_type_annotations_bytes = count_bytes(fields_type_annotations());
 102 
 103   sz->_annotations_bytes +=
 104       sz->_class_annotations_bytes +
 105       sz->_class_type_annotations_bytes +
 106       sz->_fields_annotations_bytes +
 107       sz->_fields_type_annotations_bytes;
 108 
 109   sz->_ro_bytes += sz->_annotations_bytes;
 110 }
 111 #endif // INCLUDE_SERVICES
 112 
 113 #define BULLET  " - "
 114 
 115 #ifndef PRODUCT
 116 void Annotations::print_on(outputStream* st) const {
 117   st->print(BULLET"class_annotations            "); class_annotations()->print_value_on(st);
 118   st->print(BULLET"fields_annotations           "); fields_annotations()->print_value_on(st);
 119   st->print(BULLET"class_type_annotations       "); class_type_annotations()->print_value_on(st);
 120   st->print(BULLET"fields_type_annotations      "); fields_type_annotations()->print_value_on(st);
 121 }
 122 #endif // PRODUCT