metadataFactory.hpp revision 6872:16286b7d7c6e
154359Sroberto/*
254359Sroberto * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
354359Sroberto * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4280849Scy *
554359Sroberto * This code is free software; you can redistribute it and/or modify it
654359Sroberto * under the terms of the GNU General Public License version 2 only, as
7280849Scy * published by the Free Software Foundation.
854359Sroberto *
9280849Scy * This code is distributed in the hope that it will be useful, but WITHOUT
10280849Scy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1154359Sroberto * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12280849Scy * version 2 for more details (a copy is included in the LICENSE file that
1354359Sroberto * accompanied this code).
14280849Scy *
1554359Sroberto * You should have received a copy of the GNU General Public License version
16280849Scy * 2 along with this work; if not, write to the Free Software Foundation,
1754359Sroberto * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18280849Scy *
19280849Scy * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20280849Scy * or visit www.oracle.com if you need additional information or have any
21280849Scy * questions.
22280849Scy *
2354359Sroberto */
2454359Sroberto
2554359Sroberto#ifndef SHARE_VM_MEMORY_METADATAFACTORY_HPP
26280849Scy#define SHARE_VM_MEMORY_METADATAFACTORY_HPP
2754359Sroberto
2854359Sroberto#include "classfile/classLoaderData.hpp"
29132451Sroberto#include "utilities/array.hpp"
30280849Scy#include "utilities/exceptions.hpp"
31280849Scy#include "utilities/globalDefinitions.hpp"
32280849Scy
33280849Scyclass MetadataFactory : AllStatic {
34280849Scy public:
35280849Scy  template <typename T>
36132451Sroberto  static Array<T>* new_array(ClassLoaderData* loader_data, int length, TRAPS) {
3754359Sroberto    // The "true" argument is because all metadata arrays are read only when
38289764Sglebius    // dumped to the shared archive
3954359Sroberto    return new (loader_data, length, /*read_only*/true, THREAD) Array<T>(length);
40289764Sglebius  }
41289764Sglebius
42289764Sglebius  template <typename T>
43289764Sglebius  static Array<T>* new_array(ClassLoaderData* loader_data, int length, T value, TRAPS) {
44280849Scy    Array<T>* array = new_array<T>(loader_data, length, CHECK_NULL);
45280849Scy    for (int i = 0; i < length; i++) {
46280849Scy      array->at_put(i, value);
47280849Scy    }
48280849Scy    return array;
49280849Scy  }
50280849Scy
51280849Scy  template <typename T>
52280849Scy  static Array<T>* new_writeable_array(ClassLoaderData* loader_data, int length, TRAPS) {
53280849Scy    return new (loader_data, length, /*read_only*/false, THREAD) Array<T>(length);
54280849Scy  }
55280849Scy
56280849Scy  template <typename T>
57280849Scy  static Array<T>* new_writeable_array(ClassLoaderData* loader_data, int length, T value, TRAPS) {
58280849Scy    Array<T>* array = new_writeable_array<T>(loader_data, length, CHECK_NULL);
59280849Scy    for (int i = 0; i < length; i++) {
60280849Scy      array->at_put(i, value);
61280849Scy    }
62280849Scy    return array;
63280849Scy  }
64280849Scy
65280849Scy  template <typename T>
66280849Scy  static void free_array(ClassLoaderData* loader_data, Array<T>* data) {
67280849Scy    if (data != NULL) {
68280849Scy      assert(loader_data != NULL, "shouldn't pass null");
69280849Scy      assert(!data->is_shared(), "cannot deallocate array in shared spaces");
70280849Scy      int size = data->size();
71280849Scy      if (DumpSharedSpaces) {
7254359Sroberto        loader_data->ro_metaspace()->deallocate((MetaWord*)data, size, false);
73280849Scy      } else {
74280849Scy        loader_data->metaspace_non_null()->deallocate((MetaWord*)data, size, false);
75280849Scy      }
76132451Sroberto    }
77132451Sroberto  }
78289764Sglebius
79280849Scy  // Deallocation method for metadata
80280849Scy  template <class T>
81132451Sroberto  static void free_metadata(ClassLoaderData* loader_data, T md) {
82280849Scy    if (DumpSharedSpaces) {
83280849Scy      // FIXME: the freeing code is buggy, especially when PrintSharedSpaces is enabled.
84280849Scy      // Disable for now -- this means if you specify bad classes in your classlist you
8554359Sroberto      // may have wasted space inside the archive.
8654359Sroberto      return;
87    }
88    if (md != NULL) {
89      assert(loader_data != NULL, "shouldn't pass null");
90      int size = md->size();
91      // Call metadata's deallocate function which will call deallocate fields
92      assert(!DumpSharedSpaces, "cannot deallocate metadata when dumping CDS archive");
93      assert(!md->on_stack(), "can't deallocate things on stack");
94      assert(!md->is_shared(), "cannot deallocate if in shared spaces");
95      md->deallocate_contents(loader_data);
96      loader_data->metaspace_non_null()->deallocate((MetaWord*)md, size, md->is_klass());
97    }
98  }
99};
100
101#endif // SHARE_VM_MEMORY_METADATAFACTORY_HPP
102