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 #include "precompiled.hpp"
  27 #include "memory/metaspace/chunkHeaderPool.hpp"
  28 #include "runtime/os.hpp"
  29 #include "utilities/debug.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 
  32 namespace metaspace {
  33 
  34 
  35 // Returns reference to the one global chunk header pool.
  36 ChunkHeaderPool* ChunkHeaderPool::_chunkHeaderPool = NULL;
  37 
  38 
  39 ChunkHeaderPool::ChunkHeaderPool()
  40   : _num_slabs(), _first_slab(NULL), _current_slab(NULL)
  41 {
  42 }
  43 
  44 // Note: the global chunk header pool gets never deleted; so this destructor only
  45 // exists for the sake of tests.
  46 ChunkHeaderPool::~ChunkHeaderPool() {
  47   slab_t* s = _first_slab;
  48   while (s != NULL) {
  49     slab_t* next_slab = s->next;
  50     os::free(s);
  51      s = next_slab;
  52   }
  53 }
  54 
  55 void ChunkHeaderPool::allocate_new_slab() {
  56   slab_t* slab = new slab_t();
  57   if (_current_slab != NULL) {
  58     _current_slab->next = slab;
  59   }
  60   _current_slab = slab;
  61   if (_first_slab == NULL) {
  62     _first_slab = slab;
  63   }
  64   _num_slabs.increment();
  65 }
  66 
  67 // Returns size of memory used.
  68 size_t ChunkHeaderPool::memory_footprint_words() const {
  69   return (_num_slabs.get() * sizeof(slab_t)) / BytesPerWord;
  70 }
  71 
  72 void ChunkHeaderPool::initialize() {
  73   assert(_chunkHeaderPool == NULL, "only once");
  74   _chunkHeaderPool = new ChunkHeaderPool();
  75 }
  76 
  77 #ifdef ASSERT
  78 void ChunkHeaderPool::verify(bool slow) const {
  79   const slab_t* s = _first_slab;
  80   int num = 0;
  81   while (s != NULL) {
  82     assert(s->top >= 0 && s->top <= slab_capacity,
  83            "invalid slab at " PTR_FORMAT ", top: %d, slab cap: %d",
  84            p2i(s), s->top, slab_capacity );
  85     s = s->next;
  86     num ++;
  87   }
  88   _num_slabs.check(num);
  89 }
  90 #endif
  91 
  92 } // namespace metaspace
  93 
  94