1 /*
   2  * Copyright (c) 2015, 2019, 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 #ifndef SHARE_GC_Z_ZLIST_HPP
  25 #define SHARE_GC_Z_ZLIST_HPP
  26 
  27 #include "memory/allocation.hpp"
  28 #include "utilities/macros.hpp"
  29 
  30 template <typename T> class ZList;
  31 
  32 // Element in a doubly linked list
  33 template <typename T>
  34 class ZListNode {
  35   friend class ZList<T>;
  36 
  37 private:
  38   ZListNode* _next;
  39   ZListNode* _prev;
  40 
  41   ZListNode(ZListNode* next, ZListNode* prev);
  42 
  43   void set_unused();
  44 
  45 public:
  46   ZListNode();
  47   ~ZListNode();
  48 
  49   bool is_unused() const;
  50 };
  51 
  52 // Doubly linked list
  53 template <typename T>
  54 class ZList {
  55 private:
  56   ZListNode<T> _head;
  57   size_t       _size;
  58 
  59   NONCOPYABLE(ZList);
  60 
  61   void verify() const;
  62 
  63   void insert(ZListNode<T>* before, ZListNode<T>* node);
  64 
  65   ZListNode<T>* cast_to_inner(T* elem) const;
  66   T* cast_to_outer(ZListNode<T>* node) const;
  67 
  68 public:
  69   ZList();
  70 
  71   size_t size() const;
  72   bool is_empty() const;
  73 
  74   T* first() const;
  75   T* last() const;
  76   T* next(T* elem) const;
  77   T* prev(T* elem) const;
  78 
  79   void insert_first(T* elem);
  80   void insert_last(T* elem);
  81   void insert_before(T* before, T* elem);
  82   void insert_after(T* after, T* elem);
  83 
  84   void remove(T* elem);
  85   T* remove_first();
  86   T* remove_last();
  87 
  88   void transfer(ZList<T>* list);
  89 };
  90 
  91 template <typename T, bool forward>
  92 class ZListIteratorImpl : public StackObj {
  93 private:
  94   const ZList<T>* const _list;
  95   T*                    _next;
  96 
  97 public:
  98   ZListIteratorImpl(const ZList<T>* list);
  99 
 100   bool next(T** elem);
 101 };
 102 
 103 // Iterator types
 104 #define ZLIST_FORWARD        true
 105 #define ZLIST_REVERSE        false
 106 
 107 template <typename T>
 108 class ZListIterator : public ZListIteratorImpl<T, ZLIST_FORWARD> {
 109 public:
 110   ZListIterator(const ZList<T>* list);
 111 };
 112 
 113 template <typename T>
 114 class ZListReverseIterator : public ZListIteratorImpl<T, ZLIST_REVERSE> {
 115 public:
 116   ZListReverseIterator(const ZList<T>* list);
 117 };
 118 
 119 #endif // SHARE_GC_Z_ZLIST_HPP