You need to sign in or sign up before continuing.
array.hpp 21.3 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29
#ifndef SHARE_VM_UTILITIES_ARRAY_HPP
#define SHARE_VM_UTILITIES_ARRAY_HPP

#include "memory/allocation.hpp"
#include "memory/allocation.inline.hpp"
30
#include "memory/metaspace.hpp"
31

D
duke 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// correct linkage required to compile w/o warnings
// (must be on file level - cannot be local)
extern "C" { typedef int (*ftype)(const void*, const void*); }


class ResourceArray: public ResourceObj {
 protected:
  int   _length;                                 // the number of array elements
  void* _data;                                   // the array memory
#ifdef ASSERT
  int   _nesting;                                // the resource area nesting level
#endif

  // creation
  ResourceArray() {
    _length  = 0;
    _data    = NULL;
    DEBUG_ONLY(init_nesting();)
J
jrose 已提交
50
    // client may call initialize, at most once
D
duke 已提交
51 52 53 54
  }


  ResourceArray(size_t esize, int length) {
J
jrose 已提交
55 56 57 58 59
    DEBUG_ONLY(_data = NULL);
    initialize(esize, length);
  }

  void initialize(size_t esize, int length) {
D
duke 已提交
60
    assert(length >= 0, "illegal length");
J
jrose 已提交
61
    assert(_data == NULL, "must be new object");
D
duke 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    _length  = length;
    _data    = resource_allocate_bytes(esize * length);
    DEBUG_ONLY(init_nesting();)
  }

#ifdef ASSERT
  void init_nesting();
#endif

  // helper functions
  void sort     (size_t esize, ftype f);         // sort the array
  void expand   (size_t esize, int i, int& size);// expand the array to include slot i
  void remove_at(size_t esize, int i);           // remove the element in slot i

 public:
  // standard operations
  int  length() const                            { return _length; }
  bool is_empty() const                          { return length() == 0; }
};


Z
zgu 已提交
83
template <MEMFLAGS F>class CHeapArray: public CHeapObj<F> {
D
duke 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97
 protected:
  int   _length;                                 // the number of array elements
  void* _data;                                   // the array memory

  // creation
  CHeapArray() {
    _length  = 0;
    _data    = NULL;
  }


  CHeapArray(size_t esize, int length) {
    assert(length >= 0, "illegal length");
    _length  = length;
Z
zgu 已提交
98
    _data    = (void*) NEW_C_HEAP_ARRAY(char *, esize * length, F);
D
duke 已提交
99 100
  }

101 102 103 104
  void initialize(size_t esize, int length) {
    // In debug set array to 0?
  }

D
duke 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#ifdef ASSERT
  void init_nesting();
#endif

  // helper functions
  void sort     (size_t esize, ftype f);         // sort the array
  void expand   (size_t esize, int i, int& size);// expand the array to include slot i
  void remove_at(size_t esize, int i);           // remove the element in slot i

 public:
  // standard operations
  int  length() const                            { return _length; }
  bool is_empty() const                          { return length() == 0; }
};

#define define_generic_array(array_name,element_type, base_class)                        \
  class array_name: public base_class {                                                  \
   protected:                                                                            \
    typedef element_type etype;                                                          \
    enum { esize = sizeof(etype) };                                                      \
                                                                                         \
    void base_remove_at(size_t size, int i) { base_class::remove_at(size, i); }          \
                                                                                         \
   public:                                                                               \
    /* creation */                                                                       \
    array_name() : base_class()                       {}                                 \
131
    explicit array_name(const int length) : base_class(esize, length) {}                          \
J
jrose 已提交
132 133 134 135
    array_name(const int length, const etype fx)      { initialize(length, fx); }        \
    void initialize(const int length)     { base_class::initialize(esize, length); }     \
    void initialize(const int length, const etype fx) {                                  \
      initialize(length);                                                                \
D
duke 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
      for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx;                          \
    }                                                                                    \
                                                                                         \
    /* standard operations */                                                            \
    etype& operator [] (const int i) const {                                             \
      assert(0 <= i && i < length(), "index out of bounds");                             \
      return ((etype*)_data)[i];                                                         \
    }                                                                                    \
                                                                                         \
    int index_of(const etype x) const {                                                  \
      int i = length();                                                                  \
      while (i-- > 0 && ((etype*)_data)[i] != x) ;                                       \
      /* i < 0 || ((etype*)_data)_data[i] == x */                                        \
      return i;                                                                          \
    }                                                                                    \
                                                                                         \
    void sort(int f(etype*, etype*))             { base_class::sort(esize, (ftype)f); }  \
    bool contains(const etype x) const           { return index_of(x) >= 0; }            \
                                                                                         \
    /* deprecated operations - for compatibility with GrowableArray only */              \
    etype  at(const int i) const                 { return (*this)[i]; }                  \
    void   at_put(const int i, const etype x)    { (*this)[i] = x; }                     \
    etype* adr_at(const int i)                   { return &(*this)[i]; }                 \
    int    find(const etype x)                   { return index_of(x); }                 \
  };                                                                                     \


#define define_array(array_name,element_type)                                            \
  define_generic_array(array_name, element_type, ResourceArray)


#define define_stack(stack_name,array_name)                                              \
  class stack_name: public array_name {                                                  \
   protected:                                                                            \
    int _size;                                                                           \
                                                                                         \
    void grow(const int i, const etype fx) {                                             \
      assert(i >= length(), "index too small");                                          \
      if (i >= size()) expand(esize, i, _size);                                          \
      for (int j = length(); j <= i; j++) ((etype*)_data)[j] = fx;                       \
      _length = i+1;                                                                     \
    }                                                                                    \
                                                                                         \
   public:                                                                               \
    /* creation */                                                                       \
J
jrose 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193
    stack_name() : array_name()                     { _size = 0; }                       \
    stack_name(const int size)                      { initialize(size); }                \
    stack_name(const int size, const etype fx)      { initialize(size, fx); }            \
    void initialize(const int size, const etype fx) {                                    \
      _size = size;                                                                      \
      array_name::initialize(size, fx);                                                  \
      /* _length == size, allocation and size are the same */                            \
    }                                                                                    \
    void initialize(const int size) {                                                    \
      _size = size;                                                                      \
      array_name::initialize(size);                                                      \
      _length = 0;          /* reset length to zero; _size records the allocation */     \
    }                                                                                    \
D
duke 已提交
194 195 196 197
                                                                                         \
    /* standard operations */                                                            \
    int size() const                             { return _size; }                       \
                                                                                         \
J
jrose 已提交
198 199 200 201 202 203
    int push(const etype x) {                                                            \
      int len = length();                                                                \
      if (len >= size()) expand(esize, len, _size);                                      \
      ((etype*)_data)[len] = x;                                                          \
      _length = len+1;                                                                   \
      return len;                                                                        \
D
duke 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    }                                                                                    \
                                                                                         \
    etype pop() {                                                                        \
      assert(!is_empty(), "stack is empty");                                             \
      return ((etype*)_data)[--_length];                                                 \
    }                                                                                    \
                                                                                         \
    etype top() const {                                                                  \
      assert(!is_empty(), "stack is empty");                                             \
      return ((etype*)_data)[length() - 1];                                              \
    }                                                                                    \
                                                                                         \
    void push_all(const stack_name* stack) {                                             \
      const int l = stack->length();                                                     \
      for (int i = 0; i < l; i++) push(((etype*)(stack->_data))[i]);                     \
    }                                                                                    \
                                                                                         \
    etype at_grow(const int i, const etype fx) {                                         \
      if (i >= length()) grow(i, fx);                                                    \
      return ((etype*)_data)[i];                                                         \
    }                                                                                    \
                                                                                         \
    void at_put_grow(const int i, const etype x, const etype fx) {                       \
      if (i >= length()) grow(i, fx);                                                    \
      ((etype*)_data)[i] = x;                                                            \
    }                                                                                    \
                                                                                         \
    void truncate(const int length) {                                                    \
      assert(0 <= length && length <= this->length(), "illegal length");                 \
      _length = length;                                                                  \
    }                                                                                    \
                                                                                         \
    void remove_at(int i)                        { base_remove_at(esize, i); }           \
    void remove(etype x)                         { remove_at(index_of(x)); }             \
                                                                                         \
    /* inserts the given element before the element at index i */                        \
    void insert_before(const int i, const etype el)  {                                   \
      int len = length();                                                                \
      int new_length = len + 1;                                                          \
      if (new_length >= size()) expand(esize, new_length, _size);                        \
      for (int j = len - 1; j >= i; j--) {                                               \
        ((etype*)_data)[j + 1] = ((etype*)_data)[j];                                     \
      }                                                                                  \
      _length = new_length;                                                              \
      at_put(i, el);                                                                     \
    }                                                                                    \
                                                                                         \
    /* inserts contents of the given stack before the element at index i */              \
    void insert_before(const int i, const stack_name *st) {                              \
      if (st->length() == 0) return;                                                     \
      int len = length();                                                                \
      int st_len = st->length();                                                         \
      int new_length = len + st_len;                                                     \
      if (new_length >= size()) expand(esize, new_length, _size);                        \
      int j;                                                                             \
      for (j = len - 1; j >= i; j--) {                                                   \
        ((etype*)_data)[j + st_len] = ((etype*)_data)[j];                                \
      }                                                                                  \
      for (j = 0; j < st_len; j++) {                                                     \
        ((etype*)_data)[i + j] = ((etype*)st->_data)[j];                                 \
      }                                                                                  \
      _length = new_length;                                                              \
    }                                                                                    \
                                                                                         \
    /* deprecated operations - for compatibility with GrowableArray only */              \
    int  capacity() const                        { return size(); }                      \
    void clear()                                 { truncate(0); }                        \
    void trunc_to(const int length)              { truncate(length); }                   \
J
jrose 已提交
272
    int  append(const etype x)                   { return push(x); }                     \
D
duke 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    void appendAll(const stack_name* stack)      { push_all(stack); }                    \
    etype last() const                           { return top(); }                       \
  };                                                                                     \


#define define_resource_list(element_type)                                               \
  define_generic_array(element_type##Array, element_type, ResourceArray)                 \
  define_stack(element_type##List, element_type##Array)

#define define_resource_pointer_list(element_type)                                       \
  define_generic_array(element_type##Array, element_type *, ResourceArray)               \
  define_stack(element_type##List, element_type##Array)

#define define_c_heap_list(element_type)                                                 \
  define_generic_array(element_type##Array, element_type, CHeapArray)                    \
  define_stack(element_type##List, element_type##Array)

#define define_c_heap_pointer_list(element_type)                                         \
  define_generic_array(element_type##Array, element_type *, CHeapArray)                  \
  define_stack(element_type##List, element_type##Array)


// Arrays for basic types

define_array(boolArray, bool)          define_stack(boolStack, boolArray)
define_array(intArray , int )          define_stack(intStack , intArray )
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
// Array for metadata allocation

template <typename T>
class Array: public MetaspaceObj {
  friend class MetadataFactory;
  friend class VMStructs;
  friend class MethodHandleCompiler;           // special case
protected:
  int _length;                                 // the number of array elements
  T   _data[1];                                // the array memory

  void initialize(int length) {
    _length = length;
  }

 private:
  // Turn off copy constructor and assignment operator.
  Array(const Array<T>&);
  void operator=(const Array<T>&);

320
  void* operator new(size_t size, ClassLoaderData* loader_data, int length, bool read_only, TRAPS) throw() {
321 322
    size_t word_size = Array::size(length);
    return (void*) Metaspace::allocate(loader_data, word_size, read_only,
323
                                       MetaspaceObj::array_type(sizeof(T)), CHECK_NULL);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
  }

  static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); }

  explicit Array(int length) : _length(length) {
    assert(length >= 0, "illegal length");
  }

  Array(int length, T init) : _length(length) {
    assert(length >= 0, "illegal length");
    for (int i = 0; i < length; i++) {
      _data[i] = init;
    }
  }

 public:

  // standard operations
  int  length() const                 { return _length; }
  T* data()                           { return _data; }
  bool is_empty() const               { return length() == 0; }

  int index_of(const T& x) const {
    int i = length();
    while (i-- > 0 && _data[i] != x) ;

    return i;
  }

  // sort the array.
  bool contains(const T& x) const      { return index_of(x) >= 0; }

356 357 358
  T    at(int i) const                 { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return _data[i]; }
  void at_put(const int i, const T& x) { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); _data[i] = x; }
  T*   adr_at(const int i)             { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return &_data[i]; }
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  int  find(const T& x)                { return index_of(x); }

  T at_acquire(const int which)              { return OrderAccess::load_acquire(adr_at(which)); }
  void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); }

  static int size(int length) {
    return align_size_up(byte_sizeof(length), BytesPerWord) / BytesPerWord;
  }

  int size() {
    return size(_length);
  }

  static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); }
  // Note, this offset don't have to be wordSize aligned.
  static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); };

  // FIXME: How to handle this?
  void print_value_on(outputStream* st) const {
    st->print("Array<T>(" INTPTR_FORMAT ")", this);
  }

#ifndef PRODUCT
  void print(outputStream* st) {
     for (int i = 0; i< _length; i++) {
       st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i));
     }
  }
  void print() { print(tty); }
#endif // PRODUCT
};


392
#endif // SHARE_VM_UTILITIES_ARRAY_HPP