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