src/share/vm/memory/metachunk.cpp

Tue, 24 Feb 2015 15:04:52 -0500

author
dlong
date
Tue, 24 Feb 2015 15:04:52 -0500
changeset 7598
ddce0b7cee93
parent 6680
78bbf4d43a14
child 6876
710a3c8b516e
permissions
-rw-r--r--

8072383: resolve conflicts between open and closed ports
Summary: refactor close to remove references to closed ports
Reviewed-by: kvn, simonis, sgehwolf, dholmes

jmasa@4327 1 /*
drchase@6680 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
jmasa@4327 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jmasa@4327 4 *
jmasa@4327 5 * This code is free software; you can redistribute it and/or modify it
jmasa@4327 6 * under the terms of the GNU General Public License version 2 only, as
jmasa@4327 7 * published by the Free Software Foundation.
jmasa@4327 8 *
jmasa@4327 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jmasa@4327 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jmasa@4327 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jmasa@4327 12 * version 2 for more details (a copy is included in the LICENSE file that
jmasa@4327 13 * accompanied this code).
jmasa@4327 14 *
jmasa@4327 15 * You should have received a copy of the GNU General Public License version
jmasa@4327 16 * 2 along with this work; if not, write to the Free Software Foundation,
jmasa@4327 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jmasa@4327 18 *
jmasa@4327 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jmasa@4327 20 * or visit www.oracle.com if you need additional information or have any
jmasa@4327 21 * questions.
jmasa@4327 22 *
jmasa@4327 23 */
jmasa@4327 24
jmasa@4327 25 #include "precompiled.hpp"
jmasa@4327 26 #include "memory/allocation.hpp"
jmasa@4327 27 #include "memory/metachunk.hpp"
jmasa@4327 28 #include "utilities/copy.hpp"
jmasa@4327 29 #include "utilities/debug.hpp"
jmasa@4327 30
drchase@6680 31 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 32
jmasa@5007 33 class VirtualSpaceNode;
jmasa@4327 34
jmasa@4327 35 const size_t metadata_chunk_initialize = 0xf7f7f7f7;
jmasa@4327 36
stefank@5941 37 size_t Metachunk::object_alignment() {
stefank@5942 38 // Must align pointers and sizes to 8,
stefank@5942 39 // so that 64 bit types get correctly aligned.
stefank@5942 40 const size_t alignment = 8;
stefank@5942 41
stefank@5942 42 // Make sure that the Klass alignment also agree.
stefank@5942 43 STATIC_ASSERT(alignment == (size_t)KlassAlignmentInBytes);
stefank@5942 44
stefank@5942 45 return alignment;
stefank@5941 46 }
stefank@5941 47
stefank@5941 48 size_t Metachunk::overhead() {
stefank@5941 49 return align_size_up(sizeof(Metachunk), object_alignment()) / BytesPerWord;
stefank@5941 50 }
jmasa@4327 51
jmasa@4327 52 // Metachunk methods
jmasa@4327 53
jmasa@5007 54 Metachunk::Metachunk(size_t word_size,
stefank@5941 55 VirtualSpaceNode* container)
stefank@5941 56 : Metabase<Metachunk>(word_size),
jmasa@5007 57 _top(NULL),
jmasa@5007 58 _container(container)
jmasa@5007 59 {
stefank@5941 60 _top = initial_top();
jmasa@4327 61 #ifdef ASSERT
stefank@5941 62 set_is_tagged_free(false);
jmasa@5007 63 size_t data_word_size = pointer_delta(end(),
stefank@5941 64 _top,
jmasa@5007 65 sizeof(MetaWord));
stefank@5941 66 Copy::fill_to_words((HeapWord*)_top,
jmasa@5007 67 data_word_size,
jmasa@5007 68 metadata_chunk_initialize);
jmasa@4327 69 #endif
jmasa@4327 70 }
jmasa@4327 71
jmasa@4327 72 MetaWord* Metachunk::allocate(size_t word_size) {
jmasa@4327 73 MetaWord* result = NULL;
jmasa@4327 74 // If available, bump the pointer to allocate.
jmasa@4327 75 if (free_word_size() >= word_size) {
jmasa@4327 76 result = _top;
jmasa@4327 77 _top = _top + word_size;
jmasa@4327 78 }
jmasa@4327 79 return result;
jmasa@4327 80 }
jmasa@4327 81
jmasa@4327 82 // _bottom points to the start of the chunk including the overhead.
jmasa@4382 83 size_t Metachunk::used_word_size() const {
stefank@5941 84 return pointer_delta(_top, bottom(), sizeof(MetaWord));
jmasa@4327 85 }
jmasa@4327 86
jmasa@4382 87 size_t Metachunk::free_word_size() const {
stefank@5941 88 return pointer_delta(end(), _top, sizeof(MetaWord));
jmasa@4327 89 }
jmasa@4327 90
jmasa@4327 91 void Metachunk::print_on(outputStream* st) const {
jmasa@4327 92 st->print_cr("Metachunk:"
jmasa@4327 93 " bottom " PTR_FORMAT " top " PTR_FORMAT
jmasa@4327 94 " end " PTR_FORMAT " size " SIZE_FORMAT,
stefank@5941 95 bottom(), _top, end(), word_size());
jmasa@4382 96 if (Verbose) {
jmasa@4382 97 st->print_cr(" used " SIZE_FORMAT " free " SIZE_FORMAT,
jmasa@4382 98 used_word_size(), free_word_size());
jmasa@4382 99 }
jmasa@4327 100 }
jmasa@4327 101
jmasa@4327 102 #ifndef PRODUCT
jmasa@4327 103 void Metachunk::mangle() {
jmasa@4327 104 // Mangle the payload of the chunk and not the links that
jmasa@4327 105 // maintain list of chunks.
jmasa@4327 106 HeapWord* start = (HeapWord*)(bottom() + overhead());
stefank@5941 107 size_t size = word_size() - overhead();
stefank@5941 108 Copy::fill_to_words(start, size, metadata_chunk_initialize);
jmasa@4327 109 }
jmasa@4327 110 #endif // PRODUCT
jmasa@4327 111
jmasa@4327 112 void Metachunk::verify() {
jmasa@4327 113 #ifdef ASSERT
jmasa@4327 114 // Cannot walk through the blocks unless the blocks have
jmasa@4327 115 // headers with sizes.
stefank@5941 116 assert(bottom() <= _top &&
stefank@5941 117 _top <= (MetaWord*)end(),
jmasa@4327 118 "Chunk has been smashed");
jmasa@4327 119 #endif
jmasa@4327 120 return;
jmasa@4327 121 }
stefank@5941 122
stefank@5941 123 /////////////// Unit tests ///////////////
stefank@5941 124
stefank@5941 125 #ifndef PRODUCT
stefank@5941 126
stefank@5941 127 class TestMetachunk {
stefank@5941 128 public:
stefank@5941 129 static void test() {
stefank@5941 130 size_t size = 2 * 1024 * 1024;
stefank@5941 131 void* memory = malloc(size);
stefank@5941 132 assert(memory != NULL, "Failed to malloc 2MB");
stefank@5941 133
stefank@5941 134 Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL);
stefank@5941 135
stefank@5941 136 assert(metachunk->bottom() == (MetaWord*)metachunk, "assert");
stefank@5941 137 assert(metachunk->end() == (uintptr_t*)metachunk + metachunk->size(), "assert");
stefank@5941 138
stefank@5941 139 // Check sizes
stefank@5941 140 assert(metachunk->size() == metachunk->word_size(), "assert");
stefank@5941 141 assert(metachunk->word_size() == pointer_delta(metachunk->end(), metachunk->bottom(),
stefank@5941 142 sizeof(MetaWord*)), "assert");
stefank@5941 143
stefank@5941 144 // Check usage
stefank@5941 145 assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
stefank@5941 146 assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
stefank@5941 147 assert(metachunk->top() == metachunk->initial_top(), "assert");
stefank@5941 148 assert(metachunk->is_empty(), "assert");
stefank@5941 149
stefank@5941 150 // Allocate
stefank@5941 151 size_t alloc_size = 64; // Words
stefank@5941 152 assert(is_size_aligned(alloc_size, Metachunk::object_alignment()), "assert");
stefank@5941 153
stefank@5941 154 MetaWord* mem = metachunk->allocate(alloc_size);
stefank@5941 155
stefank@5941 156 // Check post alloc
stefank@5941 157 assert(mem == metachunk->initial_top(), "assert");
stefank@5941 158 assert(mem + alloc_size == metachunk->top(), "assert");
stefank@5941 159 assert(metachunk->used_word_size() == metachunk->overhead() + alloc_size, "assert");
stefank@5941 160 assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
stefank@5941 161 assert(!metachunk->is_empty(), "assert");
stefank@5941 162
stefank@5941 163 // Clear chunk
stefank@5941 164 metachunk->reset_empty();
stefank@5941 165
stefank@5941 166 // Check post clear
stefank@5941 167 assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
stefank@5941 168 assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
stefank@5941 169 assert(metachunk->top() == metachunk->initial_top(), "assert");
stefank@5941 170 assert(metachunk->is_empty(), "assert");
stefank@5941 171
stefank@5941 172 free(memory);
stefank@5941 173 }
stefank@5941 174 };
stefank@5941 175
stefank@5941 176 void TestMetachunk_test() {
stefank@5941 177 TestMetachunk::test();
stefank@5941 178 }
stefank@5941 179
stefank@5941 180 #endif

mercurial