Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_metaspaceClosure.cpp
41144 views
1
/*
2
* Copyright (c) 2021, 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
#include "precompiled.hpp"
25
#include "memory/allocation.hpp"
26
#include "memory/metadataFactory.hpp"
27
#include "memory/metaspaceClosure.hpp"
28
#include "oops/array.hpp"
29
#include "oops/metadata.hpp"
30
#include "runtime/thread.hpp"
31
#include "unittest.hpp"
32
33
class MyMetaData : public MetaspaceObj {
34
public:
35
MyMetaData* _a;
36
MyMetaData* _b;
37
38
MyMetaData() : _a(NULL), _b(NULL) {}
39
40
MetaspaceObj::Type type() const {
41
return MetaspaceObj::SymbolType; // Just lie. It doesn't matter in this test
42
}
43
const char* internal_name() const {
44
return "MyMetaData";
45
}
46
int size() const {
47
return align_up((int)sizeof(MyMetaData), wordSize) / wordSize;
48
};
49
50
static bool is_read_only_by_default() {
51
return true;
52
}
53
54
void metaspace_pointers_do(MetaspaceClosure* it) {
55
it->push(&_a);
56
it->push(&_b);
57
}
58
};
59
60
class MyUniqueMetaspaceClosure : public MetaspaceClosure {
61
static constexpr int SIZE = 10;
62
MyMetaData* _visited[SIZE];
63
int _count;
64
public:
65
MyUniqueMetaspaceClosure() {
66
for (int i = 0; i < SIZE; i++) {
67
_visited[i] = NULL;
68
}
69
_count = 0;
70
}
71
72
virtual bool do_ref(Ref* ref, bool read_only) {
73
MyMetaData* ptr = (MyMetaData*)ref->obj();
74
assert(_count < SIZE, "out of bounds");
75
_visited[_count++] = ptr;
76
return true; // recurse
77
}
78
79
bool has_visited(MyMetaData* p) {
80
for (int i = 0; i < SIZE; i++) {
81
if (_visited[i] == p) {
82
return true;
83
}
84
}
85
return false;
86
}
87
};
88
89
// iterate an Array<MyMetaData*>
90
TEST_VM(MetaspaceClosure, MSOPointerArrayRef) {
91
JavaThread* THREAD = JavaThread::current();
92
ClassLoaderData* cld = ClassLoaderData::the_null_class_loader_data();
93
Array<MyMetaData*>* array = MetadataFactory::new_array<MyMetaData*>(cld, 4, THREAD);
94
for (int i = 0; i < array->length(); i++) {
95
EXPECT_TRUE(array->at(i) == NULL) << "should be initialized to null";
96
}
97
98
MyMetaData x;
99
MyMetaData y;
100
MyMetaData z;
101
102
array->at_put(0, &x);
103
array->at_put(2, &y);
104
y._a = &z;
105
106
MyUniqueMetaspaceClosure closure;
107
closure.push(&array);
108
109
EXPECT_TRUE(closure.has_visited(&x)) << "must be";
110
EXPECT_TRUE(closure.has_visited(&y)) << "must be";
111
EXPECT_TRUE(closure.has_visited(&z)) << "must be";
112
}
113
114
// iterate an Array<MyMetaData>
115
TEST_VM(MetaspaceClosure, MSOArrayRef) {
116
JavaThread* THREAD = JavaThread::current();
117
ClassLoaderData* cld = ClassLoaderData::the_null_class_loader_data();
118
Array<MyMetaData>* array = MetadataFactory::new_array<MyMetaData>(cld, 4, THREAD);
119
for (int i = 0; i < array->length(); i++) {
120
EXPECT_TRUE(array->at(i)._a == NULL) << "should be initialized to null";
121
EXPECT_TRUE(array->at(i)._b == NULL) << "should be initialized to null";
122
}
123
124
MyMetaData x;
125
MyMetaData y;
126
MyMetaData z;
127
128
array->adr_at(0)->_a = &x;
129
array->adr_at(2)->_b = &y;
130
y._a = &z;
131
132
MyUniqueMetaspaceClosure closure;
133
closure.push(&array);
134
135
EXPECT_TRUE(closure.has_visited(&x)) << "must be";
136
EXPECT_TRUE(closure.has_visited(&y)) << "must be";
137
EXPECT_TRUE(closure.has_visited(&z)) << "must be";
138
}
139
140