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