Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/array.hpp
32285 views
/*1* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_UTILITIES_ARRAY_HPP25#define SHARE_VM_UTILITIES_ARRAY_HPP2627#include "memory/allocation.hpp"28#include "memory/allocation.inline.hpp"29#include "memory/metaspace.hpp"30#include "runtime/orderAccess.hpp"3132// correct linkage required to compile w/o warnings33// (must be on file level - cannot be local)34extern "C" { typedef int (*ftype)(const void*, const void*); }353637class ResourceArray: public ResourceObj {38protected:39int _length; // the number of array elements40void* _data; // the array memory41#ifdef ASSERT42int _nesting; // the resource area nesting level43#endif4445// creation46ResourceArray() {47_length = 0;48_data = NULL;49DEBUG_ONLY(init_nesting();)50// client may call initialize, at most once51}525354ResourceArray(size_t esize, int length) {55DEBUG_ONLY(_data = NULL);56initialize(esize, length);57}5859void initialize(size_t esize, int length) {60assert(length >= 0, "illegal length");61assert(StressRewriter || _data == NULL, "must be new object");62_length = length;63_data = resource_allocate_bytes(esize * length);64DEBUG_ONLY(init_nesting();)65}6667#ifdef ASSERT68void init_nesting();69#endif7071// helper functions72void sort (size_t esize, ftype f); // sort the array73void expand (size_t esize, int i, int& size);// expand the array to include slot i74void remove_at(size_t esize, int i); // remove the element in slot i7576public:77// standard operations78int length() const { return _length; }79bool is_empty() const { return length() == 0; }80};818283template <MEMFLAGS F>class CHeapArray: public CHeapObj<F> {84protected:85int _length; // the number of array elements86void* _data; // the array memory8788// creation89CHeapArray() {90_length = 0;91_data = NULL;92}939495CHeapArray(size_t esize, int length) {96assert(length >= 0, "illegal length");97_length = length;98_data = (void*) NEW_C_HEAP_ARRAY(char *, esize * length, F);99}100101void initialize(size_t esize, int length) {102// In debug set array to 0?103}104105#ifdef ASSERT106void init_nesting();107#endif108109// helper functions110void sort (size_t esize, ftype f); // sort the array111void expand (size_t esize, int i, int& size);// expand the array to include slot i112void remove_at(size_t esize, int i); // remove the element in slot i113114public:115// standard operations116int length() const { return _length; }117bool is_empty() const { return length() == 0; }118};119120#define define_generic_array(array_name,element_type, base_class) \121class array_name: public base_class { \122protected: \123typedef element_type etype; \124enum { esize = sizeof(etype) }; \125\126void base_remove_at(size_t size, int i) { base_class::remove_at(size, i); } \127\128public: \129/* creation */ \130array_name() : base_class() {} \131explicit array_name(const int length) : base_class(esize, length) {} \132array_name(const int length, const etype fx) { initialize(length, fx); } \133void initialize(const int length) { base_class::initialize(esize, length); } \134void initialize(const int length, const etype fx) { \135initialize(length); \136for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx; \137} \138\139/* standard operations */ \140etype& operator [] (const int i) const { \141assert(0 <= i && i < length(), "index out of bounds"); \142return ((etype*)_data)[i]; \143} \144\145int index_of(const etype x) const { \146int i = length(); \147while (i-- > 0 && ((etype*)_data)[i] != x) ; \148/* i < 0 || ((etype*)_data)_data[i] == x */ \149return i; \150} \151\152void sort(int f(etype*, etype*)) { base_class::sort(esize, (ftype)f); } \153bool contains(const etype x) const { return index_of(x) >= 0; } \154\155/* deprecated operations - for compatibility with GrowableArray only */ \156etype at(const int i) const { return (*this)[i]; } \157void at_put(const int i, const etype x) { (*this)[i] = x; } \158etype* adr_at(const int i) { return &(*this)[i]; } \159int find(const etype x) { return index_of(x); } \160}; \161162163#define define_array(array_name,element_type) \164define_generic_array(array_name, element_type, ResourceArray)165166167#define define_stack(stack_name,array_name) \168class stack_name: public array_name { \169protected: \170int _size; \171\172void grow(const int i, const etype fx) { \173assert(i >= length(), "index too small"); \174if (i >= size()) expand(esize, i, _size); \175for (int j = length(); j <= i; j++) ((etype*)_data)[j] = fx; \176_length = i+1; \177} \178\179public: \180/* creation */ \181stack_name() : array_name() { _size = 0; } \182stack_name(const int size) { initialize(size); } \183stack_name(const int size, const etype fx) { initialize(size, fx); } \184void initialize(const int size, const etype fx) { \185_size = size; \186array_name::initialize(size, fx); \187/* _length == size, allocation and size are the same */ \188} \189void initialize(const int size) { \190_size = size; \191array_name::initialize(size); \192_length = 0; /* reset length to zero; _size records the allocation */ \193} \194\195/* standard operations */ \196int size() const { return _size; } \197\198int push(const etype x) { \199int len = length(); \200if (len >= size()) expand(esize, len, _size); \201((etype*)_data)[len] = x; \202_length = len+1; \203return len; \204} \205\206etype pop() { \207assert(!is_empty(), "stack is empty"); \208return ((etype*)_data)[--_length]; \209} \210\211etype top() const { \212assert(!is_empty(), "stack is empty"); \213return ((etype*)_data)[length() - 1]; \214} \215\216void push_all(const stack_name* stack) { \217const int l = stack->length(); \218for (int i = 0; i < l; i++) push(((etype*)(stack->_data))[i]); \219} \220\221etype at_grow(const int i, const etype fx) { \222if (i >= length()) grow(i, fx); \223return ((etype*)_data)[i]; \224} \225\226void at_put_grow(const int i, const etype x, const etype fx) { \227if (i >= length()) grow(i, fx); \228((etype*)_data)[i] = x; \229} \230\231void truncate(const int length) { \232assert(0 <= length && length <= this->length(), "illegal length"); \233_length = length; \234} \235\236void remove_at(int i) { base_remove_at(esize, i); } \237void remove(etype x) { remove_at(index_of(x)); } \238\239/* inserts the given element before the element at index i */ \240void insert_before(const int i, const etype el) { \241int len = length(); \242int new_length = len + 1; \243if (new_length >= size()) expand(esize, new_length, _size); \244for (int j = len - 1; j >= i; j--) { \245((etype*)_data)[j + 1] = ((etype*)_data)[j]; \246} \247_length = new_length; \248at_put(i, el); \249} \250\251/* inserts contents of the given stack before the element at index i */ \252void insert_before(const int i, const stack_name *st) { \253if (st->length() == 0) return; \254int len = length(); \255int st_len = st->length(); \256int new_length = len + st_len; \257if (new_length >= size()) expand(esize, new_length, _size); \258int j; \259for (j = len - 1; j >= i; j--) { \260((etype*)_data)[j + st_len] = ((etype*)_data)[j]; \261} \262for (j = 0; j < st_len; j++) { \263((etype*)_data)[i + j] = ((etype*)st->_data)[j]; \264} \265_length = new_length; \266} \267\268/* deprecated operations - for compatibility with GrowableArray only */ \269int capacity() const { return size(); } \270void clear() { truncate(0); } \271void trunc_to(const int length) { truncate(length); } \272int append(const etype x) { return push(x); } \273void appendAll(const stack_name* stack) { push_all(stack); } \274etype last() const { return top(); } \275}; \276277278#define define_resource_list(element_type) \279define_generic_array(element_type##Array, element_type, ResourceArray) \280define_stack(element_type##List, element_type##Array)281282#define define_resource_pointer_list(element_type) \283define_generic_array(element_type##Array, element_type *, ResourceArray) \284define_stack(element_type##List, element_type##Array)285286#define define_c_heap_list(element_type) \287define_generic_array(element_type##Array, element_type, CHeapArray) \288define_stack(element_type##List, element_type##Array)289290#define define_c_heap_pointer_list(element_type) \291define_generic_array(element_type##Array, element_type *, CHeapArray) \292define_stack(element_type##List, element_type##Array)293294295// Arrays for basic types296297define_array(boolArray, bool) define_stack(boolStack, boolArray)298define_array(intArray , int ) define_stack(intStack , intArray )299300// Array for metadata allocation301302template <typename T>303class Array: public MetaspaceObj {304friend class MetadataFactory;305friend class VMStructs;306friend class MethodHandleCompiler; // special case307friend class WhiteBox;308protected:309int _length; // the number of array elements310T _data[1]; // the array memory311312void initialize(int length) {313_length = length;314}315316private:317// Turn off copy constructor and assignment operator.318Array(const Array<T>&);319void operator=(const Array<T>&);320321void* operator new(size_t size, ClassLoaderData* loader_data, int length, bool read_only, TRAPS) throw() {322size_t word_size = Array::size(length);323return (void*) Metaspace::allocate(loader_data, word_size, read_only,324MetaspaceObj::array_type(sizeof(T)), THREAD);325}326327static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); }328329// WhiteBox API helper.330// Can't distinguish between array of length 0 and length 1,331// will always return 0 in those cases.332static int bytes_to_length(size_t bytes) {333assert(is_size_aligned(bytes, BytesPerWord), "Must be, for now");334335if (sizeof(Array<T>) >= bytes) {336return 0;337}338339size_t left = bytes - sizeof(Array<T>);340assert(is_size_aligned(left, sizeof(T)), "Must be");341342size_t elements = left / sizeof(T);343assert(elements <= (size_t)INT_MAX, err_msg("number of elements " SIZE_FORMAT "doesn't fit into an int.", elements));344345int length = (int)elements;346347assert((size_t)size(length) * BytesPerWord == bytes,348err_msg("Expected: " SIZE_FORMAT " got: " SIZE_FORMAT,349bytes, (size_t)size(length) * BytesPerWord));350351return length;352}353354explicit Array(int length) : _length(length) {355assert(length >= 0, "illegal length");356}357358Array(int length, T init) : _length(length) {359assert(length >= 0, "illegal length");360for (int i = 0; i < length; i++) {361_data[i] = init;362}363}364365public:366367// standard operations368int length() const { return _length; }369T* data() { return _data; }370bool is_empty() const { return length() == 0; }371372int index_of(const T& x) const {373int i = length();374while (i-- > 0 && _data[i] != x) ;375376return i;377}378379// sort the array.380bool contains(const T& x) const { return index_of(x) >= 0; }381382T at(int i) const { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return _data[i]; }383void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); _data[i] = x; }384T* adr_at(const int i) { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return &_data[i]; }385int find(const T& x) { return index_of(x); }386387T at_acquire(const int which) { return OrderAccess::load_acquire(adr_at(which)); }388void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); }389390static int size(int length) {391return align_size_up(byte_sizeof(length), BytesPerWord) / BytesPerWord;392}393394int size() {395return size(_length);396}397398static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); }399// Note, this offset don't have to be wordSize aligned.400static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); };401402// FIXME: How to handle this?403void print_value_on(outputStream* st) const {404st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this));405}406407#ifndef PRODUCT408void print(outputStream* st) {409for (int i = 0; i< _length; i++) {410st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i));411}412}413void print() { print(tty); }414#endif // PRODUCT415};416417418#endif // SHARE_VM_UTILITIES_ARRAY_HPP419420421