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