0N/A/*
3619N/A * Copyright (c) 1997, 2012, 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_GROWABLEARRAY_HPP
1879N/A#define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
1879N/A
1879N/A#include "memory/allocation.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "utilities/debug.hpp"
1879N/A#include "utilities/globalDefinitions.hpp"
1879N/A#include "utilities/top.hpp"
1879N/A
0N/A// A growable array.
0N/A
0N/A/*************************************************************************/
0N/A/* */
0N/A/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING */
0N/A/* */
0N/A/* Should you use GrowableArrays to contain handles you must be certain */
0N/A/* the the GrowableArray does not outlive the HandleMark that contains */
0N/A/* the handles. Since GrowableArrays are typically resource allocated */
0N/A/* the following is an example of INCORRECT CODE, */
0N/A/* */
0N/A/* ResourceMark rm; */
0N/A/* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size); */
0N/A/* if (blah) { */
0N/A/* while (...) { */
0N/A/* HandleMark hm; */
0N/A/* ... */
0N/A/* Handle h(THREAD, some_oop); */
0N/A/* arr->append(h); */
0N/A/* } */
0N/A/* } */
0N/A/* if (arr->length() != 0 ) { */
0N/A/* oop bad_oop = arr->at(0)(); // Handle is BAD HERE. */
0N/A/* ... */
0N/A/* } */
0N/A/* */
0N/A/* If the GrowableArrays you are creating is C_Heap allocated then it */
0N/A/* hould not old handles since the handles could trivially try and */
0N/A/* outlive their HandleMark. In some situations you might need to do */
0N/A/* this and it would be legal but be very careful and see if you can do */
0N/A/* the code in some other manner. */
0N/A/* */
0N/A/*************************************************************************/
0N/A
0N/A// To call default constructor the placement operator new() is used.
0N/A// It should be empty (it only returns the passed void* pointer).
0N/A// The definition of placement operator new(size_t, void*) in the <new>.
0N/A
0N/A#include <new>
0N/A
0N/A// Need the correct linkage to call qsort without warnings
0N/Aextern "C" {
0N/A typedef int (*_sort_Fn)(const void *, const void *);
0N/A}
0N/A
0N/Aclass GenericGrowableArray : public ResourceObj {
2772N/A friend class VMStructs;
2772N/A
0N/A protected:
0N/A int _len; // current length
0N/A int _max; // maximum length
0N/A Arena* _arena; // Indicates where allocation occurs:
0N/A // 0 means default ResourceArea
0N/A // 1 means on C heap
0N/A // otherwise, allocate in _arena
3863N/A
3863N/A MEMFLAGS _memflags; // memory type if allocation in C heap
3863N/A
0N/A#ifdef ASSERT
0N/A int _nesting; // resource area nesting at creation
0N/A void set_nesting();
0N/A void check_nesting();
0N/A#else
0N/A#define set_nesting();
0N/A#define check_nesting();
0N/A#endif
0N/A
0N/A // Where are we going to allocate memory?
0N/A bool on_C_heap() { return _arena == (Arena*)1; }
0N/A bool on_stack () { return _arena == NULL; }
0N/A bool on_arena () { return _arena > (Arena*)1; }
0N/A
0N/A // This GA will use the resource stack for storage if c_heap==false,
0N/A // Else it will use the C heap. Use clear_and_deallocate to avoid leaks.
3863N/A GenericGrowableArray(int initial_size, int initial_len, bool c_heap, MEMFLAGS flags = mtNone) {
0N/A _len = initial_len;
0N/A _max = initial_size;
3863N/A _memflags = flags;
3863N/A
3863N/A // memory type has to be specified for C heap allocation
3863N/A assert(!(c_heap && flags == mtNone), "memory type not specified for C heap object");
3863N/A
0N/A assert(_len >= 0 && _len <= _max, "initial_len too big");
0N/A _arena = (c_heap ? (Arena*)1 : NULL);
0N/A set_nesting();
1605N/A assert(!on_C_heap() || allocated_on_C_heap(), "growable array must be on C heap if elements are");
1605N/A assert(!on_stack() ||
1605N/A (allocated_on_res_area() || allocated_on_stack()),
1605N/A "growable array must be on stack if elements are not on arena and not on C heap");
0N/A }
0N/A
0N/A // This GA will use the given arena for storage.
0N/A // Consider using new(arena) GrowableArray<T> to allocate the header.
0N/A GenericGrowableArray(Arena* arena, int initial_size, int initial_len) {
0N/A _len = initial_len;
0N/A _max = initial_size;
0N/A assert(_len >= 0 && _len <= _max, "initial_len too big");
0N/A _arena = arena;
3863N/A _memflags = mtNone;
3863N/A
0N/A assert(on_arena(), "arena has taken on reserved value 0 or 1");
1605N/A // Relax next assert to allow object allocation on resource area,
1605N/A // on stack or embedded into an other object.
1605N/A assert(allocated_on_arena() || allocated_on_stack(),
1605N/A "growable array must be on arena or on stack if elements are on arena");
0N/A }
0N/A
0N/A void* raw_allocate(int elementSize);
432N/A
432N/A // some uses pass the Thread explicitly for speed (4990299 tuning)
432N/A void* raw_allocate(Thread* thread, int elementSize) {
432N/A assert(on_stack(), "fast ResourceObj path only");
432N/A return (void*)resource_allocate_bytes(thread, elementSize * _max);
432N/A }
0N/A};
0N/A
0N/Atemplate<class E> class GrowableArray : public GenericGrowableArray {
2772N/A friend class VMStructs;
2772N/A
0N/A private:
0N/A E* _data; // data array
0N/A
0N/A void grow(int j);
0N/A void raw_at_put_grow(int i, const E& p, const E& fill);
0N/A void clear_and_deallocate();
0N/A public:
432N/A GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
432N/A _data = (E*)raw_allocate(thread, sizeof(E));
432N/A for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
432N/A }
432N/A
3863N/A GrowableArray(int initial_size, bool C_heap = false, MEMFLAGS F = mtInternal)
3863N/A : GenericGrowableArray(initial_size, 0, C_heap, F) {
0N/A _data = (E*)raw_allocate(sizeof(E));
0N/A for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
0N/A }
0N/A
3863N/A GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false, MEMFLAGS memflags = mtInternal)
3863N/A : GenericGrowableArray(initial_size, initial_len, C_heap, memflags) {
0N/A _data = (E*)raw_allocate(sizeof(E));
0N/A int i = 0;
0N/A for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
0N/A for (; i < _max; i++) ::new ((void*)&_data[i]) E();
0N/A }
0N/A
0N/A GrowableArray(Arena* arena, int initial_size, int initial_len, const E& filler) : GenericGrowableArray(arena, initial_size, initial_len) {
0N/A _data = (E*)raw_allocate(sizeof(E));
0N/A int i = 0;
0N/A for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
0N/A for (; i < _max; i++) ::new ((void*)&_data[i]) E();
0N/A }
0N/A
0N/A GrowableArray() : GenericGrowableArray(2, 0, false) {
0N/A _data = (E*)raw_allocate(sizeof(E));
0N/A ::new ((void*)&_data[0]) E();
0N/A ::new ((void*)&_data[1]) E();
0N/A }
0N/A
0N/A // Does nothing for resource and arena objects
0N/A ~GrowableArray() { if (on_C_heap()) clear_and_deallocate(); }
0N/A
0N/A void clear() { _len = 0; }
0N/A int length() const { return _len; }
0N/A void trunc_to(int l) { assert(l <= _len,"cannot increase length"); _len = l; }
0N/A bool is_empty() const { return _len == 0; }
0N/A bool is_nonempty() const { return _len != 0; }
0N/A bool is_full() const { return _len == _max; }
0N/A DEBUG_ONLY(E* data_addr() const { return _data; })
0N/A
0N/A void print();
0N/A
432N/A int append(const E& elem) {
0N/A check_nesting();
0N/A if (_len == _max) grow(_len);
432N/A int idx = _len++;
432N/A _data[idx] = elem;
432N/A return idx;
0N/A }
0N/A
3619N/A bool append_if_missing(const E& elem) {
3619N/A // Returns TRUE if elem is added.
3619N/A bool missed = !contains(elem);
3619N/A if (missed) append(elem);
3619N/A return missed;
0N/A }
0N/A
0N/A E at(int i) const {
0N/A assert(0 <= i && i < _len, "illegal index");
0N/A return _data[i];
0N/A }
0N/A
0N/A E* adr_at(int i) const {
0N/A assert(0 <= i && i < _len, "illegal index");
0N/A return &_data[i];
0N/A }
0N/A
0N/A E first() const {
0N/A assert(_len > 0, "empty list");
0N/A return _data[0];
0N/A }
0N/A
0N/A E top() const {
0N/A assert(_len > 0, "empty list");
0N/A return _data[_len-1];
0N/A }
0N/A
0N/A void push(const E& elem) { append(elem); }
0N/A
0N/A E pop() {
0N/A assert(_len > 0, "empty list");
0N/A return _data[--_len];
0N/A }
0N/A
0N/A void at_put(int i, const E& elem) {
0N/A assert(0 <= i && i < _len, "illegal index");
0N/A _data[i] = elem;
0N/A }
0N/A
0N/A E at_grow(int i, const E& fill = E()) {
0N/A assert(0 <= i, "negative index");
0N/A check_nesting();
0N/A if (i >= _len) {
0N/A if (i >= _max) grow(i);
0N/A for (int j = _len; j <= i; j++)
0N/A _data[j] = fill;
0N/A _len = i+1;
0N/A }
0N/A return _data[i];
0N/A }
0N/A
0N/A void at_put_grow(int i, const E& elem, const E& fill = E()) {
0N/A assert(0 <= i, "negative index");
0N/A check_nesting();
0N/A raw_at_put_grow(i, elem, fill);
0N/A }
0N/A
0N/A bool contains(const E& elem) const {
0N/A for (int i = 0; i < _len; i++) {
0N/A if (_data[i] == elem) return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A int find(const E& elem) const {
0N/A for (int i = 0; i < _len; i++) {
0N/A if (_data[i] == elem) return i;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A int find(void* token, bool f(void*, E)) const {
0N/A for (int i = 0; i < _len; i++) {
0N/A if (f(token, _data[i])) return i;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A int find_at_end(void* token, bool f(void*, E)) const {
0N/A // start at the end of the array
0N/A for (int i = _len-1; i >= 0; i--) {
0N/A if (f(token, _data[i])) return i;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A void remove(const E& elem) {
0N/A for (int i = 0; i < _len; i++) {
0N/A if (_data[i] == elem) {
0N/A for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
0N/A _len--;
0N/A return;
0N/A }
0N/A }
0N/A ShouldNotReachHere();
0N/A }
0N/A
3619N/A // The order is preserved.
0N/A void remove_at(int index) {
0N/A assert(0 <= index && index < _len, "illegal index");
0N/A for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
0N/A _len--;
0N/A }
0N/A
3619N/A // The order is changed.
3619N/A void delete_at(int index) {
3619N/A assert(0 <= index && index < _len, "illegal index");
3619N/A if (index < --_len) {
3619N/A // Replace removed element with last one.
3619N/A _data[index] = _data[_len];
3619N/A }
3619N/A }
3619N/A
1080N/A // inserts the given element before the element at index i
1080N/A void insert_before(const int idx, const E& elem) {
1080N/A check_nesting();
1080N/A if (_len == _max) grow(_len);
1080N/A for (int j = _len - 1; j >= idx; j--) {
1080N/A _data[j + 1] = _data[j];
1080N/A }
1080N/A _len++;
1080N/A _data[idx] = elem;
1080N/A }
1080N/A
0N/A void appendAll(const GrowableArray<E>* l) {
0N/A for (int i = 0; i < l->_len; i++) {
0N/A raw_at_put_grow(_len, l->_data[i], 0);
0N/A }
0N/A }
0N/A
0N/A void sort(int f(E*,E*)) {
0N/A qsort(_data, length(), sizeof(E), (_sort_Fn)f);
0N/A }
0N/A // sort by fixed-stride sub arrays:
0N/A void sort(int f(E*,E*), int stride) {
0N/A qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
0N/A }
0N/A};
0N/A
0N/A// Global GrowableArray methods (one instance in the library per each 'E' type).
0N/A
0N/Atemplate<class E> void GrowableArray<E>::grow(int j) {
0N/A // grow the array by doubling its size (amortized growth)
0N/A int old_max = _max;
0N/A if (_max == 0) _max = 1; // prevent endless loop
0N/A while (j >= _max) _max = _max*2;
0N/A // j < _max
0N/A E* newData = (E*)raw_allocate(sizeof(E));
0N/A int i = 0;
0N/A for ( ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]);
0N/A for ( ; i < _max; i++) ::new ((void*)&newData[i]) E();
0N/A for (i = 0; i < old_max; i++) _data[i].~E();
0N/A if (on_C_heap() && _data != NULL) {
0N/A FreeHeap(_data);
0N/A }
0N/A _data = newData;
0N/A}
0N/A
0N/Atemplate<class E> void GrowableArray<E>::raw_at_put_grow(int i, const E& p, const E& fill) {
0N/A if (i >= _len) {
0N/A if (i >= _max) grow(i);
0N/A for (int j = _len; j < i; j++)
0N/A _data[j] = fill;
0N/A _len = i+1;
0N/A }
0N/A _data[i] = p;
0N/A}
0N/A
0N/A// This function clears and deallocate the data in the growable array that
0N/A// has been allocated on the C heap. It's not public - called by the
0N/A// destructor.
0N/Atemplate<class E> void GrowableArray<E>::clear_and_deallocate() {
0N/A assert(on_C_heap(),
0N/A "clear_and_deallocate should only be called when on C heap");
0N/A clear();
0N/A if (_data != NULL) {
0N/A for (int i = 0; i < _max; i++) _data[i].~E();
0N/A FreeHeap(_data);
0N/A _data = NULL;
0N/A }
0N/A}
0N/A
0N/Atemplate<class E> void GrowableArray<E>::print() {
0N/A tty->print("Growable Array " INTPTR_FORMAT, this);
0N/A tty->print(": length %ld (_max %ld) { ", _len, _max);
0N/A for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
0N/A tty->print("}\n");
0N/A}
1879N/A
1879N/A#endif // SHARE_VM_UTILITIES_GROWABLEARRAY_HPP