1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2020 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #ifndef SHARE_MEMORY_METASPACE_CHUNKALLOCSEQUENCE_HPP
  27 #define SHARE_MEMORY_METASPACE_CHUNKALLOCSEQUENCE_HPP
  28 
  29 #include "memory/metaspace.hpp" // For Metaspace::MetaspaceType
  30 #include "memory/metaspace/chunkLevel.hpp"
  31 #include "utilities/debug.hpp"
  32 
  33 namespace metaspace {
  34 
  35 
  36 // ArenaGrowthPolicy encodes the growth policy of a MetaspaceArena.
  37 //
  38 // These arenas grow in steps (by allocating new chunks). The coarseness of growth
  39 // (chunk size, level) depends on what the arena is used for. Used for a class loader
  40 // which is expected to load only one or very few classes should grow in tiny steps.
  41 // For normal classloaders, it can grow in coarser steps, and arenas used by
  42 // the boot loader will grow in even larger steps since we expect it to load a lot of
  43 // classes.
  44 // Note that when growing in large steps (in steps larger than a commit granule,
  45 // by default 64K), costs diminish somewhat since we do not commit the whole space
  46 // immediately.
  47 
  48 class ArenaGrowthPolicy {
  49 
  50   // const array specifying chunk level allocation progression (growth steps). Last
  51   //  chunk is to be an endlessly repeated allocation.
  52   const chunklevel_t* const _entries;
  53   const int _num_entries;
  54 
  55   ArenaGrowthPolicy(const chunklevel_t* array, int num_entries)
  56     : _entries(array), _num_entries(num_entries) {
  57     assert(_num_entries > 0, "must not be empty.");
  58   }
  59 
  60 public:
  61 
  62   chunklevel_t get_level_at_step(int num_allocated) const {
  63     if (num_allocated >= _num_entries) {
  64       // Caller shall repeat last allocation
  65       return _entries[_num_entries - 1];
  66     }
  67     return _entries[num_allocated];
  68   }
  69 
  70   // Given a space type, return the correct policy to use.
  71   // The returned object is static and read only.
  72   static const ArenaGrowthPolicy* policy_for_space_type(Metaspace::MetaspaceType space_type, bool is_class);
  73 
  74 };
  75 
  76 
  77 } // namespace metaspace
  78 
  79 #endif // SHARE_MEMORY_METASPACE_CHUNKALLOCSEQUENCE_HPP