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