jmasa@4327: /* jmasa@4327: * Copyright (c) 2012, 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: jmasa@4327: // jmasa@4327: // Future modification jmasa@4327: // jmasa@4327: // The Metachunk can conceivable be replaced by the Chunk in jmasa@4327: // allocation.hpp. Note that the latter Chunk is the space for jmasa@4327: // allocation (allocations from the chunk are out of the space in jmasa@4327: // the Chunk after the header for the Chunk) where as Metachunks jmasa@4327: // point to space in a VirtualSpace. To replace Metachunks with jmasa@4327: // Chunks, change Chunks so that they can be allocated out of a VirtualSpace. jmasa@4327: jmasa@4327: const size_t metadata_chunk_initialize = 0xf7f7f7f7; jmasa@4327: jmasa@4327: size_t Metachunk::_overhead = jmasa@4327: Chunk::aligned_overhead_size(sizeof(Metachunk)) / BytesPerWord; jmasa@4327: jmasa@4327: // Metachunk methods jmasa@4327: jmasa@4327: Metachunk* Metachunk::initialize(MetaWord* ptr, size_t word_size) { jmasa@4327: // Set bottom, top, and end. Allow space for the Metachunk itself jmasa@4327: Metachunk* chunk = (Metachunk*) ptr; jmasa@4327: jmasa@4327: MetaWord* chunk_bottom = ptr + _overhead; jmasa@4327: chunk->set_bottom(ptr); jmasa@4327: chunk->set_top(chunk_bottom); jmasa@4327: MetaWord* chunk_end = ptr + word_size; jmasa@4327: assert(chunk_end > chunk_bottom, "Chunk must be too small"); jmasa@4327: chunk->set_end(chunk_end); jmasa@4327: chunk->set_next(NULL); jmasa@4382: chunk->set_prev(NULL); jmasa@4327: chunk->set_word_size(word_size); jmasa@4327: #ifdef ASSERT jmasa@4327: size_t data_word_size = pointer_delta(chunk_end, chunk_bottom, sizeof(MetaWord)); jmasa@4327: Copy::fill_to_words((HeapWord*) chunk_bottom, data_word_size, metadata_chunk_initialize); jmasa@4327: #endif jmasa@4327: return chunk; jmasa@4327: } 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 { jmasa@4327: return pointer_delta(_top, _bottom, sizeof(MetaWord)); jmasa@4327: } jmasa@4327: jmasa@4382: size_t Metachunk::free_word_size() const { jmasa@4327: return pointer_delta(_end, _top, sizeof(MetaWord)); jmasa@4327: } jmasa@4327: jmasa@4382: size_t Metachunk::capacity_word_size() const { jmasa@4327: return pointer_delta(_end, _bottom, 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, jmasa@4327: 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()); jmasa@4327: size_t word_size = capacity_word_size() - overhead(); jmasa@4327: Copy::fill_to_words(start, word_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. jmasa@4327: assert(_bottom <= _top && jmasa@4327: _top <= _end, jmasa@4327: "Chunk has been smashed"); jmasa@4327: #endif jmasa@4327: return; jmasa@4327: }