Path: blob/master/test/hotspot/gtest/metaspace/metaspaceGtestCommon.cpp
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#include "precompiled.hpp"26#include "metaspaceGtestCommon.hpp"27#include "metaspaceGtestRangeHelpers.hpp"28#include "runtime/os.hpp"2930void zap_range(MetaWord* p, size_t word_size) {31for (MetaWord* pzap = p; pzap < p + word_size; pzap += os::vm_page_size() / BytesPerWord) {32*pzap = (MetaWord)NOT_LP64(0xFEFEFEFE) LP64_ONLY(0xFEFEFEFEEFEFEFEFULL);33}34}3536// Writes a unique pattern to p37void mark_address(MetaWord* p, uintx pattern) {38MetaWord x = (MetaWord)((uintx) p ^ pattern);39*p = x;40}4142// checks pattern at address43void check_marked_address(const MetaWord* p, uintx pattern) {44MetaWord x = (MetaWord)((uintx) p ^ pattern);45EXPECT_EQ(*p, x);46}4748// "fill_range_with_pattern" fills a range of heap words with pointers to itself.49//50// The idea is to fill a memory range with a pattern which is both marked clearly to the caller51// and cannot be moved without becoming invalid.52//53// The filled range can be checked with check_range_for_pattern. One also can only check54// a sub range of the original range.55void fill_range_with_pattern(MetaWord* p, size_t word_size, uintx pattern) {56assert(word_size > 0 && p != NULL, "sanity");57for (MetaWord* p2 = p; p2 < p + word_size; p2++) {58mark_address(p2, pattern);59}60}6162void check_range_for_pattern(const MetaWord* p, size_t word_size, uintx pattern) {63assert(p != NULL, "sanity");64const MetaWord* p2 = p;65while (p2 < p + word_size) {66check_marked_address(p2, pattern);67p2++;68}69}7071// Similar to fill_range_with_pattern, but only marks start and end. This is optimized for cases72// where fill_range_with_pattern just is too slow.73// Use check_marked_range to check the range. In contrast to check_range_for_pattern, only the original74// range can be checked.75void mark_range(MetaWord* p, size_t word_size, uintx pattern) {76assert(word_size > 0 && p != NULL, "sanity");77mark_address(p, pattern);78mark_address(p + word_size - 1, pattern);79}8081void check_marked_range(const MetaWord* p, size_t word_size, uintx pattern) {82assert(word_size > 0 && p != NULL, "sanity");83check_marked_address(p, pattern);84check_marked_address(p + word_size - 1, pattern);85}8687void mark_range(MetaWord* p, size_t word_size) {88assert(word_size > 0 && p != NULL, "sanity");89uintx pattern = (uintx)p2i(p);90mark_range(p, word_size, pattern);91}9293void check_marked_range(const MetaWord* p, size_t word_size) {94uintx pattern = (uintx)p2i(p);95check_marked_range(p, word_size, pattern);96}979899100