src/share/vm/services/memPtrArray.hpp

changeset 7074
833b0f92429a
parent 7073
4d3a43351904
child 7075
ac12996df59b
     1.1 --- a/src/share/vm/services/memPtrArray.hpp	Wed Aug 27 09:36:55 2014 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,306 +0,0 @@
     1.4 -/*
     1.5 - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 - *
     1.8 - * This code is free software; you can redistribute it and/or modify it
     1.9 - * under the terms of the GNU General Public License version 2 only, as
    1.10 - * published by the Free Software Foundation.
    1.11 - *
    1.12 - * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 - * version 2 for more details (a copy is included in the LICENSE file that
    1.16 - * accompanied this code).
    1.17 - *
    1.18 - * You should have received a copy of the GNU General Public License version
    1.19 - * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 - *
    1.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 - * or visit www.oracle.com if you need additional information or have any
    1.24 - * questions.
    1.25 - *
    1.26 - */
    1.27 -#ifndef SHARE_VM_UTILITIES_MEM_PTR_ARRAY_HPP
    1.28 -#define SHARE_VM_UTILITIES_MEM_PTR_ARRAY_HPP
    1.29 -
    1.30 -#include "memory/allocation.hpp"
    1.31 -#include "services/memPtr.hpp"
    1.32 -
    1.33 -class MemPtr;
    1.34 -class MemRecorder;
    1.35 -class ArenaInfo;
    1.36 -class MemSnapshot;
    1.37 -
    1.38 -extern "C" {
    1.39 -  typedef int (*FN_SORT)(const void *, const void *);
    1.40 -}
    1.41 -
    1.42 -
    1.43 -// Memory pointer array interface. This array is used by NMT to hold
    1.44 -// various memory block information.
    1.45 -// The memory pointer arrays are usually walked with their iterators.
    1.46 -
    1.47 -class MemPointerArray : public CHeapObj<mtNMT> {
    1.48 - public:
    1.49 -  virtual ~MemPointerArray() { }
    1.50 -
    1.51 -  // return true if it can not allocate storage for the data
    1.52 -  virtual bool out_of_memory() const = 0;
    1.53 -  virtual bool is_empty() const = 0;
    1.54 -  virtual bool is_full() = 0;
    1.55 -  virtual int  length() const = 0;
    1.56 -  virtual void clear() = 0;
    1.57 -  virtual bool append(MemPointer* ptr) = 0;
    1.58 -  virtual bool insert_at(MemPointer* ptr, int pos) = 0;
    1.59 -  virtual bool remove_at(int pos) = 0;
    1.60 -  virtual MemPointer* at(int index) const = 0;
    1.61 -  virtual void sort(FN_SORT fn) = 0;
    1.62 -  virtual size_t instance_size() const = 0;
    1.63 -  virtual bool shrink() = 0;
    1.64 -
    1.65 -  NOT_PRODUCT(virtual int capacity() const = 0;)
    1.66 -};
    1.67 -
    1.68 -// Iterator interface
    1.69 -class MemPointerArrayIterator VALUE_OBJ_CLASS_SPEC {
    1.70 - public:
    1.71 -  // return the pointer at current position
    1.72 -  virtual MemPointer* current() const = 0;
    1.73 -  // return the next pointer and advance current position
    1.74 -  virtual MemPointer* next() = 0;
    1.75 -  // return next pointer without advancing current position
    1.76 -  virtual MemPointer* peek_next() const = 0;
    1.77 -  // return previous pointer without changing current position
    1.78 -  virtual MemPointer* peek_prev() const = 0;
    1.79 -  // remove the pointer at current position
    1.80 -  virtual void        remove() = 0;
    1.81 -  // insert the pointer at current position
    1.82 -  virtual bool        insert(MemPointer* ptr) = 0;
    1.83 -  // insert specified element after current position and
    1.84 -  // move current position to newly inserted position
    1.85 -  virtual bool        insert_after(MemPointer* ptr) = 0;
    1.86 -};
    1.87 -
    1.88 -// implementation class
    1.89 -class MemPointerArrayIteratorImpl : public MemPointerArrayIterator {
    1.90 - protected:
    1.91 -  MemPointerArray*  _array;
    1.92 -  int               _pos;
    1.93 -
    1.94 - public:
    1.95 -  MemPointerArrayIteratorImpl(MemPointerArray* arr) {
    1.96 -    assert(arr != NULL, "Parameter check");
    1.97 -    _array = arr;
    1.98 -    _pos = 0;
    1.99 -  }
   1.100 -
   1.101 -  virtual MemPointer* current() const {
   1.102 -    if (_pos < _array->length()) {
   1.103 -      return _array->at(_pos);
   1.104 -    }
   1.105 -    return NULL;
   1.106 -  }
   1.107 -
   1.108 -  virtual MemPointer* next() {
   1.109 -    if (_pos + 1 < _array->length()) {
   1.110 -      return _array->at(++_pos);
   1.111 -    }
   1.112 -    _pos = _array->length();
   1.113 -    return NULL;
   1.114 -  }
   1.115 -
   1.116 -  virtual MemPointer* peek_next() const {
   1.117 -    if (_pos + 1 < _array->length()) {
   1.118 -      return _array->at(_pos + 1);
   1.119 -    }
   1.120 -    return NULL;
   1.121 -  }
   1.122 -
   1.123 -  virtual MemPointer* peek_prev() const {
   1.124 -    if (_pos > 0) {
   1.125 -      return _array->at(_pos - 1);
   1.126 -    }
   1.127 -    return NULL;
   1.128 -  }
   1.129 -
   1.130 -  virtual void remove() {
   1.131 -    if (_pos < _array->length()) {
   1.132 -      _array->remove_at(_pos);
   1.133 -    }
   1.134 -  }
   1.135 -
   1.136 -  virtual bool insert(MemPointer* ptr) {
   1.137 -    return _array->insert_at(ptr, _pos);
   1.138 -  }
   1.139 -
   1.140 -  virtual bool insert_after(MemPointer* ptr) {
   1.141 -    if (_array->insert_at(ptr, _pos + 1)) {
   1.142 -      _pos ++;
   1.143 -      return true;
   1.144 -    }
   1.145 -    return false;
   1.146 -  }
   1.147 -};
   1.148 -
   1.149 -
   1.150 -
   1.151 -// Memory pointer array implementation.
   1.152 -// This implementation implements expandable array
   1.153 -#define DEFAULT_PTR_ARRAY_SIZE 1024
   1.154 -
   1.155 -template <class E> class MemPointerArrayImpl : public MemPointerArray {
   1.156 - private:
   1.157 -  int                   _max_size;
   1.158 -  int                   _size;
   1.159 -  bool                  _init_elements;
   1.160 -  E*                    _data;
   1.161 -
   1.162 - public:
   1.163 -  MemPointerArrayImpl(int initial_size = DEFAULT_PTR_ARRAY_SIZE, bool init_elements = true):
   1.164 -   _max_size(initial_size), _size(0), _init_elements(init_elements) {
   1.165 -    _data = (E*)raw_allocate(sizeof(E), initial_size);
   1.166 -    if (_init_elements) {
   1.167 -      for (int index = 0; index < _max_size; index ++) {
   1.168 -        ::new ((void*)&_data[index]) E();
   1.169 -      }
   1.170 -    }
   1.171 -  }
   1.172 -
   1.173 -  virtual ~MemPointerArrayImpl() {
   1.174 -    if (_data != NULL) {
   1.175 -      raw_free(_data);
   1.176 -    }
   1.177 -  }
   1.178 -
   1.179 - public:
   1.180 -  bool out_of_memory() const {
   1.181 -    return (_data == NULL);
   1.182 -  }
   1.183 -
   1.184 -  size_t instance_size() const {
   1.185 -    return sizeof(MemPointerArrayImpl<E>) + _max_size * sizeof(E);
   1.186 -  }
   1.187 -
   1.188 -  bool is_empty() const {
   1.189 -    assert(_data != NULL, "Just check");
   1.190 -    return _size == 0;
   1.191 -  }
   1.192 -
   1.193 -  bool is_full() {
   1.194 -    assert(_data != NULL, "Just check");
   1.195 -    if (_size < _max_size) {
   1.196 -      return false;
   1.197 -    } else {
   1.198 -      return !expand_array();
   1.199 -    }
   1.200 -  }
   1.201 -
   1.202 -  int length() const {
   1.203 -    assert(_data != NULL, "Just check");
   1.204 -    return _size;
   1.205 -  }
   1.206 -
   1.207 -  NOT_PRODUCT(int capacity() const { return _max_size; })
   1.208 -
   1.209 -  void clear() {
   1.210 -    assert(_data != NULL, "Just check");
   1.211 -    _size = 0;
   1.212 -  }
   1.213 -
   1.214 -  bool append(MemPointer* ptr) {
   1.215 -    assert(_data != NULL, "Just check");
   1.216 -    if (is_full()) {
   1.217 -      return false;
   1.218 -    }
   1.219 -    _data[_size ++] = *(E*)ptr;
   1.220 -    return true;
   1.221 -  }
   1.222 -
   1.223 -  bool insert_at(MemPointer* ptr, int pos) {
   1.224 -    assert(_data != NULL, "Just check");
   1.225 -    if (is_full()) {
   1.226 -      return false;
   1.227 -    }
   1.228 -    for (int index = _size; index > pos; index --) {
   1.229 -      _data[index] = _data[index - 1];
   1.230 -    }
   1.231 -    _data[pos] = *(E*)ptr;
   1.232 -    _size ++;
   1.233 -    return true;
   1.234 -  }
   1.235 -
   1.236 -  bool remove_at(int pos) {
   1.237 -    assert(_data != NULL, "Just check");
   1.238 -    if (_size <= pos && pos >= 0) {
   1.239 -      return false;
   1.240 -    }
   1.241 -    -- _size;
   1.242 -
   1.243 -    for (int index = pos; index < _size; index ++) {
   1.244 -      _data[index] = _data[index + 1];
   1.245 -    }
   1.246 -    return true;
   1.247 -  }
   1.248 -
   1.249 -  MemPointer* at(int index) const {
   1.250 -    assert(_data != NULL, "Just check");
   1.251 -    assert(index >= 0 && index < _size, "illegal index");
   1.252 -    return &_data[index];
   1.253 -  }
   1.254 -
   1.255 -  bool shrink() {
   1.256 -    float used = ((float)_size) / ((float)_max_size);
   1.257 -    if (used < 0.40) {
   1.258 -      E* old_ptr = _data;
   1.259 -      int new_size = ((_max_size) / (2 * DEFAULT_PTR_ARRAY_SIZE) + 1) * DEFAULT_PTR_ARRAY_SIZE;
   1.260 -      _data = (E*)raw_reallocate(_data, sizeof(E), new_size);
   1.261 -      if (_data == NULL) {
   1.262 -        _data = old_ptr;
   1.263 -        return false;
   1.264 -      } else {
   1.265 -        _max_size = new_size;
   1.266 -        return true;
   1.267 -      }
   1.268 -    }
   1.269 -    return false;
   1.270 -  }
   1.271 -
   1.272 -  void sort(FN_SORT fn) {
   1.273 -    assert(_data != NULL, "Just check");
   1.274 -    qsort((void*)_data, _size, sizeof(E), fn);
   1.275 -  }
   1.276 -
   1.277 - private:
   1.278 -  bool  expand_array() {
   1.279 -    assert(_data != NULL, "Not yet allocated");
   1.280 -    E* old_ptr = _data;
   1.281 -    if ((_data = (E*)raw_reallocate((void*)_data, sizeof(E),
   1.282 -      _max_size + DEFAULT_PTR_ARRAY_SIZE)) == NULL) {
   1.283 -      _data = old_ptr;
   1.284 -      return false;
   1.285 -    } else {
   1.286 -      _max_size += DEFAULT_PTR_ARRAY_SIZE;
   1.287 -      if (_init_elements) {
   1.288 -        for (int index = _size; index < _max_size; index ++) {
   1.289 -          ::new ((void*)&_data[index]) E();
   1.290 -        }
   1.291 -      }
   1.292 -      return true;
   1.293 -    }
   1.294 -  }
   1.295 -
   1.296 -  void* raw_allocate(size_t elementSize, int items) {
   1.297 -    return os::malloc(elementSize * items, mtNMT);
   1.298 -  }
   1.299 -
   1.300 -  void* raw_reallocate(void* ptr, size_t elementSize, int items) {
   1.301 -    return os::realloc(ptr, elementSize * items, mtNMT);
   1.302 -  }
   1.303 -
   1.304 -  void  raw_free(void* ptr) {
   1.305 -    os::free(ptr, mtNMT);
   1.306 -  }
   1.307 -};
   1.308 -
   1.309 -#endif // SHARE_VM_UTILITIES_MEM_PTR_ARRAY_HPP

mercurial