hotspot/src/share/vm/services/memPtrArray.hpp
author zgu
Tue, 14 Aug 2012 13:56:46 -0400
changeset 13478 85fe611440e5
parent 13195 be27e1b6a4b9
child 13744 631fe815def5
permissions -rw-r--r--
7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT Summary: Updated all related variables and methods to use NOT_PRODUCT macros Reviewed-by: coleenp, acorn, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     1
/*
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     4
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     8
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    13
 * accompanied this code).
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    14
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    18
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    21
 * questions.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    22
 *
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    23
 */
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    24
#ifndef SHARE_VM_UTILITIES_MEM_PTR_ARRAY_HPP
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    25
#define SHARE_VM_UTILITIES_MEM_PTR_ARRAY_HPP
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    26
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    27
#include "memory/allocation.hpp"
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    28
#include "services/memPtr.hpp"
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    29
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    30
class MemPtr;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    31
class MemRecorder;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    32
class ArenaInfo;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    33
class MemSnapshot;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    34
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    35
extern "C" {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    36
  typedef int (*FN_SORT)(const void *, const void *);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    37
}
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    38
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    39
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    40
// Memory pointer array interface. This array is used by NMT to hold
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    41
// various memory block information.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    42
// The memory pointer arrays are usually walked with their iterators.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    43
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    44
class MemPointerArray : public CHeapObj<mtNMT> {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    45
 public:
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    46
  virtual ~MemPointerArray() { }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    47
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    48
  // return true if it can not allocate storage for the data
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    49
  virtual bool out_of_memory() const = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    50
  virtual bool is_empty() const = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    51
  virtual bool is_full() = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    52
  virtual int  length() const = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    53
  virtual void clear() = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    54
  virtual bool append(MemPointer* ptr) = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    55
  virtual bool insert_at(MemPointer* ptr, int pos) = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    56
  virtual bool remove_at(int pos) = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    57
  virtual MemPointer* at(int index) const = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    58
  virtual void sort(FN_SORT fn) = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    59
  virtual size_t instance_size() const = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    60
  virtual bool shrink() = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    61
13478
85fe611440e5 7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents: 13195
diff changeset
    62
  NOT_PRODUCT(virtual int capacity() const = 0;)
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    63
};
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    64
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    65
// Iterator interface
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    66
class MemPointerArrayIterator VALUE_OBJ_CLASS_SPEC {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    67
 public:
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    68
  // return the pointer at current position
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    69
  virtual MemPointer* current() const = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    70
  // return the next pointer and advance current position
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    71
  virtual MemPointer* next() = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    72
  // return next pointer without advancing current position
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    73
  virtual MemPointer* peek_next() const = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    74
  // return previous pointer without changing current position
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    75
  virtual MemPointer* peek_prev() const = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    76
  // remove the pointer at current position
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    77
  virtual void        remove() = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    78
  // insert the pointer at current position
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    79
  virtual bool        insert(MemPointer* ptr) = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    80
  // insert specified element after current position and
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    81
  // move current position to newly inserted position
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    82
  virtual bool        insert_after(MemPointer* ptr) = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    83
};
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    84
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    85
// implementation class
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    86
class MemPointerArrayIteratorImpl : public MemPointerArrayIterator {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    87
#ifdef ASSERT
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    88
 protected:
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    89
#else
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    90
 private:
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    91
#endif
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    92
  MemPointerArray*  _array;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    93
  int               _pos;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    94
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    95
 public:
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    96
  MemPointerArrayIteratorImpl(MemPointerArray* arr) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    97
    assert(arr != NULL, "Parameter check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    98
    _array = arr;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
    99
    _pos = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   100
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   101
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   102
  virtual MemPointer* current() const {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   103
    if (_pos < _array->length()) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   104
      return _array->at(_pos);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   105
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   106
    return NULL;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   107
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   108
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   109
  virtual MemPointer* next() {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   110
    if (_pos + 1 < _array->length()) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   111
      return _array->at(++_pos);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   112
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   113
    _pos = _array->length();
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   114
    return NULL;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   115
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   116
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   117
  virtual MemPointer* peek_next() const {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   118
    if (_pos + 1 < _array->length()) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   119
      return _array->at(_pos + 1);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   120
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   121
    return NULL;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   122
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   123
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   124
  virtual MemPointer* peek_prev() const {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   125
    if (_pos > 0) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   126
      return _array->at(_pos - 1);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   127
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   128
    return NULL;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   129
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   130
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   131
  virtual void remove() {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   132
    if (_pos < _array->length()) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   133
      _array->remove_at(_pos);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   134
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   135
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   136
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   137
  virtual bool insert(MemPointer* ptr) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   138
    return _array->insert_at(ptr, _pos);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   139
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   140
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   141
  virtual bool insert_after(MemPointer* ptr) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   142
    if (_array->insert_at(ptr, _pos + 1)) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   143
      _pos ++;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   144
      return true;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   145
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   146
    return false;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   147
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   148
};
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   149
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   150
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   151
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   152
// Memory pointer array implementation.
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   153
// This implementation implements expandable array
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   154
#define DEFAULT_PTR_ARRAY_SIZE 1024
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   155
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   156
template <class E> class MemPointerArrayImpl : public MemPointerArray {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   157
 private:
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   158
  int                   _max_size;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   159
  int                   _size;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   160
  bool                  _init_elements;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   161
  E*                    _data;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   162
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   163
 public:
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   164
  MemPointerArrayImpl(int initial_size = DEFAULT_PTR_ARRAY_SIZE, bool init_elements = true):
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   165
   _max_size(initial_size), _size(0), _init_elements(init_elements) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   166
    _data = (E*)raw_allocate(sizeof(E), initial_size);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   167
    if (_init_elements) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   168
      for (int index = 0; index < _max_size; index ++) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   169
        ::new ((void*)&_data[index]) E();
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   170
      }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   171
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   172
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   173
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   174
  virtual ~MemPointerArrayImpl() {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   175
    if (_data != NULL) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   176
      raw_free(_data);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   177
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   178
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   179
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   180
 public:
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   181
  bool out_of_memory() const {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   182
    return (_data == NULL);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   183
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   184
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   185
  size_t instance_size() const {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   186
    return sizeof(MemPointerArrayImpl<E>) + _max_size * sizeof(E);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   187
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   188
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   189
  bool is_empty() const {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   190
    assert(_data != NULL, "Just check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   191
    return _size == 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   192
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   193
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   194
  bool is_full() {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   195
    assert(_data != NULL, "Just check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   196
    if (_size < _max_size) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   197
      return false;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   198
    } else {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   199
      return !expand_array();
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   200
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   201
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   202
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   203
  int length() const {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   204
    assert(_data != NULL, "Just check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   205
    return _size;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   206
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   207
13478
85fe611440e5 7191124: Optimized build is broken due to inconsistent use of DEBUG_ONLY and NOT_PRODUCT macros in NMT
zgu
parents: 13195
diff changeset
   208
  NOT_PRODUCT(int capacity() const { return _max_size; })
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   209
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   210
  void clear() {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   211
    assert(_data != NULL, "Just check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   212
    _size = 0;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   213
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   214
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   215
  bool append(MemPointer* ptr) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   216
    assert(_data != NULL, "Just check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   217
    if (is_full()) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   218
      return false;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   219
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   220
    _data[_size ++] = *(E*)ptr;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   221
    return true;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   222
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   223
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   224
  bool insert_at(MemPointer* ptr, int pos) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   225
    assert(_data != NULL, "Just check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   226
    if (is_full()) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   227
      return false;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   228
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   229
    for (int index = _size; index > pos; index --) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   230
      _data[index] = _data[index - 1];
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   231
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   232
    _data[pos] = *(E*)ptr;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   233
    _size ++;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   234
    return true;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   235
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   236
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   237
  bool remove_at(int pos) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   238
    assert(_data != NULL, "Just check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   239
    if (_size <= pos && pos >= 0) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   240
      return false;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   241
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   242
    -- _size;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   243
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   244
    for (int index = pos; index < _size; index ++) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   245
      _data[index] = _data[index + 1];
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   246
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   247
    return true;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   248
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   249
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   250
  MemPointer* at(int index) const {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   251
    assert(_data != NULL, "Just check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   252
    assert(index >= 0 && index < _size, "illegal index");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   253
    return &_data[index];
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   254
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   255
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   256
  bool shrink() {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   257
    float used = ((float)_size) / ((float)_max_size);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   258
    if (used < 0.40) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   259
      E* old_ptr = _data;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   260
      int new_size = ((_max_size) / (2 * DEFAULT_PTR_ARRAY_SIZE) + 1) * DEFAULT_PTR_ARRAY_SIZE;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   261
      _data = (E*)raw_reallocate(_data, sizeof(E), new_size);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   262
      if (_data == NULL) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   263
        _data = old_ptr;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   264
        return false;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   265
      } else {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   266
        _max_size = new_size;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   267
        return true;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   268
      }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   269
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   270
    return false;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   271
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   272
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   273
  void sort(FN_SORT fn) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   274
    assert(_data != NULL, "Just check");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   275
    qsort((void*)_data, _size, sizeof(E), fn);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   276
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   277
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   278
 private:
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   279
  bool  expand_array() {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   280
    assert(_data != NULL, "Not yet allocated");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   281
    E* old_ptr = _data;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   282
    if ((_data = (E*)raw_reallocate((void*)_data, sizeof(E),
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   283
      _max_size + DEFAULT_PTR_ARRAY_SIZE)) == NULL) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   284
      _data = old_ptr;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   285
      return false;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   286
    } else {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   287
      _max_size += DEFAULT_PTR_ARRAY_SIZE;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   288
      if (_init_elements) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   289
        for (int index = _size; index < _max_size; index ++) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   290
          ::new ((void*)&_data[index]) E();
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   291
        }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   292
      }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   293
      return true;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   294
    }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   295
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   296
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   297
  void* raw_allocate(size_t elementSize, int items) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   298
    return os::malloc(elementSize * items, mtNMT);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   299
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   300
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   301
  void* raw_reallocate(void* ptr, size_t elementSize, int items) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   302
    return os::realloc(ptr, elementSize * items, mtNMT);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   303
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   304
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   305
  void  raw_free(void* ptr) {
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   306
    os::free(ptr, mtNMT);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   307
  }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   308
};
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   309
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents:
diff changeset
   310
#endif // SHARE_VM_UTILITIES_MEM_PTR_ARRAY_HPP