1 /* 2 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_OOPS_ARRAY_HPP 26 #define SHARE_VM_OOPS_ARRAY_HPP 27 28 #include "memory/metaspace.hpp" 29 #include "utilities/align.hpp" 30 31 // Array for metadata allocation 32 33 template <typename T> 34 class Array: public MetaspaceObj { 35 friend class MetadataFactory; 36 friend class MetaspaceShared; 37 friend class VMStructs; 38 friend class JVMCIVMStructs; 39 friend class MethodHandleCompiler; // special case 40 friend class WhiteBox; 41 protected: 42 int _length; // the number of array elements 43 T _data[1]; // the array memory 44 45 void initialize(int length) { 46 _length = length; 47 } 48 49 private: 50 // Turn off copy constructor and assignment operator. 51 Array(const Array<T>&); 52 void operator=(const Array<T>&); 53 54 void* operator new(size_t size, ClassLoaderData* loader_data, int length, TRAPS) throw() { 55 size_t word_size = Array::size(length); 56 return (void*) Metaspace::allocate(loader_data, word_size, 57 MetaspaceObj::array_type(sizeof(T)), THREAD); 58 } 59 60 static size_t byte_sizeof(int length, size_t elm_byte_size) { 61 return sizeof(Array<T>) + MAX2(length - 1, 0) * elm_byte_size; 62 } 63 static size_t byte_sizeof(int length) { return byte_sizeof(length, sizeof(T)); } 64 65 // WhiteBox API helper. 66 // Can't distinguish between array of length 0 and length 1, 67 // will always return 0 in those cases. 68 static int bytes_to_length(size_t bytes) { 69 assert(is_aligned(bytes, BytesPerWord), "Must be, for now"); 70 71 if (sizeof(Array<T>) >= bytes) { 72 return 0; 73 } 74 75 size_t left = bytes - sizeof(Array<T>); 76 assert(is_aligned(left, sizeof(T)), "Must be"); 77 78 size_t elements = left / sizeof(T); 79 assert(elements <= (size_t)INT_MAX, "number of elements " SIZE_FORMAT "doesn't fit into an int.", elements); 80 81 int length = (int)elements; 82 83 assert((size_t)size(length) * BytesPerWord == (size_t)bytes, 84 "Expected: " SIZE_FORMAT " got: " SIZE_FORMAT, 85 bytes, (size_t)size(length) * BytesPerWord); 86 87 return length; 88 } 89 90 explicit Array(int length) : _length(length) { 91 assert(length >= 0, "illegal length"); 92 } 93 94 Array(int length, T init) : _length(length) { 95 assert(length >= 0, "illegal length"); 96 for (int i = 0; i < length; i++) { 97 _data[i] = init; 98 } 99 } 100 101 public: 102 103 // standard operations 104 int length() const { return _length; } 105 T* data() { return _data; } 106 bool is_empty() const { return length() == 0; } 107 108 int index_of(const T& x) const { 109 int i = length(); 110 while (i-- > 0 && _data[i] != x) ; 111 112 return i; 113 } 114 115 // sort the array. 116 bool contains(const T& x) const { return index_of(x) >= 0; } 117 118 T at(int i) const { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return _data[i]; } 119 void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); _data[i] = x; } 120 T* adr_at(const int i) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return &_data[i]; } 121 int find(const T& x) { return index_of(x); } 122 123 T at_acquire(const int which); 124 void release_at_put(int which, T contents); 125 126 static int size(int length) { 127 size_t bytes = align_up(byte_sizeof(length), BytesPerWord); 128 size_t words = bytes / BytesPerWord; 129 130 assert(words <= INT_MAX, "Overflow: " SIZE_FORMAT, words); 131 132 return (int)words; 133 } 134 int size() { 135 return size(_length); 136 } 137 138 static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); } 139 // Note, this offset don't have to be wordSize aligned. 140 static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); }; 141 142 // FIXME: How to handle this? 143 void print_value_on(outputStream* st) const { 144 st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this)); 145 } 146 147 #ifndef PRODUCT 148 void print(outputStream* st) { 149 for (int i = 0; i< _length; i++) { 150 st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i)); 151 } 152 } 153 void print() { print(tty); } 154 #endif // PRODUCT 155 }; 156 157 158 #endif // SHARE_VM_OOPS_ARRAY_HPP