Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/g1BiasedArray.cpp
38921 views
/*1* Copyright (c) 2013, 2014, 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*22*/2324#include "precompiled.hpp"25#include "gc_implementation/g1/g1BiasedArray.hpp"26#include "memory/padded.inline.hpp"2728// Allocate a new array, generic version.29address G1BiasedMappedArrayBase::create_new_base_array(size_t length, size_t elem_size) {30assert(length > 0, "just checking");31assert(elem_size > 0, "just checking");32return PaddedPrimitiveArray<u_char, mtGC>::create_unfreeable(length * elem_size);33}3435#ifndef PRODUCT36void G1BiasedMappedArrayBase::verify_index(idx_t index) const {37guarantee(_base != NULL, "Array not initialized");38guarantee(index < length(), err_msg("Index out of bounds index: " SIZE_FORMAT " length: " SIZE_FORMAT, index, length()));39}4041void G1BiasedMappedArrayBase::verify_biased_index(idx_t biased_index) const {42guarantee(_biased_base != NULL, "Array not initialized");43guarantee(biased_index >= bias() && biased_index < (bias() + length()),44err_msg("Biased index out of bounds, index: " SIZE_FORMAT " bias: " SIZE_FORMAT " length: " SIZE_FORMAT, biased_index, bias(), length()));45}4647void G1BiasedMappedArrayBase::verify_biased_index_inclusive_end(idx_t biased_index) const {48guarantee(_biased_base != NULL, "Array not initialized");49guarantee(biased_index >= bias() && biased_index <= (bias() + length()),50err_msg("Biased index out of inclusive bounds, index: " SIZE_FORMAT " bias: " SIZE_FORMAT " length: " SIZE_FORMAT, biased_index, bias(), length()));51}5253class TestMappedArray : public G1BiasedMappedArray<int> {54protected:55virtual int default_value() const { return 0xBAADBABE; }56public:57static void test_biasedarray() {58const size_t REGION_SIZE_IN_WORDS = 512;59const size_t NUM_REGIONS = 20;60HeapWord* fake_heap = (HeapWord*)LP64_ONLY(0xBAAA00000) NOT_LP64(0xBA000000); // Any value that is non-zero6162TestMappedArray array;63array.initialize(fake_heap, fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS,64REGION_SIZE_IN_WORDS * HeapWordSize);65// Check address calculation (bounds)66assert(array.bottom_address_mapped() == fake_heap,67err_msg("bottom mapped address should be " PTR_FORMAT ", but is " PTR_FORMAT, p2i(fake_heap), p2i(array.bottom_address_mapped())));68assert(array.end_address_mapped() == (fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS), "must be");6970int* bottom = array.address_mapped_to(fake_heap);71assert((void*)bottom == (void*) array.base(), "must be");72int* end = array.address_mapped_to(fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS);73assert((void*)end == (void*)(array.base() + array.length()), "must be");74// The entire array should contain default value elements75for (int* current = bottom; current < end; current++) {76assert(*current == array.default_value(), "must be");77}7879// Test setting values in the table8081HeapWord* region_start_address = fake_heap + REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2);82HeapWord* region_end_address = fake_heap + (REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2) + REGION_SIZE_IN_WORDS - 1);8384// Set/get by address tests: invert some value; first retrieve one85int actual_value = array.get_by_index(NUM_REGIONS / 2);86array.set_by_index(NUM_REGIONS / 2, ~actual_value);87// Get the same value by address, should correspond to the start of the "region"88int value = array.get_by_address(region_start_address);89assert(value == ~actual_value, "must be");90// Get the same value by address, at one HeapWord before the start91value = array.get_by_address(region_start_address - 1);92assert(value == array.default_value(), "must be");93// Get the same value by address, at the end of the "region"94value = array.get_by_address(region_end_address);95assert(value == ~actual_value, "must be");96// Make sure the next value maps to another index97value = array.get_by_address(region_end_address + 1);98assert(value == array.default_value(), "must be");99100// Reset the value in the array101array.set_by_address(region_start_address + (region_end_address - region_start_address) / 2, actual_value);102103// The entire array should have the default value again104for (int* current = bottom; current < end; current++) {105assert(*current == array.default_value(), "must be");106}107108// Set/get by index tests: invert some value109idx_t index = NUM_REGIONS / 2;110actual_value = array.get_by_index(index);111array.set_by_index(index, ~actual_value);112113value = array.get_by_index(index);114assert(value == ~actual_value, "must be");115116value = array.get_by_index(index - 1);117assert(value == array.default_value(), "must be");118119value = array.get_by_index(index + 1);120assert(value == array.default_value(), "must be");121122array.set_by_index(0, 0);123value = array.get_by_index(0);124assert(value == 0, "must be");125126array.set_by_index(array.length() - 1, 0);127value = array.get_by_index(array.length() - 1);128assert(value == 0, "must be");129130array.set_by_index(index, 0);131132// The array should have three zeros, and default values otherwise133size_t num_zeros = 0;134for (int* current = bottom; current < end; current++) {135assert(*current == array.default_value() || *current == 0, "must be");136if (*current == 0) {137num_zeros++;138}139}140assert(num_zeros == 3, "must be");141}142};143144void TestG1BiasedArray_test() {145TestMappedArray::test_biasedarray();146}147148#endif149150151