tschatzl@5773: /* drchase@6680: * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. tschatzl@5773: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. tschatzl@5773: * tschatzl@5773: * This code is free software; you can redistribute it and/or modify it tschatzl@5773: * under the terms of the GNU General Public License version 2 only, as tschatzl@5773: * published by the Free Software Foundation. tschatzl@5773: * tschatzl@5773: * This code is distributed in the hope that it will be useful, but WITHOUT tschatzl@5773: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or tschatzl@5773: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License tschatzl@5773: * version 2 for more details (a copy is included in the LICENSE file that tschatzl@5773: * accompanied this code). tschatzl@5773: * tschatzl@5773: * You should have received a copy of the GNU General Public License version tschatzl@5773: * 2 along with this work; if not, write to the Free Software Foundation, tschatzl@5773: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. tschatzl@5773: * tschatzl@5773: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA tschatzl@5773: * or visit www.oracle.com if you need additional information or have any tschatzl@5773: * questions. tschatzl@5773: * tschatzl@5773: */ tschatzl@5773: tschatzl@5773: #include "precompiled.hpp" tschatzl@5773: #include "gc_implementation/g1/g1BiasedArray.hpp" tschatzl@6409: #include "memory/padded.inline.hpp" tschatzl@6409: tschatzl@6409: // Allocate a new array, generic version. tschatzl@6409: address G1BiasedMappedArrayBase::create_new_base_array(size_t length, size_t elem_size) { tschatzl@6409: assert(length > 0, "just checking"); tschatzl@6409: assert(elem_size > 0, "just checking"); tschatzl@6409: return PaddedPrimitiveArray::create_unfreeable(length * elem_size); tschatzl@6409: } tschatzl@5773: tschatzl@5773: #ifndef PRODUCT tschatzl@5773: void G1BiasedMappedArrayBase::verify_index(idx_t index) const { tschatzl@5773: guarantee(_base != NULL, "Array not initialized"); tschatzl@5773: guarantee(index < length(), err_msg("Index out of bounds index: "SIZE_FORMAT" length: "SIZE_FORMAT, index, length())); tschatzl@5773: } tschatzl@5773: tschatzl@5773: void G1BiasedMappedArrayBase::verify_biased_index(idx_t biased_index) const { tschatzl@5773: guarantee(_biased_base != NULL, "Array not initialized"); tschatzl@5773: guarantee(biased_index >= bias() && biased_index < (bias() + length()), tschatzl@5773: err_msg("Biased index out of bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length())); tschatzl@5773: } tschatzl@5773: tschatzl@5773: void G1BiasedMappedArrayBase::verify_biased_index_inclusive_end(idx_t biased_index) const { tschatzl@5773: guarantee(_biased_base != NULL, "Array not initialized"); tschatzl@5773: guarantee(biased_index >= bias() && biased_index <= (bias() + length()), tschatzl@5773: err_msg("Biased index out of inclusive bounds, index: "SIZE_FORMAT" bias: "SIZE_FORMAT" length: "SIZE_FORMAT, biased_index, bias(), length())); tschatzl@5773: } tschatzl@5773: tschatzl@5773: class TestMappedArray : public G1BiasedMappedArray { tschatzl@5773: protected: tschatzl@5773: virtual int default_value() const { return 0xBAADBABE; } tschatzl@5773: public: tschatzl@5773: static void test_biasedarray() { tschatzl@5773: const size_t REGION_SIZE_IN_WORDS = 512; tschatzl@5773: const size_t NUM_REGIONS = 20; tschatzl@5773: HeapWord* fake_heap = (HeapWord*)LP64_ONLY(0xBAAA00000) NOT_LP64(0xBA000000); // Any value that is non-zero tschatzl@5773: tschatzl@5773: TestMappedArray array; tschatzl@5773: array.initialize(fake_heap, fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS, tschatzl@5773: REGION_SIZE_IN_WORDS * HeapWordSize); tschatzl@5773: // Check address calculation (bounds) tschatzl@5773: assert(array.bottom_address_mapped() == fake_heap, drchase@6680: err_msg("bottom mapped address should be " PTR_FORMAT ", but is " PTR_FORMAT, p2i(fake_heap), p2i(array.bottom_address_mapped()))); tschatzl@5773: assert(array.end_address_mapped() == (fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS), "must be"); tschatzl@5773: tschatzl@5773: int* bottom = array.address_mapped_to(fake_heap); tschatzl@5773: assert((void*)bottom == (void*) array.base(), "must be"); tschatzl@5773: int* end = array.address_mapped_to(fake_heap + REGION_SIZE_IN_WORDS * NUM_REGIONS); tschatzl@5773: assert((void*)end == (void*)(array.base() + array.length()), "must be"); tschatzl@5773: // The entire array should contain default value elements tschatzl@5773: for (int* current = bottom; current < end; current++) { tschatzl@5773: assert(*current == array.default_value(), "must be"); tschatzl@5773: } tschatzl@5773: tschatzl@5773: // Test setting values in the table tschatzl@5773: tschatzl@5773: HeapWord* region_start_address = fake_heap + REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2); tschatzl@5773: HeapWord* region_end_address = fake_heap + (REGION_SIZE_IN_WORDS * (NUM_REGIONS / 2) + REGION_SIZE_IN_WORDS - 1); tschatzl@5773: tschatzl@5773: // Set/get by address tests: invert some value; first retrieve one tschatzl@5773: int actual_value = array.get_by_index(NUM_REGIONS / 2); tschatzl@5773: array.set_by_index(NUM_REGIONS / 2, ~actual_value); tschatzl@5773: // Get the same value by address, should correspond to the start of the "region" tschatzl@5773: int value = array.get_by_address(region_start_address); tschatzl@5773: assert(value == ~actual_value, "must be"); tschatzl@5773: // Get the same value by address, at one HeapWord before the start tschatzl@5773: value = array.get_by_address(region_start_address - 1); tschatzl@5773: assert(value == array.default_value(), "must be"); tschatzl@5773: // Get the same value by address, at the end of the "region" tschatzl@5773: value = array.get_by_address(region_end_address); tschatzl@5773: assert(value == ~actual_value, "must be"); tschatzl@5773: // Make sure the next value maps to another index tschatzl@5773: value = array.get_by_address(region_end_address + 1); tschatzl@5773: assert(value == array.default_value(), "must be"); tschatzl@5773: tschatzl@5773: // Reset the value in the array tschatzl@5773: array.set_by_address(region_start_address + (region_end_address - region_start_address) / 2, actual_value); tschatzl@5773: tschatzl@5773: // The entire array should have the default value again tschatzl@5773: for (int* current = bottom; current < end; current++) { tschatzl@5773: assert(*current == array.default_value(), "must be"); tschatzl@5773: } tschatzl@5773: tschatzl@5773: // Set/get by index tests: invert some value tschatzl@5773: idx_t index = NUM_REGIONS / 2; tschatzl@5773: actual_value = array.get_by_index(index); tschatzl@5773: array.set_by_index(index, ~actual_value); tschatzl@5773: tschatzl@5773: value = array.get_by_index(index); tschatzl@5773: assert(value == ~actual_value, "must be"); tschatzl@5773: tschatzl@5773: value = array.get_by_index(index - 1); tschatzl@5773: assert(value == array.default_value(), "must be"); tschatzl@5773: tschatzl@5773: value = array.get_by_index(index + 1); tschatzl@5773: assert(value == array.default_value(), "must be"); tschatzl@5773: tschatzl@5773: array.set_by_index(0, 0); tschatzl@5773: value = array.get_by_index(0); tschatzl@5773: assert(value == 0, "must be"); tschatzl@5773: tschatzl@5773: array.set_by_index(array.length() - 1, 0); tschatzl@5773: value = array.get_by_index(array.length() - 1); tschatzl@5773: assert(value == 0, "must be"); tschatzl@5773: tschatzl@5773: array.set_by_index(index, 0); tschatzl@5773: tschatzl@5773: // The array should have three zeros, and default values otherwise tschatzl@5773: size_t num_zeros = 0; tschatzl@5773: for (int* current = bottom; current < end; current++) { tschatzl@5773: assert(*current == array.default_value() || *current == 0, "must be"); tschatzl@5773: if (*current == 0) { tschatzl@5773: num_zeros++; tschatzl@5773: } tschatzl@5773: } tschatzl@5773: assert(num_zeros == 3, "must be"); tschatzl@5773: } tschatzl@5773: }; tschatzl@5773: tschatzl@5773: void TestG1BiasedArray_test() { tschatzl@5773: TestMappedArray::test_biasedarray(); tschatzl@5773: } tschatzl@5773: tschatzl@5773: #endif