typeArrayKlass.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 1997-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25# include "incls/_precompiled.incl"
26# include "incls/_typeArrayKlass.cpp.incl"
27
28bool typeArrayKlass::compute_is_subtype_of(klassOop k) {
29  if (!k->klass_part()->oop_is_typeArray()) {
30    return arrayKlass::compute_is_subtype_of(k);
31  }
32
33  typeArrayKlass* tak = typeArrayKlass::cast(k);
34  if (dimension() != tak->dimension()) return false;
35
36  return element_type() == tak->element_type();
37}
38
39klassOop typeArrayKlass::create_klass(BasicType type, int scale, TRAPS) {
40  typeArrayKlass o;
41
42  symbolHandle sym(symbolOop(NULL));
43  // bootstrapping: don't create sym if symbolKlass not created yet
44  if (Universe::symbolKlassObj() != NULL) {
45    sym = oopFactory::new_symbol_handle(external_name(type), CHECK_NULL);
46  }
47  KlassHandle klassklass (THREAD, Universe::typeArrayKlassKlassObj());
48
49  arrayKlassHandle k = base_create_array_klass(o.vtbl_value(), header_size(), klassklass, CHECK_NULL);
50  typeArrayKlass* ak = typeArrayKlass::cast(k());
51  ak->set_name(sym());
52  ak->set_layout_helper(array_layout_helper(type));
53  assert(scale == (1 << ak->log2_element_size()), "scale must check out");
54  assert(ak->oop_is_javaArray(), "sanity");
55  assert(ak->oop_is_typeArray(), "sanity");
56  ak->set_max_length(arrayOopDesc::max_array_length(type));
57  assert(k()->size() > header_size(), "bad size");
58
59  // Call complete_create_array_klass after all instance variables have been initialized.
60  KlassHandle super (THREAD, k->super());
61  complete_create_array_klass(k, super, CHECK_NULL);
62
63  return k();
64}
65
66typeArrayOop typeArrayKlass::allocate(int length, TRAPS) {
67  assert(log2_element_size() >= 0, "bad scale");
68  if (length >= 0) {
69    if (length <= max_length()) {
70      size_t size = typeArrayOopDesc::object_size(layout_helper(), length);
71      KlassHandle h_k(THREAD, as_klassOop());
72      typeArrayOop t;
73      CollectedHeap* ch = Universe::heap();
74      if (size < ch->large_typearray_limit()) {
75        t = (typeArrayOop)CollectedHeap::array_allocate(h_k, (int)size, length, CHECK_NULL);
76      } else {
77        t = (typeArrayOop)CollectedHeap::large_typearray_allocate(h_k, (int)size, length, CHECK_NULL);
78      }
79      assert(t->is_parsable(), "Don't publish unless parsable");
80      return t;
81    } else {
82      THROW_OOP_0(Universe::out_of_memory_error_array_size());
83    }
84  } else {
85    THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
86  }
87}
88
89typeArrayOop typeArrayKlass::allocate_permanent(int length, TRAPS) {
90  if (length < 0) THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
91  int size = typeArrayOopDesc::object_size(layout_helper(), length);
92  KlassHandle h_k(THREAD, as_klassOop());
93  typeArrayOop t = (typeArrayOop)
94    CollectedHeap::permanent_array_allocate(h_k, size, length, CHECK_NULL);
95  assert(t->is_parsable(), "Can't publish until parsable");
96  return t;
97}
98
99oop typeArrayKlass::multi_allocate(int rank, jint* last_size, TRAPS) {
100  // For typeArrays this is only called for the last dimension
101  assert(rank == 1, "just checking");
102  int length = *last_size;
103  return allocate(length, THREAD);
104}
105
106
107void typeArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS) {
108  assert(s->is_typeArray(), "must be type array");
109
110  // Check destination
111  if (!d->is_typeArray() || element_type() != typeArrayKlass::cast(d->klass())->element_type()) {
112    THROW(vmSymbols::java_lang_ArrayStoreException());
113  }
114
115  // Check is all offsets and lengths are non negative
116  if (src_pos < 0 || dst_pos < 0 || length < 0) {
117    THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
118  }
119  // Check if the ranges are valid
120  if  ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length())
121     || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) ) {
122    THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
123  }
124
125  // This is an attempt to make the copy_array fast.
126  // NB: memmove takes care of overlapping memory segments.
127  // Potential problem: memmove is not guaranteed to be word atomic
128  // Revisit in Merlin
129  int l2es = log2_element_size();
130  int ihs = array_header_in_bytes() / wordSize;
131  char* src = (char*) ((oop*)s + ihs) + (src_pos << l2es);
132  char* dst = (char*) ((oop*)d + ihs) + (dst_pos << l2es);
133  memmove(dst, src, length << l2es);
134}
135
136
137// create a klass of array holding typeArrays
138klassOop typeArrayKlass::array_klass_impl(bool or_null, int n, TRAPS) {
139  typeArrayKlassHandle h_this(THREAD, as_klassOop());
140  return array_klass_impl(h_this, or_null, n, THREAD);
141}
142
143klassOop typeArrayKlass::array_klass_impl(typeArrayKlassHandle h_this, bool or_null, int n, TRAPS) {
144  int dimension = h_this->dimension();
145  assert(dimension <= n, "check order of chain");
146    if (dimension == n)
147      return h_this();
148
149  objArrayKlassHandle  h_ak(THREAD, h_this->higher_dimension());
150  if (h_ak.is_null()) {
151    if (or_null)  return NULL;
152
153    ResourceMark rm;
154    JavaThread *jt = (JavaThread *)THREAD;
155    {
156      MutexLocker mc(Compile_lock, THREAD);   // for vtables
157      // Atomic create higher dimension and link into list
158      MutexLocker mu(MultiArray_lock, THREAD);
159
160      h_ak = objArrayKlassHandle(THREAD, h_this->higher_dimension());
161      if (h_ak.is_null()) {
162        klassOop oak = objArrayKlassKlass::cast(
163          Universe::objArrayKlassKlassObj())->allocate_objArray_klass(
164          dimension + 1, h_this, CHECK_NULL);
165        h_ak = objArrayKlassHandle(THREAD, oak);
166        h_ak->set_lower_dimension(h_this());
167        h_this->set_higher_dimension(h_ak());
168        assert(h_ak->oop_is_objArray(), "incorrect initialization of objArrayKlass");
169      }
170    }
171  } else {
172    CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
173  }
174  if (or_null) {
175    return h_ak->array_klass_or_null(n);
176  }
177  return h_ak->array_klass(n, CHECK_NULL);
178}
179
180klassOop typeArrayKlass::array_klass_impl(bool or_null, TRAPS) {
181  return array_klass_impl(or_null, dimension() +  1, THREAD);
182}
183
184int typeArrayKlass::oop_size(oop obj) const {
185  assert(obj->is_typeArray(),"must be a type array");
186  typeArrayOop t = typeArrayOop(obj);
187  return t->object_size();
188}
189
190void typeArrayKlass::oop_follow_contents(oop obj) {
191  assert(obj->is_typeArray(),"must be a type array");
192  // Performance tweak: We skip iterating over the klass pointer since we
193  // know that Universe::typeArrayKlass never moves.
194}
195
196#ifndef SERIALGC
197void typeArrayKlass::oop_follow_contents(ParCompactionManager* cm, oop obj) {
198  assert(obj->is_typeArray(),"must be a type array");
199  // Performance tweak: We skip iterating over the klass pointer since we
200  // know that Universe::typeArrayKlass never moves.
201}
202#endif // SERIALGC
203
204int typeArrayKlass::oop_adjust_pointers(oop obj) {
205  assert(obj->is_typeArray(),"must be a type array");
206  typeArrayOop t = typeArrayOop(obj);
207  // Performance tweak: We skip iterating over the klass pointer since we
208  // know that Universe::typeArrayKlass never moves.
209  return t->object_size();
210}
211
212int typeArrayKlass::oop_oop_iterate(oop obj, OopClosure* blk) {
213  assert(obj->is_typeArray(),"must be a type array");
214  typeArrayOop t = typeArrayOop(obj);
215  // Performance tweak: We skip iterating over the klass pointer since we
216  // know that Universe::typeArrayKlass never moves.
217  return t->object_size();
218}
219
220int typeArrayKlass::oop_oop_iterate_m(oop obj, OopClosure* blk, MemRegion mr) {
221  assert(obj->is_typeArray(),"must be a type array");
222  typeArrayOop t = typeArrayOop(obj);
223  // Performance tweak: We skip iterating over the klass pointer since we
224  // know that Universe::typeArrayKlass never moves.
225  return t->object_size();
226}
227
228#ifndef SERIALGC
229void typeArrayKlass::oop_copy_contents(PSPromotionManager* pm, oop obj) {
230  assert(obj->is_typeArray(),"must be a type array");
231}
232
233void typeArrayKlass::oop_push_contents(PSPromotionManager* pm, oop obj) {
234  assert(obj->is_typeArray(),"must be a type array");
235}
236
237int
238typeArrayKlass::oop_update_pointers(ParCompactionManager* cm, oop obj) {
239  assert(obj->is_typeArray(),"must be a type array");
240  return typeArrayOop(obj)->object_size();
241}
242
243int
244typeArrayKlass::oop_update_pointers(ParCompactionManager* cm, oop obj,
245                                    HeapWord* beg_addr, HeapWord* end_addr) {
246  assert(obj->is_typeArray(),"must be a type array");
247  return typeArrayOop(obj)->object_size();
248}
249#endif // SERIALGC
250
251void typeArrayKlass::initialize(TRAPS) {
252  // Nothing to do. Having this function is handy since objArrayKlasses can be
253  // initialized by calling initialize on their bottom_klass, see objArrayKlass::initialize
254}
255
256const char* typeArrayKlass::external_name(BasicType type) {
257  switch (type) {
258    case T_BOOLEAN: return "[Z";
259    case T_CHAR:    return "[C";
260    case T_FLOAT:   return "[F";
261    case T_DOUBLE:  return "[D";
262    case T_BYTE:    return "[B";
263    case T_SHORT:   return "[S";
264    case T_INT:     return "[I";
265    case T_LONG:    return "[J";
266    default: ShouldNotReachHere();
267  }
268  return NULL;
269}
270
271#ifndef PRODUCT
272// Printing
273
274static void print_boolean_array(typeArrayOop ta, int print_len, outputStream* st) {
275  for (int index = 0; index < print_len; index++) {
276    st->print_cr(" - %3d: %s", index, (ta->bool_at(index) == 0) ? "false" : "true");
277  }
278}
279
280
281static void print_char_array(typeArrayOop ta, int print_len, outputStream* st) {
282  for (int index = 0; index < print_len; index++) {
283    jchar c = ta->char_at(index);
284    st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
285  }
286}
287
288
289static void print_float_array(typeArrayOop ta, int print_len, outputStream* st) {
290  for (int index = 0; index < print_len; index++) {
291    st->print_cr(" - %3d: %g", index, ta->float_at(index));
292  }
293}
294
295
296static void print_double_array(typeArrayOop ta, int print_len, outputStream* st) {
297  for (int index = 0; index < print_len; index++) {
298    st->print_cr(" - %3d: %g", index, ta->double_at(index));
299  }
300}
301
302
303static void print_byte_array(typeArrayOop ta, int print_len, outputStream* st) {
304  for (int index = 0; index < print_len; index++) {
305    jbyte c = ta->byte_at(index);
306    st->print_cr(" - %3d: %x %c", index, c, isprint(c) ? c : ' ');
307  }
308}
309
310
311static void print_short_array(typeArrayOop ta, int print_len, outputStream* st) {
312  for (int index = 0; index < print_len; index++) {
313    int v = ta->ushort_at(index);
314    st->print_cr(" - %3d: 0x%x\t %d", index, v, v);
315  }
316}
317
318
319static void print_int_array(typeArrayOop ta, int print_len, outputStream* st) {
320  for (int index = 0; index < print_len; index++) {
321    jint v = ta->int_at(index);
322    st->print_cr(" - %3d: 0x%x %d", index, v, v);
323  }
324}
325
326
327static void print_long_array(typeArrayOop ta, int print_len, outputStream* st) {
328  for (int index = 0; index < print_len; index++) {
329    jlong v = ta->long_at(index);
330    st->print_cr(" - %3d: 0x%x 0x%x", index, high(v), low(v));
331  }
332}
333
334
335void typeArrayKlass::oop_print_on(oop obj, outputStream* st) {
336  arrayKlass::oop_print_on(obj, st);
337  typeArrayOop ta = typeArrayOop(obj);
338  int print_len = MIN2((intx) ta->length(), MaxElementPrintSize);
339  switch (element_type()) {
340    case T_BOOLEAN: print_boolean_array(ta, print_len, st); break;
341    case T_CHAR:    print_char_array(ta, print_len, st);    break;
342    case T_FLOAT:   print_float_array(ta, print_len, st);   break;
343    case T_DOUBLE:  print_double_array(ta, print_len, st);  break;
344    case T_BYTE:    print_byte_array(ta, print_len, st);    break;
345    case T_SHORT:   print_short_array(ta, print_len, st);   break;
346    case T_INT:     print_int_array(ta, print_len, st);     break;
347    case T_LONG:    print_long_array(ta, print_len, st);    break;
348    default: ShouldNotReachHere();
349  }
350  int remaining = ta->length() - print_len;
351  if (remaining > 0) {
352    tty->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining);
353  }
354}
355
356#endif // PRODUCT
357
358const char* typeArrayKlass::internal_name() const {
359  return Klass::external_name();
360}
361