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