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/javaThread.hpp"
31
#include "unittest.hpp"
33
class MyMetaData : public MetaspaceObj {
38
MyMetaData() : _a(nullptr), _b(nullptr) {}
40
MetaspaceObj::Type type() const {
41
return MetaspaceObj::SymbolType;
43
const char* internal_name() const {
47
return align_up((int)sizeof(MyMetaData), wordSize) / wordSize;
50
static bool is_read_only_by_default() {
54
void metaspace_pointers_do(MetaspaceClosure* it) {
60
class MyUniqueMetaspaceClosure : public MetaspaceClosure {
61
static constexpr int SIZE = 10;
62
MyMetaData* _visited[SIZE];
65
MyUniqueMetaspaceClosure() {
66
for (int i = 0; i < SIZE; i++) {
67
_visited[i] = nullptr;
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;
79
bool has_visited(MyMetaData* p) {
80
for (int i = 0; i < SIZE; i++) {
81
if (_visited[i] == p) {
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) == nullptr) << "should be initialized to null";
102
array->at_put(0, &x);
103
array->at_put(2, &y);
106
MyUniqueMetaspaceClosure closure;
107
closure.push(&array);
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";
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 == nullptr) << "should be initialized to null";
121
EXPECT_TRUE(array->at(i)._b == nullptr) << "should be initialized to null";
128
array->adr_at(0)->_a = &x;
129
array->adr_at(2)->_b = &y;
132
MyUniqueMetaspaceClosure closure;
133
closure.push(&array);
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";