1 /*
 2  * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
 3  * Copyright (c) 2020 SAP SE. All rights reserved.
 4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 5  *
 6  * This code is free software; you can redistribute it and/or modify it
 7  * under the terms of the GNU General Public License version 2 only, as
 8  * published by the Free Software Foundation.
 9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 #include "precompiled.hpp"
27 #include "memory/metaspace/chunkManager.hpp"
28 #include "memory/metaspace/commitLimiter.hpp"
29 #include "memory/metaspace/metaspaceContext.hpp"
30 #include "memory/metaspace/virtualSpaceList.hpp"
31 #include "utilities/debug.hpp"
32 #include "utilities/globalDefinitions.hpp"
33 #include "utilities/ostream.hpp"
34 
35 namespace metaspace {
36 
37 MetaspaceContext* MetaspaceContext::_class_space_context = nullptr;
38 MetaspaceContext* MetaspaceContext::_nonclass_space_context = nullptr;
39 
40 // Destroys the context: deletes chunkmanager and virtualspacelist.
41 //  If this is a non-expandable context over an existing space, that space remains
42 //  untouched, otherwise all memory is unmapped.
43 // Note: the standard metaspace contexts (non-class context and class context) are
44 //  never deleted. This code only exists for the sake of tests and for future reuse
45 //  of metaspace contexts in different scenarios.
46 MetaspaceContext::~MetaspaceContext() {
47   delete _cm;
48   delete _vslist;
49 }
50 
51 // Create a new, empty, expandable metaspace context.
52 MetaspaceContext* MetaspaceContext::create_expandable_context(const char* name, CommitLimiter* commit_limiter) {
53   VirtualSpaceList* vsl = new VirtualSpaceList(name, commit_limiter);
54   ChunkManager* cm = new ChunkManager(name, vsl);
55   return new MetaspaceContext(name, vsl, cm);
56 }
57 
58 // Create a new, empty, non-expandable metaspace context atop of an externally provided space.
59 MetaspaceContext* MetaspaceContext::create_nonexpandable_context(const char* name, ReservedSpace rs, CommitLimiter* commit_limiter) {
60   VirtualSpaceList* vsl = new VirtualSpaceList(name, rs, commit_limiter);
61   ChunkManager* cm = new ChunkManager(name, vsl);
62   return new MetaspaceContext(name, vsl, cm);
63 }
64 
65 void MetaspaceContext::initialize_class_space_context(ReservedSpace rs) {
66   _class_space_context = create_nonexpandable_context("class-space", rs, CommitLimiter::globalLimiter());
67 }
68 
69 void MetaspaceContext::initialize_nonclass_space_context() {
70   _nonclass_space_context = create_expandable_context("non-class-space", CommitLimiter::globalLimiter());
71 }
72 
73 void MetaspaceContext::print_on(outputStream* st) const {
74   _vslist->print_on(st);
75   _cm->print_on(st);
76 }
77 
78 size_t MetaspaceContext::used_words() const {
79   return _used_words_counter.get();
80 }
81 
82 size_t MetaspaceContext::committed_words() const {
83   return _vslist->committed_words();
84 }
85 
86 size_t MetaspaceContext::reserved_words() const {
87   return _vslist->reserved_words();
88 }
89 
90 #ifdef ASSERT
91 void MetaspaceContext::verify() const {
92   _vslist->verify();
93   _cm->verify();
94 }
95 #endif // ASSERT
96 
97 } // namespace metaspace
98