src/hotspot/share/utilities/linkedlist.hpp
author redestad
Thu, 20 Dec 2018 19:44:08 +0100
changeset 53091 5bf5b773fc41
parent 47216 71c04702a3d5
child 53244 9807daeb47c4
permissions -rw-r--r--
8215555: TieredCompilation C2 threads can excessively block handshakes Reviewed-by: kvn, neliasso, rehn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     1
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     4
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     8
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    13
 * accompanied this code).
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    14
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    18
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    21
 * questions.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    22
 *
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    23
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    24
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    25
#ifndef SHARE_VM_UTILITIES_LINKED_LIST_HPP
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    26
#define SHARE_VM_UTILITIES_LINKED_LIST_HPP
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    27
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    28
#include "memory/allocation.hpp"
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    29
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    30
/*
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    31
 * The implementation of a generic linked list, which uses various
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    32
 * backing storages, such as C heap, arena and resource, etc.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    33
 */
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    34
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    35
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    36
// An entry in a linked list. It should use the same backing storage
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    37
// as the linked list that contains this entry.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    38
template <class E> class LinkedListNode : public ResourceObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    39
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    40
  E                       _data;  // embedded content
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    41
  LinkedListNode<E>*      _next;  // next entry
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    42
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    43
 protected:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    44
  LinkedListNode() : _next(NULL) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    45
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    46
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    47
  LinkedListNode(const E& e): _data(e), _next(NULL) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    48
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    49
  inline void set_next(LinkedListNode<E>* node) { _next = node; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    50
  inline LinkedListNode<E> * next() const       { return _next; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    51
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    52
  E*  data() { return &_data; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    53
  const E* peek() const { return &_data; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    54
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    55
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    56
// A linked list interface. It does not specify
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    57
// any storage type it uses, so all methods involving
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    58
// memory allocation or deallocation are pure virtual
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    59
template <class E> class LinkedList : public ResourceObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    60
 protected:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    61
  LinkedListNode<E>*    _head;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    62
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    63
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    64
  LinkedList() : _head(NULL) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    65
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    66
  inline void set_head(LinkedListNode<E>* h) { _head = h; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    67
  inline LinkedListNode<E>* head() const     { return _head; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    68
  inline bool is_empty()           const     { return head() == NULL; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    69
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    70
  inline size_t size() const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    71
    LinkedListNode<E>* p;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    72
    size_t count = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    73
    for (p = head(); p != NULL; count++, p = p->next());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    74
    return count;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    75
 }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    76
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    77
  // Move all entries from specified linked list to this one
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    78
  virtual void move(LinkedList<E>* list) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    79
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    80
  // Add an entry to this linked list
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    81
  virtual LinkedListNode<E>* add(const E& e) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    82
  // Add all entries from specified linked list to this one,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    83
  virtual void add(LinkedListNode<E>* node) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    84
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    85
  // Add a linked list to this linked list
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    86
  virtual bool  add(const LinkedList<E>* list) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    87
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    88
  // Search entry in the linked list
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    89
  virtual LinkedListNode<E>* find_node(const E& e) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    90
  virtual E* find(const E& e) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    91
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    92
  // Insert entry to the linked list
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    93
  virtual LinkedListNode<E>* insert_before(const E& e, LinkedListNode<E>* ref) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    94
  virtual LinkedListNode<E>* insert_after (const E& e, LinkedListNode<E>* ref) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    95
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    96
  // Remove entry from the linked list
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    97
  virtual bool               remove(const E& e) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    98
  virtual bool               remove(LinkedListNode<E>* node) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
    99
  virtual bool               remove_before(LinkedListNode<E>* ref) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   100
  virtual bool               remove_after(LinkedListNode<E>*  ref) = 0;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   101
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   102
  LinkedListNode<E>* unlink_head() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   103
    LinkedListNode<E>* h = this->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   104
    if (h != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   105
      this->set_head(h->next());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   106
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   107
    return h;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   108
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   109
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   110
  DEBUG_ONLY(virtual ResourceObj::allocation_type storage_type() = 0;)
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   111
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   112
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   113
// A linked list implementation.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   114
// The linked list can be allocated in various type of memory: C heap, arena and resource area, etc.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   115
template <class E, ResourceObj::allocation_type T = ResourceObj::C_HEAP,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   116
  MEMFLAGS F = mtNMT, AllocFailType alloc_failmode = AllocFailStrategy::RETURN_NULL>
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   117
  class LinkedListImpl : public LinkedList<E> {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   118
 protected:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   119
  Arena*                 _arena;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   120
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   121
  LinkedListImpl() :  _arena(NULL) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   122
  LinkedListImpl(Arena* a) : _arena(a) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   123
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   124
  virtual ~LinkedListImpl() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   125
    clear();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   126
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   127
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   128
  virtual void clear() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   129
    LinkedListNode<E>* p = this->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   130
    this->set_head(NULL);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   131
    while (p != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   132
      LinkedListNode<E>* to_delete = p;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   133
      p = p->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   134
      delete_node(to_delete);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   135
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   136
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   137
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   138
  // Add an entry to the linked list
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   139
  virtual LinkedListNode<E>* add(const E& e)  {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   140
    LinkedListNode<E>* node = this->new_node(e);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   141
    if (node != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   142
      this->add(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   143
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   144
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   145
    return node;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   146
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   147
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   148
  virtual void add(LinkedListNode<E>* node) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   149
    assert(node != NULL, "NULL pointer");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   150
    node->set_next(this->head());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   151
    this->set_head(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   152
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   153
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   154
  // Move a linked list to this linked list, both have to be allocated on the same
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   155
  // storage type.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   156
  virtual void move(LinkedList<E>* list) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   157
    assert(list->storage_type() == this->storage_type(), "Different storage type");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   158
    LinkedListNode<E>* node = this->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   159
    while (node != NULL && node->next() != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   160
      node = node->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   161
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   162
    if (node == NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   163
      this->set_head(list->head());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   164
    } else {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   165
      node->set_next(list->head());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   166
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   167
    // All entries are moved
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   168
    list->set_head(NULL);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   169
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   170
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   171
  virtual bool add(const LinkedList<E>* list) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   172
    LinkedListNode<E>* node = list->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   173
    while (node != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   174
      if (this->add(*node->peek()) == NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   175
        return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   176
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   177
      node = node->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   178
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   179
    return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   180
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   181
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   182
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   183
  virtual LinkedListNode<E>* find_node(const E& e) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   184
    LinkedListNode<E>* p = this->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   185
    while (p != NULL && !p->peek()->equals(e)) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   186
      p = p->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   187
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   188
    return p;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   189
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   190
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   191
  E* find(const E& e) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   192
    LinkedListNode<E>* node = find_node(e);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   193
    return (node == NULL) ? NULL : node->data();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   194
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   195
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   196
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   197
  // Add an entry in front of the reference entry
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   198
  LinkedListNode<E>* insert_before(const E& e, LinkedListNode<E>* ref_node) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   199
    LinkedListNode<E>* node = this->new_node(e);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   200
    if (node == NULL) return NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   201
    if (ref_node == this->head()) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   202
      node->set_next(ref_node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   203
      this->set_head(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   204
    } else {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   205
      LinkedListNode<E>* p = this->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   206
      while (p != NULL && p->next() != ref_node) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   207
        p = p->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   208
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   209
      assert(p != NULL, "ref_node not in the list");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   210
      node->set_next(ref_node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   211
      p->set_next(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   212
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   213
    return node;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   214
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   215
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   216
   // Add an entry behind the reference entry
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   217
   LinkedListNode<E>* insert_after(const E& e, LinkedListNode<E>* ref_node) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   218
     LinkedListNode<E>* node = this->new_node(e);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   219
     if (node == NULL) return NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   220
     node->set_next(ref_node->next());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   221
     ref_node->set_next(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   222
     return node;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   223
   }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   224
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   225
   // Remove an entry from the linked list.
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   226
   // Return true if the entry is successfully removed
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   227
   virtual bool remove(const E& e) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   228
     LinkedListNode<E>* tmp = this->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   229
     LinkedListNode<E>* prev = NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   230
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   231
     while (tmp != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   232
       if (tmp->peek()->equals(e)) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   233
         return remove_after(prev);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   234
       }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   235
       prev = tmp;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   236
       tmp = tmp->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   237
     }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   238
     return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   239
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   240
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   241
  // Remove the node after the reference entry
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   242
  virtual bool remove_after(LinkedListNode<E>* prev) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   243
    LinkedListNode<E>* to_delete;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   244
    if (prev == NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   245
      to_delete = this->unlink_head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   246
    } else {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   247
      to_delete = prev->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   248
      if (to_delete != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   249
        prev->set_next(to_delete->next());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   250
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   251
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   252
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   253
    if (to_delete != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   254
      delete_node(to_delete);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   255
      return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   256
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   257
    return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   258
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   259
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   260
  virtual bool remove(LinkedListNode<E>* node) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   261
    LinkedListNode<E>* p = this->head();
38935
f7427b0e0d7c 8138705: Kitchen sink stress test fails
mockner
parents: 25946
diff changeset
   262
    if (p == node) {
f7427b0e0d7c 8138705: Kitchen sink stress test fails
mockner
parents: 25946
diff changeset
   263
      this->set_head(p->next());
f7427b0e0d7c 8138705: Kitchen sink stress test fails
mockner
parents: 25946
diff changeset
   264
      delete_node(node);
f7427b0e0d7c 8138705: Kitchen sink stress test fails
mockner
parents: 25946
diff changeset
   265
      return true;
f7427b0e0d7c 8138705: Kitchen sink stress test fails
mockner
parents: 25946
diff changeset
   266
    }
25946
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   267
    while (p != NULL && p->next() != node) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   268
      p = p->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   269
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   270
    if (p != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   271
      p->set_next(node->next());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   272
      delete_node(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   273
      return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   274
    } else {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   275
      return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   276
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   277
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   278
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   279
  virtual bool remove_before(LinkedListNode<E>* ref) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   280
    assert(ref != NULL, "NULL pointer");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   281
    LinkedListNode<E>* p = this->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   282
    LinkedListNode<E>* to_delete = NULL; // to be deleted
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   283
    LinkedListNode<E>* prev = NULL;      // node before the node to be deleted
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   284
    while (p != NULL && p != ref) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   285
      prev = to_delete;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   286
      to_delete = p;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   287
      p = p->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   288
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   289
    if (p == NULL || to_delete == NULL) return false;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   290
    assert(to_delete->next() == ref, "Wrong node to delete");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   291
    assert(prev == NULL || prev->next() == to_delete,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   292
      "Sanity check");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   293
    if (prev == NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   294
      assert(to_delete == this->head(), "Must be head");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   295
      this->set_head(to_delete->next());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   296
    } else {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   297
      prev->set_next(to_delete->next());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   298
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   299
    delete_node(to_delete);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   300
    return true;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   301
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   302
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   303
  DEBUG_ONLY(ResourceObj::allocation_type storage_type() { return T; })
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   304
 protected:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   305
  // Create new linked list node object in specified storage
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   306
  LinkedListNode<E>* new_node(const E& e) const {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   307
     switch(T) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   308
       case ResourceObj::ARENA: {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   309
         assert(_arena != NULL, "Arena not set");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   310
         return new(_arena) LinkedListNode<E>(e);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   311
       }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   312
       case ResourceObj::RESOURCE_AREA:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   313
       case ResourceObj::C_HEAP: {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   314
         if (alloc_failmode == AllocFailStrategy::RETURN_NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   315
           return new(std::nothrow, T, F) LinkedListNode<E>(e);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   316
         } else {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   317
           return new(T, F) LinkedListNode<E>(e);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   318
         }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   319
       }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   320
       default:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   321
         ShouldNotReachHere();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   322
     }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   323
     return NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   324
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   325
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   326
  // Delete linked list node object
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   327
  void delete_node(LinkedListNode<E>* node) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   328
    if (T == ResourceObj::C_HEAP) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   329
      delete node;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   330
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   331
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   332
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   333
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   334
// Sorted linked list. The linked list maintains sorting order specified by the comparison
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   335
// function
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   336
template <class E, int (*FUNC)(const E&, const E&),
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   337
  ResourceObj::allocation_type T = ResourceObj::C_HEAP,
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   338
  MEMFLAGS F = mtNMT, AllocFailType alloc_failmode = AllocFailStrategy::RETURN_NULL>
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   339
  class SortedLinkedList : public LinkedListImpl<E, T, F, alloc_failmode> {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   340
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   341
  SortedLinkedList() { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   342
  SortedLinkedList(Arena* a) : LinkedListImpl<E, T, F, alloc_failmode>(a) { }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   343
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   344
  virtual LinkedListNode<E>* add(const E& e) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   345
    return LinkedListImpl<E, T, F, alloc_failmode>::add(e);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   346
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   347
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   348
  virtual void move(LinkedList<E>* list) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   349
    assert(list->storage_type() == this->storage_type(), "Different storage type");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   350
    LinkedListNode<E>* node;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   351
    while ((node = list->unlink_head()) != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   352
      this->add(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   353
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   354
    assert(list->is_empty(), "All entries are moved");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   355
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   356
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   357
  virtual void add(LinkedListNode<E>* node) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   358
    assert(node != NULL, "NULL pointer");
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   359
    LinkedListNode<E>* tmp = this->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   360
    LinkedListNode<E>* prev = NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   361
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   362
    int cmp_val;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   363
    while (tmp != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   364
      cmp_val = FUNC(*tmp->peek(), *node->peek());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   365
      if (cmp_val >= 0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   366
        break;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   367
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   368
      prev = tmp;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   369
      tmp = tmp->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   370
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   371
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   372
    if (prev != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   373
      node->set_next(prev->next());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   374
      prev->set_next(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   375
    } else {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   376
      node->set_next(this->head());
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   377
      this->set_head(node);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   378
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   379
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   380
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   381
  virtual bool add(const LinkedList<E>* list) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   382
    return LinkedListImpl<E, T, F, alloc_failmode>::add(list);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   383
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   384
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   385
  virtual LinkedListNode<E>* find_node(const E& e) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   386
    LinkedListNode<E>* p = this->head();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   387
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   388
    while (p != NULL) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   389
      int comp_val = FUNC(*p->peek(), e);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   390
      if (comp_val == 0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   391
        return p;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   392
      } else if (comp_val > 0) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   393
        return NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   394
      }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   395
      p = p->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   396
    }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   397
    return NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   398
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   399
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   400
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   401
// Iterates all entries in the list
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   402
template <class E> class LinkedListIterator : public StackObj {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   403
 private:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   404
  LinkedListNode<E>* _p;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   405
  bool               _is_empty;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   406
 public:
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   407
  LinkedListIterator(LinkedListNode<E>* head) : _p(head) {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   408
    _is_empty = (head == NULL);
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   409
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   410
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   411
  bool is_empty() const { return _is_empty; }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   412
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   413
  const E* next() {
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   414
    if (_p == NULL) return NULL;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   415
    const E* e = _p->peek();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   416
    _p = _p->next();
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   417
    return e;
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   418
  }
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   419
};
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   420
1572c9f03fb9 8046598: Scalable Native memory tracking development
zgu
parents:
diff changeset
   421
#endif