1 /*
   2  * Copyright (c) 2012, 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 // Future modification
  34 //
  35 // The Metachunk can conceivable be replaced by the Chunk in
  36 // allocation.hpp.  Note that the latter Chunk is the space for
  37 // allocation (allocations from the chunk are out of the space in
  38 // the Chunk after the header for the Chunk) where as Metachunks
  39 // point to space in a VirtualSpace.  To replace Metachunks with
  40 // Chunks, change Chunks so that they can be allocated out of a VirtualSpace.
  41 
  42 const size_t metadata_chunk_initialize = 0xf7f7f7f7;
  43 
  44 size_t Metachunk::_overhead =
  45   Chunk::aligned_overhead_size(sizeof(Metachunk)) / BytesPerWord;
  46 
  47 // Metachunk methods
  48 
  49 Metachunk::Metachunk(size_t word_size,
  50                      VirtualSpaceNode* container) :
  51     _word_size(word_size),
  52     _bottom(NULL),
  53     _end(NULL),
  54     _top(NULL),
  55     _next(NULL),
  56     _prev(NULL),
  57     _container(container)
  58 {
  59   _bottom = (MetaWord*)this;
  60   _top = (MetaWord*)this + _overhead;
  61   _end = (MetaWord*)this + word_size;
  62 #ifdef ASSERT
  63   set_is_free(false);
  64   size_t data_word_size = pointer_delta(end(),
  65                                         top(),
  66                                         sizeof(MetaWord));
  67   Copy::fill_to_words((HeapWord*) top(),
  68                       data_word_size,
  69                       metadata_chunk_initialize);
  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 size_t Metachunk::capacity_word_size() const {
  93   return pointer_delta(_end, _bottom, sizeof(MetaWord));
  94 }
  95 
  96 void Metachunk::print_on(outputStream* st) const {
  97   st->print_cr("Metachunk:"
  98                " bottom " PTR_FORMAT " top " PTR_FORMAT
  99                " end " PTR_FORMAT " size " SIZE_FORMAT,
 100                bottom(), top(), end(), word_size());
 101   if (Verbose) {
 102     st->print_cr("    used " SIZE_FORMAT " free " SIZE_FORMAT,
 103                  used_word_size(), free_word_size());
 104   }
 105 }
 106 
 107 #ifndef PRODUCT
 108 void Metachunk::mangle() {
 109   // Mangle the payload of the chunk and not the links that
 110   // maintain list of chunks.
 111   HeapWord* start = (HeapWord*)(bottom() + overhead());
 112   size_t word_size = capacity_word_size() - overhead();
 113   Copy::fill_to_words(start, word_size, metadata_chunk_initialize);
 114 }
 115 #endif // PRODUCT
 116 
 117 void Metachunk::verify() {
 118 #ifdef ASSERT
 119   // Cannot walk through the blocks unless the blocks have
 120   // headers with sizes.
 121   assert(_bottom <= _top &&
 122          _top <= _end,
 123          "Chunk has been smashed");
 124 #endif
 125   return;
 126 }