Path: blob/master/src/hotspot/share/oops/array.hpp
40951 views
/*1* Copyright (c) 2000, 2021, 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_OOPS_ARRAY_HPP25#define SHARE_OOPS_ARRAY_HPP2627#include "runtime/atomic.hpp"28#include "utilities/align.hpp"29#include "utilities/exceptions.hpp"30#include "utilities/globalDefinitions.hpp"31#include "utilities/ostream.hpp"3233// Array for metadata allocation3435template <typename T>36class Array: public MetaspaceObj {37friend class ArchiveBuilder;38friend class MetadataFactory;39friend class VMStructs;40friend class JVMCIVMStructs;41friend class MethodHandleCompiler; // special case42friend class WhiteBox;43protected:44int _length; // the number of array elements45T _data[1]; // the array memory4647void initialize(int length) {48_length = length;49}5051private:52NONCOPYABLE(Array);5354inline void* operator new(size_t size, ClassLoaderData* loader_data, int length, TRAPS) throw();5556static size_t byte_sizeof(int length, size_t elm_byte_size) {57return sizeof(Array<T>) + MAX2(length - 1, 0) * elm_byte_size;58}59static size_t byte_sizeof(int length) { return byte_sizeof(length, sizeof(T)); }6061// WhiteBox API helper.62// Can't distinguish between array of length 0 and length 1,63// will always return 0 in those cases.64static int bytes_to_length(size_t bytes) {65assert(is_aligned(bytes, BytesPerWord), "Must be, for now");6667if (sizeof(Array<T>) >= bytes) {68return 0;69}7071size_t left = bytes - sizeof(Array<T>);72assert(is_aligned(left, sizeof(T)), "Must be");7374size_t elements = left / sizeof(T);75assert(elements <= (size_t)INT_MAX, "number of elements " SIZE_FORMAT "doesn't fit into an int.", elements);7677int length = (int)elements;7879assert((size_t)size(length) * BytesPerWord == (size_t)bytes,80"Expected: " SIZE_FORMAT " got: " SIZE_FORMAT,81bytes, (size_t)size(length) * BytesPerWord);8283return length;84}8586explicit Array(int length) : _length(length) {87assert(length >= 0, "illegal length");88}8990Array(int length, T init) : _length(length) {91assert(length >= 0, "illegal length");92for (int i = 0; i < length; i++) {93_data[i] = init;94}95}9697public:9899// standard operations100int length() const { return _length; }101T* data() { return _data; }102bool is_empty() const { return length() == 0; }103104int index_of(const T& x) const {105int i = length();106while (i-- > 0 && _data[i] != x) ;107108return i;109}110111// sort the array.112bool contains(const T& x) const { return index_of(x) >= 0; }113114T at(int i) const { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return _data[i]; }115void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); _data[i] = x; }116T* adr_at(const int i) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return &_data[i]; }117int find(const T& x) { return index_of(x); }118119T at_acquire(const int i) { return Atomic::load_acquire(adr_at(i)); }120void release_at_put(int i, T x) { Atomic::release_store(adr_at(i), x); }121122static int size(int length) {123size_t bytes = align_up(byte_sizeof(length), BytesPerWord);124size_t words = bytes / BytesPerWord;125126assert(words <= INT_MAX, "Overflow: " SIZE_FORMAT, words);127128return (int)words;129}130int size() {131return size(_length);132}133134static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); }135// Note, this offset don't have to be wordSize aligned.136static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); };137138// FIXME: How to handle this?139void print_value_on(outputStream* st) const {140st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this));141}142143#ifndef PRODUCT144void print(outputStream* st) {145for (int i = 0; i< _length; i++) {146st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i));147}148}149void print() { print(tty); }150#endif // PRODUCT151};152153154#endif // SHARE_OOPS_ARRAY_HPP155156157