Path: blob/master/test/hotspot/gtest/utilities/test_metaspaceClosure.cpp
41144 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "memory/allocation.hpp"25#include "memory/metadataFactory.hpp"26#include "memory/metaspaceClosure.hpp"27#include "oops/array.hpp"28#include "oops/metadata.hpp"29#include "runtime/thread.hpp"30#include "unittest.hpp"3132class MyMetaData : public MetaspaceObj {33public:34MyMetaData* _a;35MyMetaData* _b;3637MyMetaData() : _a(NULL), _b(NULL) {}3839MetaspaceObj::Type type() const {40return MetaspaceObj::SymbolType; // Just lie. It doesn't matter in this test41}42const char* internal_name() const {43return "MyMetaData";44}45int size() const {46return align_up((int)sizeof(MyMetaData), wordSize) / wordSize;47};4849static bool is_read_only_by_default() {50return true;51}5253void metaspace_pointers_do(MetaspaceClosure* it) {54it->push(&_a);55it->push(&_b);56}57};5859class MyUniqueMetaspaceClosure : public MetaspaceClosure {60static constexpr int SIZE = 10;61MyMetaData* _visited[SIZE];62int _count;63public:64MyUniqueMetaspaceClosure() {65for (int i = 0; i < SIZE; i++) {66_visited[i] = NULL;67}68_count = 0;69}7071virtual bool do_ref(Ref* ref, bool read_only) {72MyMetaData* ptr = (MyMetaData*)ref->obj();73assert(_count < SIZE, "out of bounds");74_visited[_count++] = ptr;75return true; // recurse76}7778bool has_visited(MyMetaData* p) {79for (int i = 0; i < SIZE; i++) {80if (_visited[i] == p) {81return true;82}83}84return false;85}86};8788// iterate an Array<MyMetaData*>89TEST_VM(MetaspaceClosure, MSOPointerArrayRef) {90JavaThread* THREAD = JavaThread::current();91ClassLoaderData* cld = ClassLoaderData::the_null_class_loader_data();92Array<MyMetaData*>* array = MetadataFactory::new_array<MyMetaData*>(cld, 4, THREAD);93for (int i = 0; i < array->length(); i++) {94EXPECT_TRUE(array->at(i) == NULL) << "should be initialized to null";95}9697MyMetaData x;98MyMetaData y;99MyMetaData z;100101array->at_put(0, &x);102array->at_put(2, &y);103y._a = &z;104105MyUniqueMetaspaceClosure closure;106closure.push(&array);107108EXPECT_TRUE(closure.has_visited(&x)) << "must be";109EXPECT_TRUE(closure.has_visited(&y)) << "must be";110EXPECT_TRUE(closure.has_visited(&z)) << "must be";111}112113// iterate an Array<MyMetaData>114TEST_VM(MetaspaceClosure, MSOArrayRef) {115JavaThread* THREAD = JavaThread::current();116ClassLoaderData* cld = ClassLoaderData::the_null_class_loader_data();117Array<MyMetaData>* array = MetadataFactory::new_array<MyMetaData>(cld, 4, THREAD);118for (int i = 0; i < array->length(); i++) {119EXPECT_TRUE(array->at(i)._a == NULL) << "should be initialized to null";120EXPECT_TRUE(array->at(i)._b == NULL) << "should be initialized to null";121}122123MyMetaData x;124MyMetaData y;125MyMetaData z;126127array->adr_at(0)->_a = &x;128array->adr_at(2)->_b = &y;129y._a = &z;130131MyUniqueMetaspaceClosure closure;132closure.push(&array);133134EXPECT_TRUE(closure.has_visited(&x)) << "must be";135EXPECT_TRUE(closure.has_visited(&y)) << "must be";136EXPECT_TRUE(closure.has_visited(&z)) << "must be";137}138139140