0N/A/*
1879N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_UTILITIES_ARRAY_HPP
1879N/A#define SHARE_VM_UTILITIES_ARRAY_HPP
1879N/A
1879N/A#include "memory/allocation.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A
0N/A// correct linkage required to compile w/o warnings
0N/A// (must be on file level - cannot be local)
0N/Aextern "C" { typedef int (*ftype)(const void*, const void*); }
0N/A
0N/A
0N/Aclass ResourceArray: public ResourceObj {
0N/A protected:
0N/A int _length; // the number of array elements
0N/A void* _data; // the array memory
0N/A#ifdef ASSERT
0N/A int _nesting; // the resource area nesting level
0N/A#endif
0N/A
0N/A // creation
0N/A ResourceArray() {
0N/A _length = 0;
0N/A _data = NULL;
0N/A DEBUG_ONLY(init_nesting();)
432N/A // client may call initialize, at most once
0N/A }
0N/A
0N/A
0N/A ResourceArray(size_t esize, int length) {
432N/A DEBUG_ONLY(_data = NULL);
432N/A initialize(esize, length);
432N/A }
432N/A
432N/A void initialize(size_t esize, int length) {
0N/A assert(length >= 0, "illegal length");
432N/A assert(_data == NULL, "must be new object");
0N/A _length = length;
0N/A _data = resource_allocate_bytes(esize * length);
0N/A DEBUG_ONLY(init_nesting();)
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A void init_nesting();
0N/A#endif
0N/A
0N/A // helper functions
0N/A void sort (size_t esize, ftype f); // sort the array
0N/A void expand (size_t esize, int i, int& size);// expand the array to include slot i
0N/A void remove_at(size_t esize, int i); // remove the element in slot i
0N/A
0N/A public:
0N/A // standard operations
0N/A int length() const { return _length; }
0N/A bool is_empty() const { return length() == 0; }
0N/A};
0N/A
0N/A
3863N/Atemplate <MEMFLAGS F>class CHeapArray: public CHeapObj<F> {
0N/A protected:
0N/A int _length; // the number of array elements
0N/A void* _data; // the array memory
0N/A
0N/A // creation
0N/A CHeapArray() {
0N/A _length = 0;
0N/A _data = NULL;
0N/A }
0N/A
0N/A
0N/A CHeapArray(size_t esize, int length) {
0N/A assert(length >= 0, "illegal length");
0N/A _length = length;
3863N/A _data = (void*) NEW_C_HEAP_ARRAY(char *, esize * length, F);
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A void init_nesting();
0N/A#endif
0N/A
0N/A // helper functions
0N/A void sort (size_t esize, ftype f); // sort the array
0N/A void expand (size_t esize, int i, int& size);// expand the array to include slot i
0N/A void remove_at(size_t esize, int i); // remove the element in slot i
0N/A
0N/A public:
0N/A // standard operations
0N/A int length() const { return _length; }
0N/A bool is_empty() const { return length() == 0; }
0N/A};
0N/A
0N/A#define define_generic_array(array_name,element_type, base_class) \
0N/A class array_name: public base_class { \
0N/A protected: \
0N/A typedef element_type etype; \
0N/A enum { esize = sizeof(etype) }; \
0N/A \
0N/A void base_remove_at(size_t size, int i) { base_class::remove_at(size, i); } \
0N/A \
0N/A public: \
0N/A /* creation */ \
0N/A array_name() : base_class() {} \
0N/A array_name(const int length) : base_class(esize, length) {} \
432N/A array_name(const int length, const etype fx) { initialize(length, fx); } \
432N/A void initialize(const int length) { base_class::initialize(esize, length); } \
432N/A void initialize(const int length, const etype fx) { \
432N/A initialize(length); \
0N/A for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx; \
0N/A } \
0N/A \
0N/A /* standard operations */ \
0N/A etype& operator [] (const int i) const { \
0N/A assert(0 <= i && i < length(), "index out of bounds"); \
0N/A return ((etype*)_data)[i]; \
0N/A } \
0N/A \
0N/A int index_of(const etype x) const { \
0N/A int i = length(); \
0N/A while (i-- > 0 && ((etype*)_data)[i] != x) ; \
0N/A /* i < 0 || ((etype*)_data)_data[i] == x */ \
0N/A return i; \
0N/A } \
0N/A \
0N/A void sort(int f(etype*, etype*)) { base_class::sort(esize, (ftype)f); } \
0N/A bool contains(const etype x) const { return index_of(x) >= 0; } \
0N/A \
0N/A /* deprecated operations - for compatibility with GrowableArray only */ \
0N/A etype at(const int i) const { return (*this)[i]; } \
0N/A void at_put(const int i, const etype x) { (*this)[i] = x; } \
0N/A etype* adr_at(const int i) { return &(*this)[i]; } \
0N/A int find(const etype x) { return index_of(x); } \
0N/A }; \
0N/A
0N/A
0N/A#define define_array(array_name,element_type) \
0N/A define_generic_array(array_name, element_type, ResourceArray)
0N/A
0N/A
0N/A#define define_stack(stack_name,array_name) \
0N/A class stack_name: public array_name { \
0N/A protected: \
0N/A int _size; \
0N/A \
0N/A void grow(const int i, const etype fx) { \
0N/A assert(i >= length(), "index too small"); \
0N/A if (i >= size()) expand(esize, i, _size); \
0N/A for (int j = length(); j <= i; j++) ((etype*)_data)[j] = fx; \
0N/A _length = i+1; \
0N/A } \
0N/A \
0N/A public: \
0N/A /* creation */ \
432N/A stack_name() : array_name() { _size = 0; } \
432N/A stack_name(const int size) { initialize(size); } \
432N/A stack_name(const int size, const etype fx) { initialize(size, fx); } \
432N/A void initialize(const int size, const etype fx) { \
432N/A _size = size; \
432N/A array_name::initialize(size, fx); \
432N/A /* _length == size, allocation and size are the same */ \
432N/A } \
432N/A void initialize(const int size) { \
432N/A _size = size; \
432N/A array_name::initialize(size); \
432N/A _length = 0; /* reset length to zero; _size records the allocation */ \
432N/A } \
0N/A \
0N/A /* standard operations */ \
0N/A int size() const { return _size; } \
0N/A \
432N/A int push(const etype x) { \
432N/A int len = length(); \
432N/A if (len >= size()) expand(esize, len, _size); \
432N/A ((etype*)_data)[len] = x; \
432N/A _length = len+1; \
432N/A return len; \
0N/A } \
0N/A \
0N/A etype pop() { \
0N/A assert(!is_empty(), "stack is empty"); \
0N/A return ((etype*)_data)[--_length]; \
0N/A } \
0N/A \
0N/A etype top() const { \
0N/A assert(!is_empty(), "stack is empty"); \
0N/A return ((etype*)_data)[length() - 1]; \
0N/A } \
0N/A \
0N/A void push_all(const stack_name* stack) { \
0N/A const int l = stack->length(); \
0N/A for (int i = 0; i < l; i++) push(((etype*)(stack->_data))[i]); \
0N/A } \
0N/A \
0N/A etype at_grow(const int i, const etype fx) { \
0N/A if (i >= length()) grow(i, fx); \
0N/A return ((etype*)_data)[i]; \
0N/A } \
0N/A \
0N/A void at_put_grow(const int i, const etype x, const etype fx) { \
0N/A if (i >= length()) grow(i, fx); \
0N/A ((etype*)_data)[i] = x; \
0N/A } \
0N/A \
0N/A void truncate(const int length) { \
0N/A assert(0 <= length && length <= this->length(), "illegal length"); \
0N/A _length = length; \
0N/A } \
0N/A \
0N/A void remove_at(int i) { base_remove_at(esize, i); } \
0N/A void remove(etype x) { remove_at(index_of(x)); } \
0N/A \
0N/A /* inserts the given element before the element at index i */ \
0N/A void insert_before(const int i, const etype el) { \
0N/A int len = length(); \
0N/A int new_length = len + 1; \
0N/A if (new_length >= size()) expand(esize, new_length, _size); \
0N/A for (int j = len - 1; j >= i; j--) { \
0N/A ((etype*)_data)[j + 1] = ((etype*)_data)[j]; \
0N/A } \
0N/A _length = new_length; \
0N/A at_put(i, el); \
0N/A } \
0N/A \
0N/A /* inserts contents of the given stack before the element at index i */ \
0N/A void insert_before(const int i, const stack_name *st) { \
0N/A if (st->length() == 0) return; \
0N/A int len = length(); \
0N/A int st_len = st->length(); \
0N/A int new_length = len + st_len; \
0N/A if (new_length >= size()) expand(esize, new_length, _size); \
0N/A int j; \
0N/A for (j = len - 1; j >= i; j--) { \
0N/A ((etype*)_data)[j + st_len] = ((etype*)_data)[j]; \
0N/A } \
0N/A for (j = 0; j < st_len; j++) { \
0N/A ((etype*)_data)[i + j] = ((etype*)st->_data)[j]; \
0N/A } \
0N/A _length = new_length; \
0N/A } \
0N/A \
0N/A /* deprecated operations - for compatibility with GrowableArray only */ \
0N/A int capacity() const { return size(); } \
0N/A void clear() { truncate(0); } \
0N/A void trunc_to(const int length) { truncate(length); } \
432N/A int append(const etype x) { return push(x); } \
0N/A void appendAll(const stack_name* stack) { push_all(stack); } \
0N/A etype last() const { return top(); } \
0N/A }; \
0N/A
0N/A
0N/A#define define_resource_list(element_type) \
0N/A define_generic_array(element_type##Array, element_type, ResourceArray) \
0N/A define_stack(element_type##List, element_type##Array)
0N/A
0N/A#define define_resource_pointer_list(element_type) \
0N/A define_generic_array(element_type##Array, element_type *, ResourceArray) \
0N/A define_stack(element_type##List, element_type##Array)
0N/A
0N/A#define define_c_heap_list(element_type) \
0N/A define_generic_array(element_type##Array, element_type, CHeapArray) \
0N/A define_stack(element_type##List, element_type##Array)
0N/A
0N/A#define define_c_heap_pointer_list(element_type) \
0N/A define_generic_array(element_type##Array, element_type *, CHeapArray) \
0N/A define_stack(element_type##List, element_type##Array)
0N/A
0N/A
0N/A// Arrays for basic types
0N/A
0N/Adefine_array(boolArray, bool) define_stack(boolStack, boolArray)
0N/Adefine_array(intArray , int ) define_stack(intStack , intArray )
1879N/A
1879N/A#endif // SHARE_VM_UTILITIES_ARRAY_HPP