< prev index next >

src/hotspot/share/oops/array.hpp

Print this page




  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 


 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));


  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/atomic.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 


 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 Atomic::load_acquire(adr_at(i)); }
 126   void release_at_put(int i, T x)      { Atomic::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));
< prev index next >