1 /* 2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "memory/allocation.hpp" 27 #include "memory/metaspace/metachunk.hpp" 28 #include "memory/metaspace/occupancyMap.hpp" 29 #include "memory/metaspace/virtualSpaceNode.hpp" 30 #include "utilities/align.hpp" 31 #include "utilities/copy.hpp" 32 #include "utilities/debug.hpp" 33 34 namespace metaspace { 35 namespace internals { 36 37 size_t Metachunk::object_alignment() { 38 // Must align pointers and sizes to 8, 39 // so that 64 bit types get correctly aligned. 40 const size_t alignment = 8; 41 42 // Make sure that the Klass alignment also agree. 43 STATIC_ASSERT(alignment == (size_t)KlassAlignmentInBytes); 44 45 return alignment; 46 } 47 48 size_t Metachunk::overhead() { 49 return align_up(sizeof(Metachunk), object_alignment()) / BytesPerWord; 50 } 51 52 // Metachunk methods 53 54 Metachunk::Metachunk(ChunkIndex chunktype, bool is_class, size_t word_size, 55 VirtualSpaceNode* container) 56 : Metabase<Metachunk>(word_size), 57 _chunk_type(chunktype), 58 _is_class(is_class), 59 _sentinel(CHUNK_SENTINEL), 60 _origin(origin_normal), 61 _use_count(0), 62 _top(NULL), 63 _container(container) 64 { 65 _top = initial_top(); 66 set_is_tagged_free(false); 67 #ifdef ASSERT 68 mangle(uninitMetaWordVal); 69 verify(); 70 #endif 71 } 72 73 MetaWord* Metachunk::allocate(size_t word_size) { 74 MetaWord* result = NULL; 75 // If available, bump the pointer to allocate. 76 if (free_word_size() >= word_size) { 77 result = _top; 78 _top = _top + word_size; 79 } 80 return result; 81 } 82 83 // _bottom points to the start of the chunk including the overhead. 84 size_t Metachunk::used_word_size() const { 85 return pointer_delta(_top, bottom(), sizeof(MetaWord)); 86 } 87 88 size_t Metachunk::free_word_size() const { 89 return pointer_delta(end(), _top, sizeof(MetaWord)); 90 } 91 92 void Metachunk::print_on(outputStream* st) const { 93 st->print_cr("Metachunk:" 94 " bottom " PTR_FORMAT " top " PTR_FORMAT 95 " end " PTR_FORMAT " size " SIZE_FORMAT " (%s)", 96 p2i(bottom()), p2i(_top), p2i(end()), word_size(), 97 chunk_size_name(get_chunk_type())); 98 if (Verbose) { 99 st->print_cr(" used " SIZE_FORMAT " free " SIZE_FORMAT, 100 used_word_size(), free_word_size()); 101 } 102 } 103 104 #ifdef ASSERT 105 void Metachunk::mangle(juint word_value) { 106 // Overwrite the payload of the chunk and not the links that 107 // maintain list of chunks. 108 HeapWord* start = (HeapWord*)initial_top(); 109 size_t size = word_size() - overhead(); 110 Copy::fill_to_words(start, size, word_value); 111 } 112 113 void Metachunk::verify() const { 114 assert(is_valid_sentinel(), "Chunk " PTR_FORMAT ": sentinel invalid", p2i(this)); 115 const ChunkIndex chunk_type = get_chunk_type(); 116 assert(is_valid_chunktype(chunk_type), "Chunk " PTR_FORMAT ": Invalid chunk type.", p2i(this)); 117 if (chunk_type != HumongousIndex) { 118 assert(word_size() == get_size_for_nonhumongous_chunktype(chunk_type, is_class()), 119 "Chunk " PTR_FORMAT ": wordsize " SIZE_FORMAT " does not fit chunk type %s.", 120 p2i(this), word_size(), chunk_size_name(chunk_type)); 121 } 122 assert(is_valid_chunkorigin(get_origin()), "Chunk " PTR_FORMAT ": Invalid chunk origin.", p2i(this)); 123 assert(bottom() <= _top && _top <= (MetaWord*)end(), 124 "Chunk " PTR_FORMAT ": Chunk top out of chunk bounds.", p2i(this)); 125 126 // For non-humongous chunks, starting address shall be aligned 127 // to its chunk size. Humongous chunks start address is 128 // aligned to specialized chunk size. 129 const size_t required_alignment = 130 (chunk_type != HumongousIndex ? word_size() : get_size_for_nonhumongous_chunktype(SpecializedIndex, is_class())) * sizeof(MetaWord); 131 assert(is_aligned((address)this, required_alignment), 132 "Chunk " PTR_FORMAT ": (size " SIZE_FORMAT ") not aligned to " SIZE_FORMAT ".", 133 p2i(this), word_size() * sizeof(MetaWord), required_alignment); 134 } 135 136 #endif // ASSERT 137 138 // Helper, returns a descriptive name for the given index. 139 const char* chunk_size_name(ChunkIndex index) { 140 switch (index) { 141 case SpecializedIndex: 142 return "specialized"; 143 case SmallIndex: 144 return "small"; 145 case MediumIndex: 146 return "medium"; 147 case HumongousIndex: 148 return "humongous"; 149 default: 150 return "Invalid index"; 151 } 152 } 153 154 #ifdef ASSERT 155 void do_verify_chunk(Metachunk* chunk) { 156 guarantee(chunk != NULL, "Sanity"); 157 // Verify chunk itself; then verify that it is consistent with the 158 // occupany map of its containing node. 159 chunk->verify(); 160 VirtualSpaceNode* const vsn = chunk->container(); 161 OccupancyMap* const ocmap = vsn->occupancy_map(); 162 ocmap->verify_for_chunk(chunk); 163 } 164 #endif 165 166 void do_update_in_use_info_for_chunk(Metachunk* chunk, bool inuse) { 167 chunk->set_is_tagged_free(!inuse); 168 OccupancyMap* const ocmap = chunk->container()->occupancy_map(); 169 ocmap->set_region_in_use((MetaWord*)chunk, chunk->word_size(), inuse); 170 } 171 172 } // namespace metaspace 173 } // namespace internals