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_SMALLBLOCKS_HPP_
  26 #define SHARE_MEMORY_METASPACE_SMALLBLOCKS_HPP_
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/binaryTreeDictionary.hpp"
  30 #include "memory/metaspace/metablock.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 
  33 class outputStream;
  34 
  35 namespace metaspace {
  36 namespace internals {
  37 
  38 class SmallBlocks : public CHeapObj<mtClass> {
  39 
  40   const static uint _small_block_max_size = sizeof(TreeChunk<Metablock,  FreeList<Metablock> >)/HeapWordSize;
  41   // Note: this corresponds to the imposed miminum allocation size, see SpaceManager::get_allocation_word_size()
  42   const static uint _small_block_min_size = sizeof(Metablock)/HeapWordSize;
  43 
  44 private:
  45   FreeList<Metablock> _small_lists[_small_block_max_size - _small_block_min_size];
  46 
  47   FreeList<Metablock>& list_at(size_t word_size) {
  48     assert(word_size >= _small_block_min_size, "There are no metaspace objects less than %u words", _small_block_min_size);
  49     return _small_lists[word_size - _small_block_min_size];
  50   }
  51 
  52 public:
  53   SmallBlocks() {
  54     for (uint i = _small_block_min_size; i < _small_block_max_size; i++) {
  55       uint k = i - _small_block_min_size;
  56       _small_lists[k].set_size(i);
  57     }
  58   }
  59 
  60   // Returns the total size, in words, of all blocks, across all block sizes.
  61   size_t total_size() const;
  62 
  63   // Returns the total number of all blocks across all block sizes.
  64   uintx total_num_blocks() const;
  65 
  66   static uint small_block_max_size() { return _small_block_max_size; }
  67   static uint small_block_min_size() { return _small_block_min_size; }
  68 
  69   MetaWord* get_block(size_t word_size) {
  70     if (list_at(word_size).count() > 0) {
  71       MetaWord* new_block = (MetaWord*) list_at(word_size).get_chunk_at_head();
  72       return new_block;
  73     } else {
  74       return NULL;
  75     }
  76   }
  77   void return_block(Metablock* free_chunk, size_t word_size) {
  78     list_at(word_size).return_chunk_at_head(free_chunk, false);
  79     assert(list_at(word_size).count() > 0, "Should have a chunk");
  80   }
  81 
  82   void print_on(outputStream* st) const;
  83 
  84 };
  85 
  86 } // namespace metaspace
  87 } // namespace internals
  88 
  89 
  90 #endif /* SHARE_MEMORY_METASPACE_SMALLBLOCKS_HPP_ */