1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/g1BiasedArray.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,149 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "gc_implementation/g1/g1BiasedArray.hpp" 1.30 +#include "memory/padded.inline.hpp" 1.31 + 1.32 +// Allocate a new array, generic version. 1.33 +address G1BiasedMappedArrayBase::create_new_base_array(size_t length, size_t elem_size) { 1.34 + assert(length > 0, "just checking"); 1.35 + assert(elem_size > 0, "just checking"); 1.36 + return PaddedPrimitiveArray<u_char, mtGC>::create_unfreeable(length * elem_size); 1.37 +} 1.38 + 1.39 +#ifndef PRODUCT 1.40 +void G1BiasedMappedArrayBase::verify_index(idx_t index) const { 1.41 + guarantee(_base != NULL, "Array not initialized"); 1.42 + guarantee(index < length(), err_msg("Index out of bounds index: "SIZE_FORMAT" length: "SIZE_FORMAT, index, length())); 1.43 +} 1.44 + 1.45 +void G1BiasedMappedArrayBase::verify_biased_index(idx_t biased_index) const { 1.46 + guarantee(_biased_base != NULL, "Array not initialized"); 1.47 + guarantee(biased_index >= bias() && biased_index < (bias() + length()), 1.48 + err_msg("Biased index out of bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length())); 1.49 +} 1.50 + 1.51 +void G1BiasedMappedArrayBase::verify_biased_index_inclusive_end(idx_t biased_index) const { 1.52 + guarantee(_biased_base != NULL, "Array not initialized"); 1.53 + guarantee(biased_index >= bias() && biased_index <= (bias() + length()), 1.54 + err_msg("Biased index out of inclusive bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length())); 1.55 +} 1.56 + 1.57 +class TestMappedArray : public G1BiasedMappedArray<int> { 1.58 +protected: 1.59 + virtual int default_value() const { return 0xBAADBABE; } 1.60 +public: 1.61 + static void test_biasedarray() { 1.62 + const size_t REGION_SIZE_IN_WORDS = 512; 1.63 + const size_t NUM_REGIONS = 20; 1.64 + HeapWord* fake_heap = (HeapWord*)LP64_ONLY(0xBAAA00000) NOT_LP64(0xBA000000); // Any value that is non-zero 1.65 + 1.66 + TestMappedArray array; 1.67 + array.initialize(fake_heap, fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS, 1.68 + REGION_SIZE_IN_WORDS * HeapWordSize); 1.69 + // Check address calculation (bounds) 1.70 + assert(array.bottom_address_mapped() == fake_heap, 1.71 + err_msg("bottom mapped address should be " PTR_FORMAT ", but is " PTR_FORMAT, p2i(fake_heap), p2i(array.bottom_address_mapped()))); 1.72 + assert(array.end_address_mapped() == (fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS), "must be"); 1.73 + 1.74 + int* bottom = array.address_mapped_to(fake_heap); 1.75 + assert((void*)bottom == (void*) array.base(), "must be"); 1.76 + int* end = array.address_mapped_to(fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS); 1.77 + assert((void*)end == (void*)(array.base() + array.length()), "must be"); 1.78 + // The entire array should contain default value elements 1.79 + for (int* current = bottom; current < end; current++) { 1.80 + assert(*current == array.default_value(), "must be"); 1.81 + } 1.82 + 1.83 + // Test setting values in the table 1.84 + 1.85 + HeapWord* region_start_address = fake_heap + REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2); 1.86 + HeapWord* region_end_address = fake_heap + (REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2) + REGION_SIZE_IN_WORDS - 1); 1.87 + 1.88 + // Set/get by address tests: invert some value; first retrieve one 1.89 + int actual_value = array.get_by_index(NUM_REGIONS / 2); 1.90 + array.set_by_index(NUM_REGIONS / 2, ~actual_value); 1.91 + // Get the same value by address, should correspond to the start of the "region" 1.92 + int value = array.get_by_address(region_start_address); 1.93 + assert(value == ~actual_value, "must be"); 1.94 + // Get the same value by address, at one HeapWord before the start 1.95 + value = array.get_by_address(region_start_address - 1); 1.96 + assert(value == array.default_value(), "must be"); 1.97 + // Get the same value by address, at the end of the "region" 1.98 + value = array.get_by_address(region_end_address); 1.99 + assert(value == ~actual_value, "must be"); 1.100 + // Make sure the next value maps to another index 1.101 + value = array.get_by_address(region_end_address + 1); 1.102 + assert(value == array.default_value(), "must be"); 1.103 + 1.104 + // Reset the value in the array 1.105 + array.set_by_address(region_start_address + (region_end_address - region_start_address) / 2, actual_value); 1.106 + 1.107 + // The entire array should have the default value again 1.108 + for (int* current = bottom; current < end; current++) { 1.109 + assert(*current == array.default_value(), "must be"); 1.110 + } 1.111 + 1.112 + // Set/get by index tests: invert some value 1.113 + idx_t index = NUM_REGIONS / 2; 1.114 + actual_value = array.get_by_index(index); 1.115 + array.set_by_index(index, ~actual_value); 1.116 + 1.117 + value = array.get_by_index(index); 1.118 + assert(value == ~actual_value, "must be"); 1.119 + 1.120 + value = array.get_by_index(index - 1); 1.121 + assert(value == array.default_value(), "must be"); 1.122 + 1.123 + value = array.get_by_index(index + 1); 1.124 + assert(value == array.default_value(), "must be"); 1.125 + 1.126 + array.set_by_index(0, 0); 1.127 + value = array.get_by_index(0); 1.128 + assert(value == 0, "must be"); 1.129 + 1.130 + array.set_by_index(array.length() - 1, 0); 1.131 + value = array.get_by_index(array.length() - 1); 1.132 + assert(value == 0, "must be"); 1.133 + 1.134 + array.set_by_index(index, 0); 1.135 + 1.136 + // The array should have three zeros, and default values otherwise 1.137 + size_t num_zeros = 0; 1.138 + for (int* current = bottom; current < end; current++) { 1.139 + assert(*current == array.default_value() || *current == 0, "must be"); 1.140 + if (*current == 0) { 1.141 + num_zeros++; 1.142 + } 1.143 + } 1.144 + assert(num_zeros == 3, "must be"); 1.145 + } 1.146 +}; 1.147 + 1.148 +void TestG1BiasedArray_test() { 1.149 + TestMappedArray::test_biasedarray(); 1.150 +} 1.151 + 1.152 +#endif