Tue, 08 Aug 2017 15:57:29 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #include "precompiled.hpp" |
aoqi@0 | 26 | #include "gc_implementation/g1/g1BiasedArray.hpp" |
aoqi@0 | 27 | #include "memory/padded.inline.hpp" |
aoqi@0 | 28 | |
aoqi@0 | 29 | // Allocate a new array, generic version. |
aoqi@0 | 30 | address G1BiasedMappedArrayBase::create_new_base_array(size_t length, size_t elem_size) { |
aoqi@0 | 31 | assert(length > 0, "just checking"); |
aoqi@0 | 32 | assert(elem_size > 0, "just checking"); |
aoqi@0 | 33 | return PaddedPrimitiveArray<u_char, mtGC>::create_unfreeable(length * elem_size); |
aoqi@0 | 34 | } |
aoqi@0 | 35 | |
aoqi@0 | 36 | #ifndef PRODUCT |
aoqi@0 | 37 | void G1BiasedMappedArrayBase::verify_index(idx_t index) const { |
aoqi@0 | 38 | guarantee(_base != NULL, "Array not initialized"); |
aoqi@0 | 39 | guarantee(index < length(), err_msg("Index out of bounds index: "SIZE_FORMAT" length: "SIZE_FORMAT, index, length())); |
aoqi@0 | 40 | } |
aoqi@0 | 41 | |
aoqi@0 | 42 | void G1BiasedMappedArrayBase::verify_biased_index(idx_t biased_index) const { |
aoqi@0 | 43 | guarantee(_biased_base != NULL, "Array not initialized"); |
aoqi@0 | 44 | guarantee(biased_index >= bias() && biased_index < (bias() + length()), |
aoqi@0 | 45 | err_msg("Biased index out of bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length())); |
aoqi@0 | 46 | } |
aoqi@0 | 47 | |
aoqi@0 | 48 | void G1BiasedMappedArrayBase::verify_biased_index_inclusive_end(idx_t biased_index) const { |
aoqi@0 | 49 | guarantee(_biased_base != NULL, "Array not initialized"); |
aoqi@0 | 50 | guarantee(biased_index >= bias() && biased_index <= (bias() + length()), |
aoqi@0 | 51 | err_msg("Biased index out of inclusive bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length())); |
aoqi@0 | 52 | } |
aoqi@0 | 53 | |
aoqi@0 | 54 | class TestMappedArray : public G1BiasedMappedArray<int> { |
aoqi@0 | 55 | protected: |
aoqi@0 | 56 | virtual int default_value() const { return 0xBAADBABE; } |
aoqi@0 | 57 | public: |
aoqi@0 | 58 | static void test_biasedarray() { |
aoqi@0 | 59 | const size_t REGION_SIZE_IN_WORDS = 512; |
aoqi@0 | 60 | const size_t NUM_REGIONS = 20; |
aoqi@0 | 61 | HeapWord* fake_heap = (HeapWord*)LP64_ONLY(0xBAAA00000) NOT_LP64(0xBA000000); // Any value that is non-zero |
aoqi@0 | 62 | |
aoqi@0 | 63 | TestMappedArray array; |
aoqi@0 | 64 | array.initialize(fake_heap, fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS, |
aoqi@0 | 65 | REGION_SIZE_IN_WORDS * HeapWordSize); |
aoqi@0 | 66 | // Check address calculation (bounds) |
aoqi@0 | 67 | assert(array.bottom_address_mapped() == fake_heap, |
aoqi@0 | 68 | err_msg("bottom mapped address should be " PTR_FORMAT ", but is " PTR_FORMAT, p2i(fake_heap), p2i(array.bottom_address_mapped()))); |
aoqi@0 | 69 | assert(array.end_address_mapped() == (fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS), "must be"); |
aoqi@0 | 70 | |
aoqi@0 | 71 | int* bottom = array.address_mapped_to(fake_heap); |
aoqi@0 | 72 | assert((void*)bottom == (void*) array.base(), "must be"); |
aoqi@0 | 73 | int* end = array.address_mapped_to(fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS); |
aoqi@0 | 74 | assert((void*)end == (void*)(array.base() + array.length()), "must be"); |
aoqi@0 | 75 | // The entire array should contain default value elements |
aoqi@0 | 76 | for (int* current = bottom; current < end; current++) { |
aoqi@0 | 77 | assert(*current == array.default_value(), "must be"); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | // Test setting values in the table |
aoqi@0 | 81 | |
aoqi@0 | 82 | HeapWord* region_start_address = fake_heap + REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2); |
aoqi@0 | 83 | HeapWord* region_end_address = fake_heap + (REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2) + REGION_SIZE_IN_WORDS - 1); |
aoqi@0 | 84 | |
aoqi@0 | 85 | // Set/get by address tests: invert some value; first retrieve one |
aoqi@0 | 86 | int actual_value = array.get_by_index(NUM_REGIONS / 2); |
aoqi@0 | 87 | array.set_by_index(NUM_REGIONS / 2, ~actual_value); |
aoqi@0 | 88 | // Get the same value by address, should correspond to the start of the "region" |
aoqi@0 | 89 | int value = array.get_by_address(region_start_address); |
aoqi@0 | 90 | assert(value == ~actual_value, "must be"); |
aoqi@0 | 91 | // Get the same value by address, at one HeapWord before the start |
aoqi@0 | 92 | value = array.get_by_address(region_start_address - 1); |
aoqi@0 | 93 | assert(value == array.default_value(), "must be"); |
aoqi@0 | 94 | // Get the same value by address, at the end of the "region" |
aoqi@0 | 95 | value = array.get_by_address(region_end_address); |
aoqi@0 | 96 | assert(value == ~actual_value, "must be"); |
aoqi@0 | 97 | // Make sure the next value maps to another index |
aoqi@0 | 98 | value = array.get_by_address(region_end_address + 1); |
aoqi@0 | 99 | assert(value == array.default_value(), "must be"); |
aoqi@0 | 100 | |
aoqi@0 | 101 | // Reset the value in the array |
aoqi@0 | 102 | array.set_by_address(region_start_address + (region_end_address - region_start_address) / 2, actual_value); |
aoqi@0 | 103 | |
aoqi@0 | 104 | // The entire array should have the default value again |
aoqi@0 | 105 | for (int* current = bottom; current < end; current++) { |
aoqi@0 | 106 | assert(*current == array.default_value(), "must be"); |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | // Set/get by index tests: invert some value |
aoqi@0 | 110 | idx_t index = NUM_REGIONS / 2; |
aoqi@0 | 111 | actual_value = array.get_by_index(index); |
aoqi@0 | 112 | array.set_by_index(index, ~actual_value); |
aoqi@0 | 113 | |
aoqi@0 | 114 | value = array.get_by_index(index); |
aoqi@0 | 115 | assert(value == ~actual_value, "must be"); |
aoqi@0 | 116 | |
aoqi@0 | 117 | value = array.get_by_index(index - 1); |
aoqi@0 | 118 | assert(value == array.default_value(), "must be"); |
aoqi@0 | 119 | |
aoqi@0 | 120 | value = array.get_by_index(index + 1); |
aoqi@0 | 121 | assert(value == array.default_value(), "must be"); |
aoqi@0 | 122 | |
aoqi@0 | 123 | array.set_by_index(0, 0); |
aoqi@0 | 124 | value = array.get_by_index(0); |
aoqi@0 | 125 | assert(value == 0, "must be"); |
aoqi@0 | 126 | |
aoqi@0 | 127 | array.set_by_index(array.length() - 1, 0); |
aoqi@0 | 128 | value = array.get_by_index(array.length() - 1); |
aoqi@0 | 129 | assert(value == 0, "must be"); |
aoqi@0 | 130 | |
aoqi@0 | 131 | array.set_by_index(index, 0); |
aoqi@0 | 132 | |
aoqi@0 | 133 | // The array should have three zeros, and default values otherwise |
aoqi@0 | 134 | size_t num_zeros = 0; |
aoqi@0 | 135 | for (int* current = bottom; current < end; current++) { |
aoqi@0 | 136 | assert(*current == array.default_value() || *current == 0, "must be"); |
aoqi@0 | 137 | if (*current == 0) { |
aoqi@0 | 138 | num_zeros++; |
aoqi@0 | 139 | } |
aoqi@0 | 140 | } |
aoqi@0 | 141 | assert(num_zeros == 3, "must be"); |
aoqi@0 | 142 | } |
aoqi@0 | 143 | }; |
aoqi@0 | 144 | |
aoqi@0 | 145 | void TestG1BiasedArray_test() { |
aoqi@0 | 146 | TestMappedArray::test_biasedarray(); |
aoqi@0 | 147 | } |
aoqi@0 | 148 | |
aoqi@0 | 149 | #endif |