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_MSMETACHUNKLIST_HPP
  27 #define SHARE_MEMORY_METASPACE_MSMETACHUNKLIST_HPP
  28 
  29 #include "memory/metaspace/msCommon.hpp"
  30 #include "memory/metaspace/msCounter.hpp"
  31 #include "memory/metaspace/msMetachunk.hpp"
  32 #include "utilities/debug.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 class outputStream;
  36 
  37 namespace metaspace {
  38 
  39 // A simple single-linked list of chunks, used in MetaspaceArena to keep
  40 //  a list of retired chunks, as well as in the ChunkHeaderPool to keep
  41 //  a cache of unused chunk headers.
  42 
  43 class MetachunkList {
  44 
  45   Metachunk* _first;
  46   IntCounter _num_chunks;
  47 
  48   // Note: The chunks inside this list may be dead (->chunk header pool).
  49   // So, do not call c->word size on them or anything else which may not
  50   // work with dead chunks.
  51 
  52 public:
  53 
  54   MetachunkList() : _first(NULL), _num_chunks() {}
  55 
  56   int count() const { return _num_chunks.get(); }
  57 
  58   void add(Metachunk* c) {
  59     // Note: contains is expensive (linear search).
  60     ASSERT_SOMETIMES(contains(c) == false, "Chunk already in this list");
  61     c->set_next(_first);
  62     if (_first) {
  63       _first->set_prev(c);
  64     }
  65     _first = c;
  66     _num_chunks.increment();
  67   }
  68 
  69   Metachunk* remove_first() {
  70     if (_first) {
  71       Metachunk* c = _first;
  72       _first = _first->next();
  73       if (_first) {
  74         _first->set_prev(NULL);
  75       }
  76       _num_chunks.decrement();
  77       c->set_prev(NULL);
  78       c->set_next(NULL);
  79       return c;
  80     }
  81     return NULL;
  82   }
  83 
  84   Metachunk* first()              { return _first; }
  85   const Metachunk* first() const  { return _first; }
  86 
  87 #ifdef ASSERT
  88   // Note: linear search
  89   bool contains(const Metachunk* c) const;
  90   void verify() const;
  91 #endif
  92 
  93   size_t calc_committed_word_size() const;
  94   size_t calc_word_size() const;
  95 
  96   void print_on(outputStream* st) const;
  97 
  98 };
  99 
 100 } // namespace metaspace
 101 
 102 #endif // SHARE_MEMORY_METASPACE_MSMETACHUNKLIST_HPP