src/share/vm/memory/metachunk.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 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 "memory/allocation.hpp"
aoqi@0 27 #include "memory/metachunk.hpp"
aoqi@0 28 #include "utilities/copy.hpp"
aoqi@0 29 #include "utilities/debug.hpp"
aoqi@0 30
aoqi@0 31 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 32
aoqi@0 33 class VirtualSpaceNode;
aoqi@0 34
aoqi@0 35 const size_t metadata_chunk_initialize = 0xf7f7f7f7;
aoqi@0 36
aoqi@0 37 size_t Metachunk::object_alignment() {
aoqi@0 38 // Must align pointers and sizes to 8,
aoqi@0 39 // so that 64 bit types get correctly aligned.
aoqi@0 40 const size_t alignment = 8;
aoqi@0 41
aoqi@0 42 // Make sure that the Klass alignment also agree.
aoqi@0 43 STATIC_ASSERT(alignment == (size_t)KlassAlignmentInBytes);
aoqi@0 44
aoqi@0 45 return alignment;
aoqi@0 46 }
aoqi@0 47
aoqi@0 48 size_t Metachunk::overhead() {
aoqi@0 49 return align_size_up(sizeof(Metachunk), object_alignment()) / BytesPerWord;
aoqi@0 50 }
aoqi@0 51
aoqi@0 52 // Metachunk methods
aoqi@0 53
aoqi@0 54 Metachunk::Metachunk(size_t word_size,
aoqi@0 55 VirtualSpaceNode* container)
aoqi@0 56 : Metabase<Metachunk>(word_size),
aoqi@0 57 _top(NULL),
aoqi@0 58 _container(container)
aoqi@0 59 {
aoqi@0 60 _top = initial_top();
aoqi@0 61 #ifdef ASSERT
aoqi@0 62 set_is_tagged_free(false);
aoqi@0 63 size_t data_word_size = pointer_delta(end(),
aoqi@0 64 _top,
aoqi@0 65 sizeof(MetaWord));
aoqi@0 66 Copy::fill_to_words((HeapWord*)_top,
aoqi@0 67 data_word_size,
aoqi@0 68 metadata_chunk_initialize);
aoqi@0 69 #endif
aoqi@0 70 }
aoqi@0 71
aoqi@0 72 MetaWord* Metachunk::allocate(size_t word_size) {
aoqi@0 73 MetaWord* result = NULL;
aoqi@0 74 // If available, bump the pointer to allocate.
aoqi@0 75 if (free_word_size() >= word_size) {
aoqi@0 76 result = _top;
aoqi@0 77 _top = _top + word_size;
aoqi@0 78 }
aoqi@0 79 return result;
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 // _bottom points to the start of the chunk including the overhead.
aoqi@0 83 size_t Metachunk::used_word_size() const {
aoqi@0 84 return pointer_delta(_top, bottom(), sizeof(MetaWord));
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 size_t Metachunk::free_word_size() const {
aoqi@0 88 return pointer_delta(end(), _top, sizeof(MetaWord));
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 void Metachunk::print_on(outputStream* st) const {
aoqi@0 92 st->print_cr("Metachunk:"
aoqi@0 93 " bottom " PTR_FORMAT " top " PTR_FORMAT
aoqi@0 94 " end " PTR_FORMAT " size " SIZE_FORMAT,
aoqi@0 95 bottom(), _top, end(), word_size());
aoqi@0 96 if (Verbose) {
aoqi@0 97 st->print_cr(" used " SIZE_FORMAT " free " SIZE_FORMAT,
aoqi@0 98 used_word_size(), free_word_size());
aoqi@0 99 }
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 #ifndef PRODUCT
aoqi@0 103 void Metachunk::mangle() {
aoqi@0 104 // Mangle the payload of the chunk and not the links that
aoqi@0 105 // maintain list of chunks.
aoqi@0 106 HeapWord* start = (HeapWord*)(bottom() + overhead());
aoqi@0 107 size_t size = word_size() - overhead();
aoqi@0 108 Copy::fill_to_words(start, size, metadata_chunk_initialize);
aoqi@0 109 }
aoqi@0 110 #endif // PRODUCT
aoqi@0 111
aoqi@0 112 void Metachunk::verify() {
aoqi@0 113 #ifdef ASSERT
aoqi@0 114 // Cannot walk through the blocks unless the blocks have
aoqi@0 115 // headers with sizes.
aoqi@0 116 assert(bottom() <= _top &&
aoqi@0 117 _top <= (MetaWord*)end(),
aoqi@0 118 "Chunk has been smashed");
aoqi@0 119 #endif
aoqi@0 120 return;
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 /////////////// Unit tests ///////////////
aoqi@0 124
aoqi@0 125 #ifndef PRODUCT
aoqi@0 126
aoqi@0 127 class TestMetachunk {
aoqi@0 128 public:
aoqi@0 129 static void test() {
aoqi@0 130 size_t size = 2 * 1024 * 1024;
aoqi@0 131 void* memory = malloc(size);
aoqi@0 132 assert(memory != NULL, "Failed to malloc 2MB");
aoqi@0 133
aoqi@0 134 Metachunk* metachunk = ::new (memory) Metachunk(size / BytesPerWord, NULL);
aoqi@0 135
aoqi@0 136 assert(metachunk->bottom() == (MetaWord*)metachunk, "assert");
aoqi@0 137 assert(metachunk->end() == (uintptr_t*)metachunk + metachunk->size(), "assert");
aoqi@0 138
aoqi@0 139 // Check sizes
aoqi@0 140 assert(metachunk->size() == metachunk->word_size(), "assert");
aoqi@0 141 assert(metachunk->word_size() == pointer_delta(metachunk->end(), metachunk->bottom(),
aoqi@0 142 sizeof(MetaWord*)), "assert");
aoqi@0 143
aoqi@0 144 // Check usage
aoqi@0 145 assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
aoqi@0 146 assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
aoqi@0 147 assert(metachunk->top() == metachunk->initial_top(), "assert");
aoqi@0 148 assert(metachunk->is_empty(), "assert");
aoqi@0 149
aoqi@0 150 // Allocate
aoqi@0 151 size_t alloc_size = 64; // Words
aoqi@0 152 assert(is_size_aligned(alloc_size, Metachunk::object_alignment()), "assert");
aoqi@0 153
aoqi@0 154 MetaWord* mem = metachunk->allocate(alloc_size);
aoqi@0 155
aoqi@0 156 // Check post alloc
aoqi@0 157 assert(mem == metachunk->initial_top(), "assert");
aoqi@0 158 assert(mem + alloc_size == metachunk->top(), "assert");
aoqi@0 159 assert(metachunk->used_word_size() == metachunk->overhead() + alloc_size, "assert");
aoqi@0 160 assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
aoqi@0 161 assert(!metachunk->is_empty(), "assert");
aoqi@0 162
aoqi@0 163 // Clear chunk
aoqi@0 164 metachunk->reset_empty();
aoqi@0 165
aoqi@0 166 // Check post clear
aoqi@0 167 assert(metachunk->used_word_size() == metachunk->overhead(), "assert");
aoqi@0 168 assert(metachunk->free_word_size() == metachunk->word_size() - metachunk->used_word_size(), "assert");
aoqi@0 169 assert(metachunk->top() == metachunk->initial_top(), "assert");
aoqi@0 170 assert(metachunk->is_empty(), "assert");
aoqi@0 171
aoqi@0 172 free(memory);
aoqi@0 173 }
aoqi@0 174 };
aoqi@0 175
aoqi@0 176 void TestMetachunk_test() {
aoqi@0 177 TestMetachunk::test();
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 #endif

mercurial