1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/g1BiasedArray.cpp Wed Sep 25 13:25:24 2013 +0200 1.3 @@ -0,0 +1,141 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, 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 + 1.31 +#ifndef PRODUCT 1.32 +void G1BiasedMappedArrayBase::verify_index(idx_t index) const { 1.33 + guarantee(_base != NULL, "Array not initialized"); 1.34 + guarantee(index < length(), err_msg("Index out of bounds index: "SIZE_FORMAT" length: "SIZE_FORMAT, index, length())); 1.35 +} 1.36 + 1.37 +void G1BiasedMappedArrayBase::verify_biased_index(idx_t biased_index) const { 1.38 + guarantee(_biased_base != NULL, "Array not initialized"); 1.39 + guarantee(biased_index >= bias() && biased_index < (bias() + length()), 1.40 + err_msg("Biased index out of bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length())); 1.41 +} 1.42 + 1.43 +void G1BiasedMappedArrayBase::verify_biased_index_inclusive_end(idx_t biased_index) const { 1.44 + guarantee(_biased_base != NULL, "Array not initialized"); 1.45 + guarantee(biased_index >= bias() && biased_index <= (bias() + length()), 1.46 + err_msg("Biased index out of inclusive bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length())); 1.47 +} 1.48 + 1.49 +class TestMappedArray : public G1BiasedMappedArray<int> { 1.50 +protected: 1.51 + virtual int default_value() const { return 0xBAADBABE; } 1.52 +public: 1.53 + static void test_biasedarray() { 1.54 + const size_t REGION_SIZE_IN_WORDS = 512; 1.55 + const size_t NUM_REGIONS = 20; 1.56 + HeapWord* fake_heap = (HeapWord*)LP64_ONLY(0xBAAA00000) NOT_LP64(0xBA000000); // Any value that is non-zero 1.57 + 1.58 + TestMappedArray array; 1.59 + array.initialize(fake_heap, fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS, 1.60 + REGION_SIZE_IN_WORDS * HeapWordSize); 1.61 + // Check address calculation (bounds) 1.62 + assert(array.bottom_address_mapped() == fake_heap, 1.63 + err_msg("bottom mapped address should be "PTR_FORMAT", but is "PTR_FORMAT, fake_heap, array.bottom_address_mapped())); 1.64 + assert(array.end_address_mapped() == (fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS), "must be"); 1.65 + 1.66 + int* bottom = array.address_mapped_to(fake_heap); 1.67 + assert((void*)bottom == (void*) array.base(), "must be"); 1.68 + int* end = array.address_mapped_to(fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS); 1.69 + assert((void*)end == (void*)(array.base() + array.length()), "must be"); 1.70 + // The entire array should contain default value elements 1.71 + for (int* current = bottom; current < end; current++) { 1.72 + assert(*current == array.default_value(), "must be"); 1.73 + } 1.74 + 1.75 + // Test setting values in the table 1.76 + 1.77 + HeapWord* region_start_address = fake_heap + REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2); 1.78 + HeapWord* region_end_address = fake_heap + (REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2) + REGION_SIZE_IN_WORDS - 1); 1.79 + 1.80 + // Set/get by address tests: invert some value; first retrieve one 1.81 + int actual_value = array.get_by_index(NUM_REGIONS / 2); 1.82 + array.set_by_index(NUM_REGIONS / 2, ~actual_value); 1.83 + // Get the same value by address, should correspond to the start of the "region" 1.84 + int value = array.get_by_address(region_start_address); 1.85 + assert(value == ~actual_value, "must be"); 1.86 + // Get the same value by address, at one HeapWord before the start 1.87 + value = array.get_by_address(region_start_address - 1); 1.88 + assert(value == array.default_value(), "must be"); 1.89 + // Get the same value by address, at the end of the "region" 1.90 + value = array.get_by_address(region_end_address); 1.91 + assert(value == ~actual_value, "must be"); 1.92 + // Make sure the next value maps to another index 1.93 + value = array.get_by_address(region_end_address + 1); 1.94 + assert(value == array.default_value(), "must be"); 1.95 + 1.96 + // Reset the value in the array 1.97 + array.set_by_address(region_start_address + (region_end_address - region_start_address) / 2, actual_value); 1.98 + 1.99 + // The entire array should have the default value again 1.100 + for (int* current = bottom; current < end; current++) { 1.101 + assert(*current == array.default_value(), "must be"); 1.102 + } 1.103 + 1.104 + // Set/get by index tests: invert some value 1.105 + idx_t index = NUM_REGIONS / 2; 1.106 + actual_value = array.get_by_index(index); 1.107 + array.set_by_index(index, ~actual_value); 1.108 + 1.109 + value = array.get_by_index(index); 1.110 + assert(value == ~actual_value, "must be"); 1.111 + 1.112 + value = array.get_by_index(index - 1); 1.113 + assert(value == array.default_value(), "must be"); 1.114 + 1.115 + value = array.get_by_index(index + 1); 1.116 + assert(value == array.default_value(), "must be"); 1.117 + 1.118 + array.set_by_index(0, 0); 1.119 + value = array.get_by_index(0); 1.120 + assert(value == 0, "must be"); 1.121 + 1.122 + array.set_by_index(array.length() - 1, 0); 1.123 + value = array.get_by_index(array.length() - 1); 1.124 + assert(value == 0, "must be"); 1.125 + 1.126 + array.set_by_index(index, 0); 1.127 + 1.128 + // The array should have three zeros, and default values otherwise 1.129 + size_t num_zeros = 0; 1.130 + for (int* current = bottom; current < end; current++) { 1.131 + assert(*current == array.default_value() || *current == 0, "must be"); 1.132 + if (*current == 0) { 1.133 + num_zeros++; 1.134 + } 1.135 + } 1.136 + assert(num_zeros == 3, "must be"); 1.137 + } 1.138 +}; 1.139 + 1.140 +void TestG1BiasedArray_test() { 1.141 + TestMappedArray::test_biasedarray(); 1.142 +} 1.143 + 1.144 +#endif