1 /*
   2  * Copyright (c) 2000, 2016, Oracle and/or its affiliates. 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 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 template <MEMFLAGS F>class CHeapArray: public CHeapObj<F> {
  38  protected:
  39   int   _length;                                 // the number of array elements
  40   void* _data;                                   // the array memory
  41 
  42   // creation
  43   CHeapArray() {
  44     _length  = 0;
  45     _data    = NULL;
  46   }
  47 
  48 
  49   CHeapArray(size_t esize, int length) {
  50     assert(length >= 0, "illegal length");
  51     _length  = length;
  52     _data    = (void*) NEW_C_HEAP_ARRAY(char *, esize * length, F);
  53   }
  54 
  55   void initialize(size_t esize, int length) {
  56     // In debug set array to 0?
  57   }
  58 
  59 #ifdef ASSERT
  60   void init_nesting();
  61 #endif
  62 
  63   // helper functions
  64   void sort     (size_t esize, ftype f);         // sort the array
  65   void expand   (size_t esize, int i, int& size);// expand the array to include slot i
  66   void remove_at(size_t esize, int i);           // remove the element in slot i
  67 
  68  public:
  69   // standard operations
  70   int  length() const                            { return _length; }
  71   bool is_empty() const                          { return length() == 0; }
  72 };
  73 
  74 // Arrays for basic types
  75 typedef GrowableArray<int> intArray;
  76 typedef GrowableArray<int> intStack;
  77 typedef GrowableArray<bool> boolArray;
  78 typedef GrowableArray<bool> boolStack;
  79 
  80 // Array for metadata allocation
  81 
  82 template <typename T>
  83 class Array: public MetaspaceObj {
  84   friend class MetadataFactory;
  85   friend class VMStructs;
  86   friend class JVMCIVMStructs;
  87   friend class MethodHandleCompiler;           // special case
  88   friend class WhiteBox;
  89 protected:
  90   int _length;                                 // the number of array elements
  91   T   _data[1];                                // the array memory
  92 
  93   void initialize(int length) {
  94     _length = length;
  95   }
  96 
  97  private:
  98   // Turn off copy constructor and assignment operator.
  99   Array(const Array<T>&);
 100   void operator=(const Array<T>&);
 101 
 102   void* operator new(size_t size, ClassLoaderData* loader_data, int length, bool read_only, TRAPS) throw() {
 103     size_t word_size = Array::size(length);
 104     return (void*) Metaspace::allocate(loader_data, word_size, read_only,
 105                                        MetaspaceObj::array_type(sizeof(T)), THREAD);
 106   }
 107 
 108   static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); }
 109 
 110   // WhiteBox API helper.
 111   // Can't distinguish between array of length 0 and length 1,
 112   // will always return 0 in those cases.
 113   static int bytes_to_length(size_t bytes)       {
 114     assert(is_size_aligned(bytes, BytesPerWord), "Must be, for now");
 115 
 116     if (sizeof(Array<T>) >= bytes) {
 117       return 0;
 118     }
 119 
 120     size_t left = bytes - sizeof(Array<T>);
 121     assert(is_size_aligned(left, sizeof(T)), "Must be");
 122 
 123     size_t elements = left / sizeof(T);
 124     assert(elements <= (size_t)INT_MAX, "number of elements " SIZE_FORMAT "doesn't fit into an int.", elements);
 125 
 126     int length = (int)elements;
 127 
 128     assert((size_t)size(length) * BytesPerWord == bytes,
 129            "Expected: " SIZE_FORMAT " got: " SIZE_FORMAT,
 130            bytes, (size_t)size(length) * BytesPerWord);
 131 
 132     return length;
 133   }
 134 
 135   explicit Array(int length) : _length(length) {
 136     assert(length >= 0, "illegal length");
 137   }
 138 
 139   Array(int length, T init) : _length(length) {
 140     assert(length >= 0, "illegal length");
 141     for (int i = 0; i < length; i++) {
 142       _data[i] = init;
 143     }
 144   }
 145 
 146  public:
 147 
 148   // standard operations
 149   int  length() const                 { return _length; }
 150   T* data()                           { return _data; }
 151   bool is_empty() const               { return length() == 0; }
 152 
 153   int index_of(const T& x) const {
 154     int i = length();
 155     while (i-- > 0 && _data[i] != x) ;
 156 
 157     return i;
 158   }
 159 
 160   // sort the array.
 161   bool contains(const T& x) const      { return index_of(x) >= 0; }
 162 
 163   T    at(int i) const                 { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return _data[i]; }
 164   void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); _data[i] = x; }
 165   T*   adr_at(const int i)             { assert(i >= 0 && i< _length, "oob: 0 <= %d < %d", i, _length); return &_data[i]; }
 166   int  find(const T& x)                { return index_of(x); }
 167 
 168   T at_acquire(const int which)              { return OrderAccess::load_acquire(adr_at(which)); }
 169   void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); }
 170 
 171   static int size(int length) {
 172     return align_size_up(byte_sizeof(length), BytesPerWord) / BytesPerWord;
 173   }
 174 
 175   int size() {
 176     return size(_length);
 177   }
 178 
 179   static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); }
 180   // Note, this offset don't have to be wordSize aligned.
 181   static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); };
 182 
 183   // FIXME: How to handle this?
 184   void print_value_on(outputStream* st) const {
 185     st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this));
 186   }
 187 
 188 #ifndef PRODUCT
 189   void print(outputStream* st) {
 190      for (int i = 0; i< _length; i++) {
 191        st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i));
 192      }
 193   }
 194   void print() { print(tty); }
 195 #endif // PRODUCT
 196 };
 197 
 198 
 199 #endif // SHARE_VM_UTILITIES_ARRAY_HPP