jmasa@4327: /* drchase@6680: * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. jmasa@4327: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jmasa@4327: * jmasa@4327: * This code is free software; you can redistribute it and/or modify it jmasa@4327: * under the terms of the GNU General Public License version 2 only, as jmasa@4327: * published by the Free Software Foundation. jmasa@4327: * jmasa@4327: * This code is distributed in the hope that it will be useful, but WITHOUT jmasa@4327: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jmasa@4327: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jmasa@4327: * version 2 for more details (a copy is included in the LICENSE file that jmasa@4327: * accompanied this code). jmasa@4327: * jmasa@4327: * You should have received a copy of the GNU General Public License version jmasa@4327: * 2 along with this work; if not, write to the Free Software Foundation, jmasa@4327: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jmasa@4327: * jmasa@4327: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jmasa@4327: * or visit www.oracle.com if you need additional information or have any jmasa@4327: * questions. jmasa@4327: * jmasa@4327: */ jmasa@4327: jmasa@4327: #include "precompiled.hpp" jmasa@4327: #include "memory/allocation.hpp" jmasa@4327: #include "memory/metachunk.hpp" jmasa@4327: #include "utilities/copy.hpp" jmasa@4327: #include "utilities/debug.hpp" jmasa@4327: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: jmasa@5007: class VirtualSpaceNode; jmasa@4327: jmasa@4327: const size_t metadata_chunk_initialize = 0xf7f7f7f7; jmasa@4327: stefank@5941: size_t Metachunk::object_alignment() { stefank@5942: // Must align pointers and sizes to 8, stefank@5942: // so that 64 bit types get correctly aligned. stefank@5942: const size_t alignment = 8; stefank@5942: stefank@5942: // Make sure that the Klass alignment also agree. stefank@5942: STATIC_ASSERT(alignment == (size_t)KlassAlignmentInBytes); stefank@5942: stefank@5942: return alignment; stefank@5941: } stefank@5941: stefank@5941: size_t Metachunk::overhead() { stefank@5941: return align_size_up(sizeof(Metachunk), object_alignment()) / BytesPerWord; stefank@5941: } jmasa@4327: jmasa@4327: // Metachunk methods jmasa@4327: jmasa@5007: Metachunk::Metachunk(size_t word_size, stefank@5941: VirtualSpaceNode* container) stefank@5941: : Metabase(word_size), jmasa@5007: _top(NULL), jmasa@5007: _container(container) jmasa@5007: { stefank@5941: _top = initial_top(); jmasa@4327: #ifdef ASSERT stefank@5941: set_is_tagged_free(false); jmasa@5007: size_t data_word_size = pointer_delta(end(), stefank@5941: _top, jmasa@5007: sizeof(MetaWord)); stefank@5941: Copy::fill_to_words((HeapWord*)_top, jmasa@5007: data_word_size, jmasa@5007: metadata_chunk_initialize); jmasa@4327: #endif jmasa@4327: } jmasa@4327: jmasa@4327: MetaWord* Metachunk::allocate(size_t word_size) { jmasa@4327: MetaWord* result = NULL; jmasa@4327: // If available, bump the pointer to allocate. jmasa@4327: if (free_word_size() >= word_size) { jmasa@4327: result = _top; jmasa@4327: _top = _top + word_size; jmasa@4327: } jmasa@4327: return result; jmasa@4327: } jmasa@4327: jmasa@4327: // _bottom points to the start of the chunk including the overhead. jmasa@4382: size_t Metachunk::used_word_size() const { stefank@5941: return pointer_delta(_top, bottom(), sizeof(MetaWord)); jmasa@4327: } jmasa@4327: jmasa@4382: size_t Metachunk::free_word_size() const { stefank@5941: return pointer_delta(end(), _top, sizeof(MetaWord)); jmasa@4327: } jmasa@4327: jmasa@4327: void Metachunk::print_on(outputStream* st) const { jmasa@4327: st->print_cr("Metachunk:" jmasa@4327: " bottom " PTR_FORMAT " top " PTR_FORMAT jmasa@4327: " end " PTR_FORMAT " size " SIZE_FORMAT, stefank@5941: bottom(), _top, end(), word_size()); jmasa@4382: if (Verbose) { jmasa@4382: st->print_cr(" used " SIZE_FORMAT " free " SIZE_FORMAT, jmasa@4382: used_word_size(), free_word_size()); jmasa@4382: } jmasa@4327: } jmasa@4327: jmasa@4327: #ifndef PRODUCT jmasa@4327: void Metachunk::mangle() { jmasa@4327: // Mangle the payload of the chunk and not the links that jmasa@4327: // maintain list of chunks. jmasa@4327: HeapWord* start = (HeapWord*)(bottom() + overhead()); stefank@5941: size_t size = word_size() - overhead(); stefank@5941: Copy::fill_to_words(start, size, metadata_chunk_initialize); jmasa@4327: } jmasa@4327: #endif // PRODUCT jmasa@4327: jmasa@4327: void Metachunk::verify() { jmasa@4327: #ifdef ASSERT jmasa@4327: // Cannot walk through the blocks unless the blocks have jmasa@4327: // headers with sizes. stefank@5941: assert(bottom() <= _top && stefank@5941: _top <= (MetaWord*)end(), jmasa@4327: "Chunk has been smashed"); jmasa@4327: #endif jmasa@4327: return; jmasa@4327: } stefank@5941: stefank@5941: /////////////// Unit tests /////////////// stefank@5941: stefank@5941: #ifndef PRODUCT stefank@5941: stefank@5941: class TestMetachunk { stefank@5941: public: stefank@5941: static void test() { stefank@5941: size_t size = 2 * 1024 * 1024; stefank@5941: void* memory = malloc(size); stefank@5941: assert(memory != NULL, "Failed to malloc 2MB"); stefank@5941: stefank@5941: Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL); stefank@5941: stefank@5941: assert(metachunk->bottom() == (MetaWord*)metachunk, "assert"); stefank@5941: assert(metachunk->end() == (uintptr_t*)metachunk + metachunk->size(), "assert"); stefank@5941: stefank@5941: // Check sizes stefank@5941: assert(metachunk->size() == metachunk->word_size(), "assert"); stefank@5941: assert(metachunk->word_size() == pointer_delta(metachunk->end(), metachunk->bottom(), stefank@5941: sizeof(MetaWord*)), "assert"); stefank@5941: stefank@5941: // Check usage stefank@5941: assert(metachunk->used_word_size() == metachunk->overhead(), "assert"); stefank@5941: assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert"); stefank@5941: assert(metachunk->top() == metachunk->initial_top(), "assert"); stefank@5941: assert(metachunk->is_empty(), "assert"); stefank@5941: stefank@5941: // Allocate stefank@5941: size_t alloc_size = 64; // Words stefank@5941: assert(is_size_aligned(alloc_size, Metachunk::object_alignment()), "assert"); stefank@5941: stefank@5941: MetaWord* mem = metachunk->allocate(alloc_size); stefank@5941: stefank@5941: // Check post alloc stefank@5941: assert(mem == metachunk->initial_top(), "assert"); stefank@5941: assert(mem + alloc_size == metachunk->top(), "assert"); stefank@5941: assert(metachunk->used_word_size() == metachunk->overhead() + alloc_size, "assert"); stefank@5941: assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert"); stefank@5941: assert(!metachunk->is_empty(), "assert"); stefank@5941: stefank@5941: // Clear chunk stefank@5941: metachunk->reset_empty(); stefank@5941: stefank@5941: // Check post clear stefank@5941: assert(metachunk->used_word_size() == metachunk->overhead(), "assert"); stefank@5941: assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert"); stefank@5941: assert(metachunk->top() == metachunk->initial_top(), "assert"); stefank@5941: assert(metachunk->is_empty(), "assert"); stefank@5941: stefank@5941: free(memory); stefank@5941: } stefank@5941: }; stefank@5941: stefank@5941: void TestMetachunk_test() { stefank@5941: TestMetachunk::test(); stefank@5941: } stefank@5941: stefank@5941: #endif