< prev index next >

src/share/vm/oops/array.hpp

Print this page


  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/allocation.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/metaspace.hpp"
  31 #include "runtime/orderAccess.hpp"
  32 
  33 // Array for metadata allocation
  34 
  35 template <typename T>
  36 class Array: public MetaspaceObj {
  37   friend class MetadataFactory;

  38   friend class VMStructs;
  39   friend class JVMCIVMStructs;
  40   friend class MethodHandleCompiler;           // special case
  41   friend class WhiteBox;
  42 protected:
  43   int _length;                                 // the number of array elements
  44   T   _data[1];                                // the array memory
  45 
  46   void initialize(int length) {
  47     _length = length;
  48   }
  49 
  50  private:
  51   // Turn off copy constructor and assignment operator.
  52   Array(const Array<T>&);
  53   void operator=(const Array<T>&);
  54 
  55   void* operator new(size_t size, ClassLoaderData* loader_data, int length, bool read_only, TRAPS) throw() {
  56     size_t word_size = Array::size(length);
  57     return (void*) Metaspace::allocate(loader_data, word_size, read_only,
  58                                        MetaspaceObj::array_type(sizeof(T)), THREAD);
  59   }
  60 
  61   static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); }



  62 
  63   // WhiteBox API helper.
  64   // Can't distinguish between array of length 0 and length 1,
  65   // will always return 0 in those cases.
  66   static int bytes_to_length(size_t bytes)       {
  67     assert(is_size_aligned(bytes, BytesPerWord), "Must be, for now");
  68 
  69     if (sizeof(Array<T>) >= bytes) {
  70       return 0;
  71     }
  72 
  73     size_t left = bytes - sizeof(Array<T>);
  74     assert(is_size_aligned(left, sizeof(T)), "Must be");
  75 
  76     size_t elements = left / sizeof(T);
  77     assert(elements <= (size_t)INT_MAX, "number of elements " SIZE_FORMAT "doesn't fit into an int.", elements);
  78 
  79     int length = (int)elements;
  80 
  81     assert((size_t)size(length) * BytesPerWord == bytes,


 106   int index_of(const T& x) const {
 107     int i = length();
 108     while (i-- > 0 && _data[i] != x) ;
 109 
 110     return i;
 111   }
 112 
 113   // sort the array.
 114   bool contains(const T& x) const      { return index_of(x) >= 0; }
 115 
 116   T    at(int i) const                 { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return _data[i]; }
 117   void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); _data[i] = x; }
 118   T*   adr_at(const int i)             { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return &_data[i]; }
 119   int  find(const T& x)                { return index_of(x); }
 120 
 121   T at_acquire(const int which)              { return OrderAccess::load_acquire(adr_at(which)); }
 122   void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); }
 123 
 124   static int size(int length) {
 125     return align_size_up(byte_sizeof(length), BytesPerWord) / BytesPerWord;



 126   }
 127 
 128   int size() {
 129     return size(_length);
 130   }
 131 
 132   static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); }
 133   // Note, this offset don't have to be wordSize aligned.
 134   static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); };
 135 
 136   // FIXME: How to handle this?
 137   void print_value_on(outputStream* st) const {
 138     st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this));
 139   }
 140 
 141 #ifndef PRODUCT
 142   void print(outputStream* st) {
 143      for (int i = 0; i< _length; i++) {
 144        st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i));
 145      }


  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/allocation.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/metaspace.hpp"
  31 #include "runtime/orderAccess.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_size_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_size_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 == bytes,


 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 which)              { return OrderAccess::load_acquire(adr_at(which)); }
 126   void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); }
 127 
 128   static int size(int length) {
 129     return align_size_up(byte_sizeof(length), BytesPerWord) / BytesPerWord;
 130   }
 131   static int size(int length, int elm_byte_size) {
 132     return align_size_up(byte_sizeof(length, elm_byte_size), BytesPerWord) / BytesPerWord;
 133   }
 134 
 135   int size() {
 136     return size(_length);
 137   }
 138 
 139   static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); }
 140   // Note, this offset don't have to be wordSize aligned.
 141   static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); };
 142 
 143   // FIXME: How to handle this?
 144   void print_value_on(outputStream* st) const {
 145     st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this));
 146   }
 147 
 148 #ifndef PRODUCT
 149   void print(outputStream* st) {
 150      for (int i = 0; i< _length; i++) {
 151        st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i));
 152      }
< prev index next >