src/share/vm/oops/arrayOop.hpp

changeset 3266
6fd81579526f
parent 2314
f95d63e2154a
child 3271
aa4c21b00f7f
     1.1 --- a/src/share/vm/oops/arrayOop.hpp	Fri Oct 28 13:04:10 2011 -0400
     1.2 +++ b/src/share/vm/oops/arrayOop.hpp	Mon Oct 31 08:01:20 2011 +0100
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -104,20 +104,26 @@
    1.11  
    1.12    // Return the maximum length of an array of BasicType.  The length can passed
    1.13    // to typeArrayOop::object_size(scale, length, header_size) without causing an
    1.14 -  // overflow.
    1.15 +  // overflow. We also need to make sure that this will not overflow a size_t on
    1.16 +  // 32 bit platforms when we convert it to a byte size.
    1.17    static int32_t max_array_length(BasicType type) {
    1.18      assert(type >= 0 && type < T_CONFLICT, "wrong type");
    1.19      assert(type2aelembytes(type) != 0, "wrong type");
    1.20 -    const int bytes_per_element = type2aelembytes(type);
    1.21 -    if (bytes_per_element < HeapWordSize) {
    1.22 +
    1.23 +    const size_t max_element_words_per_size_t  = align_size_down((SIZE_MAX/HeapWordSize - header_size(type)), MinObjAlignment);
    1.24 +    const size_t max_elements_per_size_t = HeapWordSize * max_element_words_per_size_t  / type2aelembytes(type);
    1.25 +    if ((size_t)max_jint < max_elements_per_size_t) {
    1.26        return max_jint;
    1.27      }
    1.28 +    return (int32_t)max_elements_per_size_t;
    1.29 +  }
    1.30  
    1.31 -    const int32_t max_words = align_size_down(max_jint, MinObjAlignment);
    1.32 -    const int32_t max_element_words = max_words - header_size(type);
    1.33 -    const int32_t words_per_element = bytes_per_element >> LogHeapWordSize;
    1.34 -    return max_element_words / words_per_element;
    1.35 -  }
    1.36 +// for unit testing
    1.37 +#ifndef PRODUCT
    1.38 +  static bool check_max_length_overflow(BasicType type);
    1.39 +  static int32_t old_max_array_length(BasicType type);
    1.40 +  static bool test_max_array_length();
    1.41 +#endif
    1.42  };
    1.43  
    1.44  #endif // SHARE_VM_OOPS_ARRAYOOP_HPP

mercurial