src/share/vm/utilities/array.hpp

Print this page




   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_UTILITIES_ARRAY_HPP
  26 #define SHARE_VM_UTILITIES_ARRAY_HPP
  27 

  28 #include "memory/allocation.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/metaspace.hpp"
  31 #include "runtime/orderAccess.hpp"
  32 
  33 // correct linkage required to compile w/o warnings
  34 // (must be on file level - cannot be local)
  35 extern "C" { typedef int (*ftype)(const void*, const void*); }
  36 
  37 
  38 class ResourceArray: public ResourceObj {
  39  protected:
  40   int   _length;                                 // the number of array elements
  41   void* _data;                                   // the array memory
  42 #ifdef ASSERT
  43   int   _nesting;                                // the resource area nesting level
  44 #endif
  45 
  46   // creation
  47   ResourceArray() {


 288   define_generic_array(element_type##Array, element_type, CHeapArray)                    \
 289   define_stack(element_type##List, element_type##Array)
 290 
 291 #define define_c_heap_pointer_list(element_type)                                         \
 292   define_generic_array(element_type##Array, element_type *, CHeapArray)                  \
 293   define_stack(element_type##List, element_type##Array)
 294 
 295 
 296 // Arrays for basic types
 297 
 298 define_array(boolArray, bool)          define_stack(boolStack, boolArray)
 299 define_array(intArray , int )          define_stack(intStack , intArray )
 300 
 301 // Array for metadata allocation
 302 
 303 template <typename T>
 304 class Array: public MetaspaceObj {
 305   friend class MetadataFactory;
 306   friend class VMStructs;
 307   friend class MethodHandleCompiler;           // special case

 308 protected:
 309   int _length;                                 // the number of array elements
 310   T   _data[1];                                // the array memory
 311 
 312   void initialize(int length) {
 313     _length = length;
 314   }
 315 
 316  private:
 317   // Turn off copy constructor and assignment operator.
 318   Array(const Array<T>&);
 319   void operator=(const Array<T>&);
 320 
 321   void* operator new(size_t size, ClassLoaderData* loader_data, int length, bool read_only, TRAPS) throw() {
 322     size_t word_size = Array::size(length);
 323     return (void*) Metaspace::allocate(loader_data, word_size, read_only,
 324                                        MetaspaceObj::array_type(sizeof(T)), CHECK_NULL);
 325   }
 326 
 327   static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); }























 328 
 329   explicit Array(int length) : _length(length) {
 330     assert(length >= 0, "illegal length");
 331   }
 332 
 333   Array(int length, T init) : _length(length) {
 334     assert(length >= 0, "illegal length");
 335     for (int i = 0; i < length; i++) {
 336       _data[i] = init;
 337     }
 338   }
 339 
 340  public:
 341 
 342   // standard operations
 343   int  length() const                 { return _length; }
 344   T* data()                           { return _data; }
 345   bool is_empty() const               { return length() == 0; }
 346 
 347   int index_of(const T& x) const {




   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_UTILITIES_ARRAY_HPP
  26 #define SHARE_VM_UTILITIES_ARRAY_HPP
  27 
  28 #include "classfile/classLoaderData.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/metaspace.hpp"
  32 #include "runtime/orderAccess.hpp"
  33 
  34 // correct linkage required to compile w/o warnings
  35 // (must be on file level - cannot be local)
  36 extern "C" { typedef int (*ftype)(const void*, const void*); }
  37 
  38 
  39 class ResourceArray: public ResourceObj {
  40  protected:
  41   int   _length;                                 // the number of array elements
  42   void* _data;                                   // the array memory
  43 #ifdef ASSERT
  44   int   _nesting;                                // the resource area nesting level
  45 #endif
  46 
  47   // creation
  48   ResourceArray() {


 289   define_generic_array(element_type##Array, element_type, CHeapArray)                    \
 290   define_stack(element_type##List, element_type##Array)
 291 
 292 #define define_c_heap_pointer_list(element_type)                                         \
 293   define_generic_array(element_type##Array, element_type *, CHeapArray)                  \
 294   define_stack(element_type##List, element_type##Array)
 295 
 296 
 297 // Arrays for basic types
 298 
 299 define_array(boolArray, bool)          define_stack(boolStack, boolArray)
 300 define_array(intArray , int )          define_stack(intStack , intArray )
 301 
 302 // Array for metadata allocation
 303 
 304 template <typename T>
 305 class Array: public MetaspaceObj {
 306   friend class MetadataFactory;
 307   friend class VMStructs;
 308   friend class MethodHandleCompiler;           // special case
 309   friend class WhiteBox;
 310 protected:
 311   int _length;                                 // the number of array elements
 312   T   _data[1];                                // the array memory
 313 
 314   void initialize(int length) {
 315     _length = length;
 316   }
 317 
 318  private:
 319   // Turn off copy constructor and assignment operator.
 320   Array(const Array<T>&);
 321   void operator=(const Array<T>&);
 322 
 323   void* operator new(size_t size, ClassLoaderData* loader_data, int length, bool read_only, TRAPS) throw() {
 324     size_t word_size = Array::size(length);
 325     return (void*) Metaspace::allocate(loader_data, word_size, read_only,
 326                                        MetaspaceObj::array_type(sizeof(T)), CHECK_NULL);
 327   }
 328 
 329   static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); }
 330 
 331   // WhiteBox API helper.
 332   static int bytes_to_length(size_t bytes)       {
 333     assert(is_size_aligned(bytes, BytesPerWord), "Must be, for now");
 334 
 335     if (sizeof(Array<T>) >= bytes) {
 336       return 0;
 337     }
 338 
 339     size_t left = bytes - sizeof(Array<T>);
 340     assert(is_size_aligned(left, sizeof(T)), "Must be");
 341 
 342     size_t elements = left / sizeof(T);
 343     assert(elements <= (size_t)INT_MAX, err_msg("number of elements " SIZE_FORMAT "doesn't fit into an int.", elements));
 344 
 345     int length = (int)elements;
 346 
 347     assert((size_t)size(length) * BytesPerWord == bytes,
 348         err_msg("Expected: " SIZE_FORMAT " got: " SIZE_FORMAT,
 349                 bytes, (size_t)size(length) * BytesPerWord));
 350 
 351     return length;
 352   }
 353 
 354   explicit Array(int length) : _length(length) {
 355     assert(length >= 0, "illegal length");
 356   }
 357 
 358   Array(int length, T init) : _length(length) {
 359     assert(length >= 0, "illegal length");
 360     for (int i = 0; i < length; i++) {
 361       _data[i] = init;
 362     }
 363   }
 364 
 365  public:
 366 
 367   // standard operations
 368   int  length() const                 { return _length; }
 369   T* data()                           { return _data; }
 370   bool is_empty() const               { return length() == 0; }
 371 
 372   int index_of(const T& x) const {