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