1 | /* |
2 | * Copyright (c) 2000, 2019, 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_OOPS_ARRAY_HPP |
26 | #define SHARE_OOPS_ARRAY_HPP |
27 | |
28 | #include "memory/allocation.hpp" |
29 | #include "memory/metaspace.hpp" |
30 | #include "runtime/orderAccess.hpp" |
31 | #include "utilities/align.hpp" |
32 | |
33 | // Array for metadata allocation |
34 | |
35 | template <typename T> |
36 | class Array: public MetaspaceObj { |
37 | friend class MetadataFactory; |
38 | friend class MetaspaceShared; |
39 | friend class VMStructs; |
40 | friend class JVMCIVMStructs; |
41 | friend class MethodHandleCompiler; // special case |
42 | friend class WhiteBox; |
43 | protected: |
44 | int _length; // the number of array elements |
45 | T _data[1]; // the array memory |
46 | |
47 | void initialize(int length) { |
48 | _length = length; |
49 | } |
50 | |
51 | private: |
52 | // Turn off copy constructor and assignment operator. |
53 | Array(const Array<T>&); |
54 | void operator=(const Array<T>&); |
55 | |
56 | void* operator new(size_t size, ClassLoaderData* loader_data, int length, TRAPS) throw() { |
57 | size_t word_size = Array::size(length); |
58 | return (void*) Metaspace::allocate(loader_data, word_size, |
59 | MetaspaceObj::array_type(sizeof(T)), THREAD); |
60 | } |
61 | |
62 | static size_t byte_sizeof(int length, size_t elm_byte_size) { |
63 | return sizeof(Array<T>) + MAX2(length - 1, 0) * elm_byte_size; |
64 | } |
65 | static size_t byte_sizeof(int length) { return byte_sizeof(length, sizeof(T)); } |
66 | |
67 | // WhiteBox API helper. |
68 | // Can't distinguish between array of length 0 and length 1, |
69 | // will always return 0 in those cases. |
70 | static int bytes_to_length(size_t bytes) { |
71 | assert(is_aligned(bytes, BytesPerWord), "Must be, for now" ); |
72 | |
73 | if (sizeof(Array<T>) >= bytes) { |
74 | return 0; |
75 | } |
76 | |
77 | size_t left = bytes - sizeof(Array<T>); |
78 | assert(is_aligned(left, sizeof(T)), "Must be" ); |
79 | |
80 | size_t elements = left / sizeof(T); |
81 | assert(elements <= (size_t)INT_MAX, "number of elements " SIZE_FORMAT "doesn't fit into an int." , elements); |
82 | |
83 | int length = (int)elements; |
84 | |
85 | assert((size_t)size(length) * BytesPerWord == (size_t)bytes, |
86 | "Expected: " SIZE_FORMAT " got: " SIZE_FORMAT, |
87 | bytes, (size_t)size(length) * BytesPerWord); |
88 | |
89 | return length; |
90 | } |
91 | |
92 | explicit Array(int length) : _length(length) { |
93 | assert(length >= 0, "illegal length" ); |
94 | } |
95 | |
96 | Array(int length, T init) : _length(length) { |
97 | assert(length >= 0, "illegal length" ); |
98 | for (int i = 0; i < length; i++) { |
99 | _data[i] = init; |
100 | } |
101 | } |
102 | |
103 | public: |
104 | |
105 | // standard operations |
106 | int length() const { return _length; } |
107 | T* data() { return _data; } |
108 | bool is_empty() const { return length() == 0; } |
109 | |
110 | int index_of(const T& x) const { |
111 | int i = length(); |
112 | while (i-- > 0 && _data[i] != x) ; |
113 | |
114 | return i; |
115 | } |
116 | |
117 | // sort the array. |
118 | bool contains(const T& x) const { return index_of(x) >= 0; } |
119 | |
120 | T at(int i) const { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d" , i, _length); return _data[i]; } |
121 | void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d" , i, _length); _data[i] = x; } |
122 | T* adr_at(const int i) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d" , i, _length); return &_data[i]; } |
123 | int find(const T& x) { return index_of(x); } |
124 | |
125 | T at_acquire(const int i) { return OrderAccess::load_acquire(adr_at(i)); } |
126 | void release_at_put(int i, T x) { OrderAccess::release_store(adr_at(i), x); } |
127 | |
128 | static int size(int length) { |
129 | size_t bytes = align_up(byte_sizeof(length), BytesPerWord); |
130 | size_t words = bytes / BytesPerWord; |
131 | |
132 | assert(words <= INT_MAX, "Overflow: " SIZE_FORMAT, words); |
133 | |
134 | return (int)words; |
135 | } |
136 | int size() { |
137 | return size(_length); |
138 | } |
139 | |
140 | static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); } |
141 | // Note, this offset don't have to be wordSize aligned. |
142 | static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); }; |
143 | |
144 | // FIXME: How to handle this? |
145 | void print_value_on(outputStream* st) const { |
146 | st->print("Array<T>(" INTPTR_FORMAT ")" , p2i(this)); |
147 | } |
148 | |
149 | #ifndef PRODUCT |
150 | void print(outputStream* st) { |
151 | for (int i = 0; i< _length; i++) { |
152 | st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i)); |
153 | } |
154 | } |
155 | void print() { print(tty); } |
156 | #endif // PRODUCT |
157 | }; |
158 | |
159 | |
160 | #endif // SHARE_OOPS_ARRAY_HPP |
161 | |