1 /*
   2  * Copyright (c) 2018, 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 #include "precompiled.hpp"
  25 
  26 #include "logging/log.hpp"
  27 #include "memory/binaryTreeDictionary.inline.hpp"
  28 #include "memory/metaspace/blockFreelist.hpp"
  29 #include "utilities/ostream.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 
  32 
  33 namespace metaspace {
  34 namespace internals {
  35 
  36 
  37 BlockFreelist::BlockFreelist() : _dictionary(new BlockTreeDictionary()), _small_blocks(NULL) {}
  38 
  39 BlockFreelist::~BlockFreelist() {
  40   delete _dictionary;
  41   if (_small_blocks != NULL) {
  42     delete _small_blocks;
  43   }
  44 }
  45 
  46 void BlockFreelist::return_block(MetaWord* p, size_t word_size) {
  47   assert(word_size >= SmallBlocks::small_block_min_size(), "never return dark matter");
  48 
  49   Metablock* free_chunk = ::new (p) Metablock(word_size);
  50   if (word_size < SmallBlocks::small_block_max_size()) {
  51     small_blocks()->return_block(free_chunk, word_size);
  52   } else {
  53   dictionary()->return_chunk(free_chunk);
  54 }
  55   log_trace(gc, metaspace, freelist, blocks)("returning block at " INTPTR_FORMAT " size = "
  56             SIZE_FORMAT, p2i(free_chunk), word_size);
  57 }
  58 
  59 MetaWord* BlockFreelist::get_block(size_t word_size) {
  60   assert(word_size >= SmallBlocks::small_block_min_size(), "never get dark matter");
  61 
  62   // Try small_blocks first.
  63   if (word_size < SmallBlocks::small_block_max_size()) {
  64     // Don't create small_blocks() until needed.  small_blocks() allocates the small block list for
  65     // this space manager.
  66     MetaWord* new_block = (MetaWord*) small_blocks()->get_block(word_size);
  67     if (new_block != NULL) {
  68       log_trace(gc, metaspace, freelist, blocks)("getting block at " INTPTR_FORMAT " size = " SIZE_FORMAT,
  69               p2i(new_block), word_size);
  70       return new_block;
  71     }
  72   }
  73 
  74   if (word_size < BlockFreelist::min_dictionary_size()) {
  75     // If allocation in small blocks fails, this is Dark Matter.  Too small for dictionary.
  76     return NULL;
  77   }
  78 
  79   Metablock* free_block = dictionary()->get_chunk(word_size);
  80   if (free_block == NULL) {
  81     return NULL;
  82   }
  83 
  84   const size_t block_size = free_block->size();
  85   if (block_size > WasteMultiplier * word_size) {
  86     return_block((MetaWord*)free_block, block_size);
  87     return NULL;
  88   }
  89 
  90   MetaWord* new_block = (MetaWord*)free_block;
  91   assert(block_size >= word_size, "Incorrect size of block from freelist");
  92   const size_t unused = block_size - word_size;
  93   if (unused >= SmallBlocks::small_block_min_size()) {
  94     return_block(new_block + word_size, unused);
  95   }
  96 
  97   log_trace(gc, metaspace, freelist, blocks)("getting block at " INTPTR_FORMAT " size = " SIZE_FORMAT,
  98             p2i(new_block), word_size);
  99   return new_block;
 100 }
 101 
 102 void BlockFreelist::print_on(outputStream* st) const {
 103   dictionary()->print_free_lists(st);
 104   if (_small_blocks != NULL) {
 105     _small_blocks->print_on(st);
 106   }
 107 }
 108 
 109 } // namespace metaspace
 110 } // namespace internals