1 /*
   2  * Copyright (c) 2018, 2019, 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 #ifndef SHARE_MEMORY_METASPACE_BLOCKFREELIST_HPP
  26 #define SHARE_MEMORY_METASPACE_BLOCKFREELIST_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/binaryTreeDictionary.hpp"
  30 #include "memory/freeList.hpp"
  31 #include "memory/metaspace/smallBlocks.hpp"
  32 #include "memory/metaspace/metablock.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 namespace metaspace {
  36 
  37 typedef BinaryTreeDictionary<Metablock, FreeList<Metablock> > BlockTreeDictionary;
  38 
  39 // Used to manage the free list of Metablocks (a block corresponds
  40 // to the allocation of a quantum of metadata).
  41 class BlockFreelist : public CHeapObj<mtClass> {
  42 
  43   BlockTreeDictionary* const _dictionary;
  44   SmallBlocks* _small_blocks;
  45 
  46   // Only allocate and split from freelist if the size of the allocation
  47   // is at least 1/4th the size of the available block.
  48   const static int WasteMultiplier = 4;
  49 
  50   // Accessors
  51   BlockTreeDictionary* dictionary() const { return _dictionary; }
  52   SmallBlocks* small_blocks() {
  53     if (_small_blocks == NULL) {
  54       _small_blocks = new SmallBlocks();
  55     }
  56     return _small_blocks;
  57   }
  58 
  59  public:
  60 
  61   BlockFreelist();
  62   ~BlockFreelist();
  63 
  64   // Get and return a block to the free list
  65   MetaWord* get_block(size_t word_size);
  66   void return_block(MetaWord* p, size_t word_size);
  67 
  68   // Returns the total size, in words, of all blocks kept in this structure.
  69   size_t total_size() const  {
  70     size_t result = dictionary()->total_size();
  71     if (_small_blocks != NULL) {
  72       result = result + _small_blocks->total_size();
  73     }
  74     return result;
  75   }
  76 
  77   // Returns the number of all blocks kept in this structure.
  78   uintx num_blocks() const {
  79     uintx result = dictionary()->total_free_blocks();
  80     if (_small_blocks != NULL) {
  81       result = result + _small_blocks->total_num_blocks();
  82     }
  83     return result;
  84   }
  85 
  86   static size_t min_dictionary_size()   { return TreeChunk<Metablock, FreeList<Metablock> >::min_size(); }
  87   void print_on(outputStream* st) const;
  88 };
  89 
  90 } // namespace metaspace
  91 
  92 #endif // SHARE_MEMORY_METASPACE_BLOCKFREELIST_HPP