zgu@3900: /* zgu@3900: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. zgu@3900: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. zgu@3900: * zgu@3900: * This code is free software; you can redistribute it and/or modify it zgu@3900: * under the terms of the GNU General Public License version 2 only, as zgu@3900: * published by the Free Software Foundation. zgu@3900: * zgu@3900: * This code is distributed in the hope that it will be useful, but WITHOUT zgu@3900: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or zgu@3900: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License zgu@3900: * version 2 for more details (a copy is included in the LICENSE file that zgu@3900: * accompanied this code). zgu@3900: * zgu@3900: * You should have received a copy of the GNU General Public License version zgu@3900: * 2 along with this work; if not, write to the Free Software Foundation, zgu@3900: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. zgu@3900: * zgu@3900: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA zgu@3900: * or visit www.oracle.com if you need additional information or have any zgu@3900: * questions. zgu@3900: * zgu@3900: */ zgu@3900: #ifndef SHARE_VM_UTILITIES_MEM_PTR_ARRAY_HPP zgu@3900: #define SHARE_VM_UTILITIES_MEM_PTR_ARRAY_HPP zgu@3900: zgu@3900: #include "memory/allocation.hpp" zgu@3900: #include "services/memPtr.hpp" zgu@3900: zgu@3900: class MemPtr; zgu@3900: class MemRecorder; zgu@3900: class ArenaInfo; zgu@3900: class MemSnapshot; zgu@3900: zgu@3900: extern "C" { zgu@3900: typedef int (*FN_SORT)(const void *, const void *); zgu@3900: } zgu@3900: zgu@3900: zgu@3900: // Memory pointer array interface. This array is used by NMT to hold zgu@3900: // various memory block information. zgu@3900: // The memory pointer arrays are usually walked with their iterators. zgu@3900: zgu@3900: class MemPointerArray : public CHeapObj { zgu@3900: public: zgu@3900: virtual ~MemPointerArray() { } zgu@3900: zgu@3900: // return true if it can not allocate storage for the data zgu@3900: virtual bool out_of_memory() const = 0; zgu@3900: virtual bool is_empty() const = 0; zgu@3900: virtual bool is_full() = 0; zgu@3900: virtual int length() const = 0; zgu@3900: virtual void clear() = 0; zgu@3900: virtual bool append(MemPointer* ptr) = 0; zgu@3900: virtual bool insert_at(MemPointer* ptr, int pos) = 0; zgu@3900: virtual bool remove_at(int pos) = 0; zgu@3900: virtual MemPointer* at(int index) const = 0; zgu@3900: virtual void sort(FN_SORT fn) = 0; zgu@3900: virtual size_t instance_size() const = 0; zgu@3900: virtual bool shrink() = 0; zgu@3900: zgu@3994: NOT_PRODUCT(virtual int capacity() const = 0;) zgu@3900: }; zgu@3900: zgu@3900: // Iterator interface zgu@3900: class MemPointerArrayIterator VALUE_OBJ_CLASS_SPEC { zgu@3900: public: zgu@3900: // return the pointer at current position zgu@3900: virtual MemPointer* current() const = 0; zgu@3900: // return the next pointer and advance current position zgu@3900: virtual MemPointer* next() = 0; zgu@3900: // return next pointer without advancing current position zgu@3900: virtual MemPointer* peek_next() const = 0; zgu@3900: // return previous pointer without changing current position zgu@3900: virtual MemPointer* peek_prev() const = 0; zgu@3900: // remove the pointer at current position zgu@3900: virtual void remove() = 0; zgu@3900: // insert the pointer at current position zgu@3900: virtual bool insert(MemPointer* ptr) = 0; zgu@3900: // insert specified element after current position and zgu@3900: // move current position to newly inserted position zgu@3900: virtual bool insert_after(MemPointer* ptr) = 0; zgu@3900: }; zgu@3900: zgu@3900: // implementation class zgu@3900: class MemPointerArrayIteratorImpl : public MemPointerArrayIterator { zgu@3900: protected: zgu@3900: MemPointerArray* _array; zgu@3900: int _pos; zgu@3900: zgu@3900: public: zgu@3900: MemPointerArrayIteratorImpl(MemPointerArray* arr) { zgu@3900: assert(arr != NULL, "Parameter check"); zgu@3900: _array = arr; zgu@3900: _pos = 0; zgu@3900: } zgu@3900: zgu@3900: virtual MemPointer* current() const { zgu@3900: if (_pos < _array->length()) { zgu@3900: return _array->at(_pos); zgu@3900: } zgu@3900: return NULL; zgu@3900: } zgu@3900: zgu@3900: virtual MemPointer* next() { zgu@3900: if (_pos + 1 < _array->length()) { zgu@3900: return _array->at(++_pos); zgu@3900: } zgu@3900: _pos = _array->length(); zgu@3900: return NULL; zgu@3900: } zgu@3900: zgu@3900: virtual MemPointer* peek_next() const { zgu@3900: if (_pos + 1 < _array->length()) { zgu@3900: return _array->at(_pos + 1); zgu@3900: } zgu@3900: return NULL; zgu@3900: } zgu@3900: zgu@3900: virtual MemPointer* peek_prev() const { zgu@3900: if (_pos > 0) { zgu@3900: return _array->at(_pos - 1); zgu@3900: } zgu@3900: return NULL; zgu@3900: } zgu@3900: zgu@3900: virtual void remove() { zgu@3900: if (_pos < _array->length()) { zgu@3900: _array->remove_at(_pos); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: virtual bool insert(MemPointer* ptr) { zgu@3900: return _array->insert_at(ptr, _pos); zgu@3900: } zgu@3900: zgu@3900: virtual bool insert_after(MemPointer* ptr) { zgu@3900: if (_array->insert_at(ptr, _pos + 1)) { zgu@3900: _pos ++; zgu@3900: return true; zgu@3900: } zgu@3900: return false; zgu@3900: } zgu@3900: }; zgu@3900: zgu@3900: zgu@3900: zgu@3900: // Memory pointer array implementation. zgu@3900: // This implementation implements expandable array zgu@3900: #define DEFAULT_PTR_ARRAY_SIZE 1024 zgu@3900: zgu@3900: template class MemPointerArrayImpl : public MemPointerArray { zgu@3900: private: zgu@3900: int _max_size; zgu@3900: int _size; zgu@3900: bool _init_elements; zgu@3900: E* _data; zgu@3900: zgu@3900: public: zgu@3900: MemPointerArrayImpl(int initial_size = DEFAULT_PTR_ARRAY_SIZE, bool init_elements = true): zgu@3900: _max_size(initial_size), _size(0), _init_elements(init_elements) { zgu@3900: _data = (E*)raw_allocate(sizeof(E), initial_size); zgu@3900: if (_init_elements) { zgu@3900: for (int index = 0; index < _max_size; index ++) { zgu@3900: ::new ((void*)&_data[index]) E(); zgu@3900: } zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: virtual ~MemPointerArrayImpl() { zgu@3900: if (_data != NULL) { zgu@3900: raw_free(_data); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: public: zgu@3900: bool out_of_memory() const { zgu@3900: return (_data == NULL); zgu@3900: } zgu@3900: zgu@3900: size_t instance_size() const { zgu@3900: return sizeof(MemPointerArrayImpl) + _max_size * sizeof(E); zgu@3900: } zgu@3900: zgu@3900: bool is_empty() const { zgu@3900: assert(_data != NULL, "Just check"); zgu@3900: return _size == 0; zgu@3900: } zgu@3900: zgu@3900: bool is_full() { zgu@3900: assert(_data != NULL, "Just check"); zgu@3900: if (_size < _max_size) { zgu@3900: return false; zgu@3900: } else { zgu@3900: return !expand_array(); zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: int length() const { zgu@3900: assert(_data != NULL, "Just check"); zgu@3900: return _size; zgu@3900: } zgu@3900: zgu@3994: NOT_PRODUCT(int capacity() const { return _max_size; }) zgu@3900: zgu@3900: void clear() { zgu@3900: assert(_data != NULL, "Just check"); zgu@3900: _size = 0; zgu@3900: } zgu@3900: zgu@3900: bool append(MemPointer* ptr) { zgu@3900: assert(_data != NULL, "Just check"); zgu@3900: if (is_full()) { zgu@3900: return false; zgu@3900: } zgu@3900: _data[_size ++] = *(E*)ptr; zgu@3900: return true; zgu@3900: } zgu@3900: zgu@3900: bool insert_at(MemPointer* ptr, int pos) { zgu@3900: assert(_data != NULL, "Just check"); zgu@3900: if (is_full()) { zgu@3900: return false; zgu@3900: } zgu@3900: for (int index = _size; index > pos; index --) { zgu@3900: _data[index] = _data[index - 1]; zgu@3900: } zgu@3900: _data[pos] = *(E*)ptr; zgu@3900: _size ++; zgu@3900: return true; zgu@3900: } zgu@3900: zgu@3900: bool remove_at(int pos) { zgu@3900: assert(_data != NULL, "Just check"); zgu@3900: if (_size <= pos && pos >= 0) { zgu@3900: return false; zgu@3900: } zgu@3900: -- _size; zgu@3900: zgu@3900: for (int index = pos; index < _size; index ++) { zgu@3900: _data[index] = _data[index + 1]; zgu@3900: } zgu@3900: return true; zgu@3900: } zgu@3900: zgu@3900: MemPointer* at(int index) const { zgu@3900: assert(_data != NULL, "Just check"); zgu@3900: assert(index >= 0 && index < _size, "illegal index"); zgu@3900: return &_data[index]; zgu@3900: } zgu@3900: zgu@3900: bool shrink() { zgu@3900: float used = ((float)_size) / ((float)_max_size); zgu@3900: if (used < 0.40) { zgu@3900: E* old_ptr = _data; zgu@3900: int new_size = ((_max_size) / (2 * DEFAULT_PTR_ARRAY_SIZE) + 1) * DEFAULT_PTR_ARRAY_SIZE; zgu@3900: _data = (E*)raw_reallocate(_data, sizeof(E), new_size); zgu@3900: if (_data == NULL) { zgu@3900: _data = old_ptr; zgu@3900: return false; zgu@3900: } else { zgu@3900: _max_size = new_size; zgu@3900: return true; zgu@3900: } zgu@3900: } zgu@3900: return false; zgu@3900: } zgu@3900: zgu@3900: void sort(FN_SORT fn) { zgu@3900: assert(_data != NULL, "Just check"); zgu@3900: qsort((void*)_data, _size, sizeof(E), fn); zgu@3900: } zgu@3900: zgu@3900: private: zgu@3900: bool expand_array() { zgu@3900: assert(_data != NULL, "Not yet allocated"); zgu@3900: E* old_ptr = _data; zgu@3900: if ((_data = (E*)raw_reallocate((void*)_data, sizeof(E), zgu@3900: _max_size + DEFAULT_PTR_ARRAY_SIZE)) == NULL) { zgu@3900: _data = old_ptr; zgu@3900: return false; zgu@3900: } else { zgu@3900: _max_size += DEFAULT_PTR_ARRAY_SIZE; zgu@3900: if (_init_elements) { zgu@3900: for (int index = _size; index < _max_size; index ++) { zgu@3900: ::new ((void*)&_data[index]) E(); zgu@3900: } zgu@3900: } zgu@3900: return true; zgu@3900: } zgu@3900: } zgu@3900: zgu@3900: void* raw_allocate(size_t elementSize, int items) { zgu@3900: return os::malloc(elementSize * items, mtNMT); zgu@3900: } zgu@3900: zgu@3900: void* raw_reallocate(void* ptr, size_t elementSize, int items) { zgu@3900: return os::realloc(ptr, elementSize * items, mtNMT); zgu@3900: } zgu@3900: zgu@3900: void raw_free(void* ptr) { zgu@3900: os::free(ptr, mtNMT); zgu@3900: } zgu@3900: }; zgu@3900: zgu@3900: #endif // SHARE_VM_UTILITIES_MEM_PTR_ARRAY_HPP