src/share/vm/utilities/array.hpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6993
870c03421152
parent 6876
710a3c8b516e
child 9572
624a0741915c
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_UTILITIES_ARRAY_HPP
aoqi@0 26 #define SHARE_VM_UTILITIES_ARRAY_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "memory/allocation.inline.hpp"
aoqi@0 30 #include "memory/metaspace.hpp"
goetz@6911 31 #include "runtime/orderAccess.hpp"
aoqi@0 32
aoqi@0 33 // correct linkage required to compile w/o warnings
aoqi@0 34 // (must be on file level - cannot be local)
aoqi@0 35 extern "C" { typedef int (*ftype)(const void*, const void*); }
aoqi@0 36
aoqi@0 37
aoqi@0 38 class ResourceArray: public ResourceObj {
aoqi@0 39 protected:
aoqi@0 40 int _length; // the number of array elements
aoqi@0 41 void* _data; // the array memory
aoqi@0 42 #ifdef ASSERT
aoqi@0 43 int _nesting; // the resource area nesting level
aoqi@0 44 #endif
aoqi@0 45
aoqi@0 46 // creation
aoqi@0 47 ResourceArray() {
aoqi@0 48 _length = 0;
aoqi@0 49 _data = NULL;
aoqi@0 50 DEBUG_ONLY(init_nesting();)
aoqi@0 51 // client may call initialize, at most once
aoqi@0 52 }
aoqi@0 53
aoqi@0 54
aoqi@0 55 ResourceArray(size_t esize, int length) {
aoqi@0 56 DEBUG_ONLY(_data = NULL);
aoqi@0 57 initialize(esize, length);
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 void initialize(size_t esize, int length) {
aoqi@0 61 assert(length >= 0, "illegal length");
aoqi@0 62 assert(StressRewriter || _data == NULL, "must be new object");
aoqi@0 63 _length = length;
aoqi@0 64 _data = resource_allocate_bytes(esize * length);
aoqi@0 65 DEBUG_ONLY(init_nesting();)
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 #ifdef ASSERT
aoqi@0 69 void init_nesting();
aoqi@0 70 #endif
aoqi@0 71
aoqi@0 72 // helper functions
aoqi@0 73 void sort (size_t esize, ftype f); // sort the array
aoqi@0 74 void expand (size_t esize, int i, int& size);// expand the array to include slot i
aoqi@0 75 void remove_at(size_t esize, int i); // remove the element in slot i
aoqi@0 76
aoqi@0 77 public:
aoqi@0 78 // standard operations
aoqi@0 79 int length() const { return _length; }
aoqi@0 80 bool is_empty() const { return length() == 0; }
aoqi@0 81 };
aoqi@0 82
aoqi@0 83
aoqi@0 84 template <MEMFLAGS F>class CHeapArray: public CHeapObj<F> {
aoqi@0 85 protected:
aoqi@0 86 int _length; // the number of array elements
aoqi@0 87 void* _data; // the array memory
aoqi@0 88
aoqi@0 89 // creation
aoqi@0 90 CHeapArray() {
aoqi@0 91 _length = 0;
aoqi@0 92 _data = NULL;
aoqi@0 93 }
aoqi@0 94
aoqi@0 95
aoqi@0 96 CHeapArray(size_t esize, int length) {
aoqi@0 97 assert(length >= 0, "illegal length");
aoqi@0 98 _length = length;
aoqi@0 99 _data = (void*) NEW_C_HEAP_ARRAY(char *, esize * length, F);
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 void initialize(size_t esize, int length) {
aoqi@0 103 // In debug set array to 0?
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 #ifdef ASSERT
aoqi@0 107 void init_nesting();
aoqi@0 108 #endif
aoqi@0 109
aoqi@0 110 // helper functions
aoqi@0 111 void sort (size_t esize, ftype f); // sort the array
aoqi@0 112 void expand (size_t esize, int i, int& size);// expand the array to include slot i
aoqi@0 113 void remove_at(size_t esize, int i); // remove the element in slot i
aoqi@0 114
aoqi@0 115 public:
aoqi@0 116 // standard operations
aoqi@0 117 int length() const { return _length; }
aoqi@0 118 bool is_empty() const { return length() == 0; }
aoqi@0 119 };
aoqi@0 120
aoqi@0 121 #define define_generic_array(array_name,element_type, base_class) \
aoqi@0 122 class array_name: public base_class { \
aoqi@0 123 protected: \
aoqi@0 124 typedef element_type etype; \
aoqi@0 125 enum { esize = sizeof(etype) }; \
aoqi@0 126 \
aoqi@0 127 void base_remove_at(size_t size, int i) { base_class::remove_at(size, i); } \
aoqi@0 128 \
aoqi@0 129 public: \
aoqi@0 130 /* creation */ \
aoqi@0 131 array_name() : base_class() {} \
aoqi@0 132 explicit array_name(const int length) : base_class(esize, length) {} \
aoqi@0 133 array_name(const int length, const etype fx) { initialize(length, fx); } \
aoqi@0 134 void initialize(const int length) { base_class::initialize(esize, length); } \
aoqi@0 135 void initialize(const int length, const etype fx) { \
aoqi@0 136 initialize(length); \
aoqi@0 137 for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx; \
aoqi@0 138 } \
aoqi@0 139 \
aoqi@0 140 /* standard operations */ \
aoqi@0 141 etype& operator [] (const int i) const { \
aoqi@0 142 assert(0 <= i && i < length(), "index out of bounds"); \
aoqi@0 143 return ((etype*)_data)[i]; \
aoqi@0 144 } \
aoqi@0 145 \
aoqi@0 146 int index_of(const etype x) const { \
aoqi@0 147 int i = length(); \
aoqi@0 148 while (i-- > 0 && ((etype*)_data)[i] != x) ; \
aoqi@0 149 /* i < 0 || ((etype*)_data)_data[i] == x */ \
aoqi@0 150 return i; \
aoqi@0 151 } \
aoqi@0 152 \
aoqi@0 153 void sort(int f(etype*, etype*)) { base_class::sort(esize, (ftype)f); } \
aoqi@0 154 bool contains(const etype x) const { return index_of(x) >= 0; } \
aoqi@0 155 \
aoqi@0 156 /* deprecated operations - for compatibility with GrowableArray only */ \
aoqi@0 157 etype at(const int i) const { return (*this)[i]; } \
aoqi@0 158 void at_put(const int i, const etype x) { (*this)[i] = x; } \
aoqi@0 159 etype* adr_at(const int i) { return &(*this)[i]; } \
aoqi@0 160 int find(const etype x) { return index_of(x); } \
aoqi@0 161 }; \
aoqi@0 162
aoqi@0 163
aoqi@0 164 #define define_array(array_name,element_type) \
aoqi@0 165 define_generic_array(array_name, element_type, ResourceArray)
aoqi@0 166
aoqi@0 167
aoqi@0 168 #define define_stack(stack_name,array_name) \
aoqi@0 169 class stack_name: public array_name { \
aoqi@0 170 protected: \
aoqi@0 171 int _size; \
aoqi@0 172 \
aoqi@0 173 void grow(const int i, const etype fx) { \
aoqi@0 174 assert(i >= length(), "index too small"); \
aoqi@0 175 if (i >= size()) expand(esize, i, _size); \
aoqi@0 176 for (int j = length(); j <= i; j++) ((etype*)_data)[j] = fx; \
aoqi@0 177 _length = i+1; \
aoqi@0 178 } \
aoqi@0 179 \
aoqi@0 180 public: \
aoqi@0 181 /* creation */ \
aoqi@0 182 stack_name() : array_name() { _size = 0; } \
aoqi@0 183 stack_name(const int size) { initialize(size); } \
aoqi@0 184 stack_name(const int size, const etype fx) { initialize(size, fx); } \
aoqi@0 185 void initialize(const int size, const etype fx) { \
aoqi@0 186 _size = size; \
aoqi@0 187 array_name::initialize(size, fx); \
aoqi@0 188 /* _length == size, allocation and size are the same */ \
aoqi@0 189 } \
aoqi@0 190 void initialize(const int size) { \
aoqi@0 191 _size = size; \
aoqi@0 192 array_name::initialize(size); \
aoqi@0 193 _length = 0; /* reset length to zero; _size records the allocation */ \
aoqi@0 194 } \
aoqi@0 195 \
aoqi@0 196 /* standard operations */ \
aoqi@0 197 int size() const { return _size; } \
aoqi@0 198 \
aoqi@0 199 int push(const etype x) { \
aoqi@0 200 int len = length(); \
aoqi@0 201 if (len >= size()) expand(esize, len, _size); \
aoqi@0 202 ((etype*)_data)[len] = x; \
aoqi@0 203 _length = len+1; \
aoqi@0 204 return len; \
aoqi@0 205 } \
aoqi@0 206 \
aoqi@0 207 etype pop() { \
aoqi@0 208 assert(!is_empty(), "stack is empty"); \
aoqi@0 209 return ((etype*)_data)[--_length]; \
aoqi@0 210 } \
aoqi@0 211 \
aoqi@0 212 etype top() const { \
aoqi@0 213 assert(!is_empty(), "stack is empty"); \
aoqi@0 214 return ((etype*)_data)[length() - 1]; \
aoqi@0 215 } \
aoqi@0 216 \
aoqi@0 217 void push_all(const stack_name* stack) { \
aoqi@0 218 const int l = stack->length(); \
aoqi@0 219 for (int i = 0; i < l; i++) push(((etype*)(stack->_data))[i]); \
aoqi@0 220 } \
aoqi@0 221 \
aoqi@0 222 etype at_grow(const int i, const etype fx) { \
aoqi@0 223 if (i >= length()) grow(i, fx); \
aoqi@0 224 return ((etype*)_data)[i]; \
aoqi@0 225 } \
aoqi@0 226 \
aoqi@0 227 void at_put_grow(const int i, const etype x, const etype fx) { \
aoqi@0 228 if (i >= length()) grow(i, fx); \
aoqi@0 229 ((etype*)_data)[i] = x; \
aoqi@0 230 } \
aoqi@0 231 \
aoqi@0 232 void truncate(const int length) { \
aoqi@0 233 assert(0 <= length && length <= this->length(), "illegal length"); \
aoqi@0 234 _length = length; \
aoqi@0 235 } \
aoqi@0 236 \
aoqi@0 237 void remove_at(int i) { base_remove_at(esize, i); } \
aoqi@0 238 void remove(etype x) { remove_at(index_of(x)); } \
aoqi@0 239 \
aoqi@0 240 /* inserts the given element before the element at index i */ \
aoqi@0 241 void insert_before(const int i, const etype el) { \
aoqi@0 242 int len = length(); \
aoqi@0 243 int new_length = len + 1; \
aoqi@0 244 if (new_length >= size()) expand(esize, new_length, _size); \
aoqi@0 245 for (int j = len - 1; j >= i; j--) { \
aoqi@0 246 ((etype*)_data)[j + 1] = ((etype*)_data)[j]; \
aoqi@0 247 } \
aoqi@0 248 _length = new_length; \
aoqi@0 249 at_put(i, el); \
aoqi@0 250 } \
aoqi@0 251 \
aoqi@0 252 /* inserts contents of the given stack before the element at index i */ \
aoqi@0 253 void insert_before(const int i, const stack_name *st) { \
aoqi@0 254 if (st->length() == 0) return; \
aoqi@0 255 int len = length(); \
aoqi@0 256 int st_len = st->length(); \
aoqi@0 257 int new_length = len + st_len; \
aoqi@0 258 if (new_length >= size()) expand(esize, new_length, _size); \
aoqi@0 259 int j; \
aoqi@0 260 for (j = len - 1; j >= i; j--) { \
aoqi@0 261 ((etype*)_data)[j + st_len] = ((etype*)_data)[j]; \
aoqi@0 262 } \
aoqi@0 263 for (j = 0; j < st_len; j++) { \
aoqi@0 264 ((etype*)_data)[i + j] = ((etype*)st->_data)[j]; \
aoqi@0 265 } \
aoqi@0 266 _length = new_length; \
aoqi@0 267 } \
aoqi@0 268 \
aoqi@0 269 /* deprecated operations - for compatibility with GrowableArray only */ \
aoqi@0 270 int capacity() const { return size(); } \
aoqi@0 271 void clear() { truncate(0); } \
aoqi@0 272 void trunc_to(const int length) { truncate(length); } \
aoqi@0 273 int append(const etype x) { return push(x); } \
aoqi@0 274 void appendAll(const stack_name* stack) { push_all(stack); } \
aoqi@0 275 etype last() const { return top(); } \
aoqi@0 276 }; \
aoqi@0 277
aoqi@0 278
aoqi@0 279 #define define_resource_list(element_type) \
aoqi@0 280 define_generic_array(element_type##Array, element_type, ResourceArray) \
aoqi@0 281 define_stack(element_type##List, element_type##Array)
aoqi@0 282
aoqi@0 283 #define define_resource_pointer_list(element_type) \
aoqi@0 284 define_generic_array(element_type##Array, element_type *, ResourceArray) \
aoqi@0 285 define_stack(element_type##List, element_type##Array)
aoqi@0 286
aoqi@0 287 #define define_c_heap_list(element_type) \
aoqi@0 288 define_generic_array(element_type##Array, element_type, CHeapArray) \
aoqi@0 289 define_stack(element_type##List, element_type##Array)
aoqi@0 290
aoqi@0 291 #define define_c_heap_pointer_list(element_type) \
aoqi@0 292 define_generic_array(element_type##Array, element_type *, CHeapArray) \
aoqi@0 293 define_stack(element_type##List, element_type##Array)
aoqi@0 294
aoqi@0 295
aoqi@0 296 // Arrays for basic types
aoqi@0 297
aoqi@0 298 define_array(boolArray, bool) define_stack(boolStack, boolArray)
aoqi@0 299 define_array(intArray , int ) define_stack(intStack , intArray )
aoqi@0 300
aoqi@0 301 // Array for metadata allocation
aoqi@0 302
aoqi@0 303 template <typename T>
aoqi@0 304 class Array: public MetaspaceObj {
aoqi@0 305 friend class MetadataFactory;
aoqi@0 306 friend class VMStructs;
aoqi@0 307 friend class MethodHandleCompiler; // special case
stefank@6992 308 friend class WhiteBox;
aoqi@0 309 protected:
aoqi@0 310 int _length; // the number of array elements
aoqi@0 311 T _data[1]; // the array memory
aoqi@0 312
aoqi@0 313 void initialize(int length) {
aoqi@0 314 _length = length;
aoqi@0 315 }
aoqi@0 316
aoqi@0 317 private:
aoqi@0 318 // Turn off copy constructor and assignment operator.
aoqi@0 319 Array(const Array<T>&);
aoqi@0 320 void operator=(const Array<T>&);
aoqi@0 321
aoqi@0 322 void* operator new(size_t size, ClassLoaderData* loader_data, int length, bool read_only, TRAPS) throw() {
aoqi@0 323 size_t word_size = Array::size(length);
aoqi@0 324 return (void*) Metaspace::allocate(loader_data, word_size, read_only,
aoqi@0 325 MetaspaceObj::array_type(sizeof(T)), CHECK_NULL);
aoqi@0 326 }
aoqi@0 327
aoqi@0 328 static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); }
aoqi@0 329
stefank@6992 330 // WhiteBox API helper.
ehelin@6993 331 // Can't distinguish between array of length 0 and length 1,
ehelin@6993 332 // will always return 0 in those cases.
stefank@6992 333 static int bytes_to_length(size_t bytes) {
stefank@6992 334 assert(is_size_aligned(bytes, BytesPerWord), "Must be, for now");
stefank@6992 335
stefank@6992 336 if (sizeof(Array<T>) >= bytes) {
stefank@6992 337 return 0;
stefank@6992 338 }
stefank@6992 339
stefank@6992 340 size_t left = bytes - sizeof(Array<T>);
stefank@6992 341 assert(is_size_aligned(left, sizeof(T)), "Must be");
stefank@6992 342
stefank@6992 343 size_t elements = left / sizeof(T);
stefank@6992 344 assert(elements <= (size_t)INT_MAX, err_msg("number of elements " SIZE_FORMAT "doesn't fit into an int.", elements));
stefank@6992 345
stefank@6992 346 int length = (int)elements;
stefank@6992 347
stefank@6992 348 assert((size_t)size(length) * BytesPerWord == bytes,
stefank@6992 349 err_msg("Expected: " SIZE_FORMAT " got: " SIZE_FORMAT,
stefank@6992 350 bytes, (size_t)size(length) * BytesPerWord));
stefank@6992 351
stefank@6992 352 return length;
stefank@6992 353 }
stefank@6992 354
aoqi@0 355 explicit Array(int length) : _length(length) {
aoqi@0 356 assert(length >= 0, "illegal length");
aoqi@0 357 }
aoqi@0 358
aoqi@0 359 Array(int length, T init) : _length(length) {
aoqi@0 360 assert(length >= 0, "illegal length");
aoqi@0 361 for (int i = 0; i < length; i++) {
aoqi@0 362 _data[i] = init;
aoqi@0 363 }
aoqi@0 364 }
aoqi@0 365
aoqi@0 366 public:
aoqi@0 367
aoqi@0 368 // standard operations
aoqi@0 369 int length() const { return _length; }
aoqi@0 370 T* data() { return _data; }
aoqi@0 371 bool is_empty() const { return length() == 0; }
aoqi@0 372
aoqi@0 373 int index_of(const T& x) const {
aoqi@0 374 int i = length();
aoqi@0 375 while (i-- > 0 && _data[i] != x) ;
aoqi@0 376
aoqi@0 377 return i;
aoqi@0 378 }
aoqi@0 379
aoqi@0 380 // sort the array.
aoqi@0 381 bool contains(const T& x) const { return index_of(x) >= 0; }
aoqi@0 382
aoqi@0 383 T at(int i) const { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return _data[i]; }
aoqi@0 384 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; }
aoqi@0 385 T* adr_at(const int i) { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return &_data[i]; }
aoqi@0 386 int find(const T& x) { return index_of(x); }
aoqi@0 387
aoqi@0 388 T at_acquire(const int which) { return OrderAccess::load_acquire(adr_at(which)); }
aoqi@0 389 void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); }
aoqi@0 390
aoqi@0 391 static int size(int length) {
aoqi@0 392 return align_size_up(byte_sizeof(length), BytesPerWord) / BytesPerWord;
aoqi@0 393 }
aoqi@0 394
aoqi@0 395 int size() {
aoqi@0 396 return size(_length);
aoqi@0 397 }
aoqi@0 398
aoqi@0 399 static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); }
aoqi@0 400 // Note, this offset don't have to be wordSize aligned.
aoqi@0 401 static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); };
aoqi@0 402
aoqi@0 403 // FIXME: How to handle this?
aoqi@0 404 void print_value_on(outputStream* st) const {
aoqi@0 405 st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this));
aoqi@0 406 }
aoqi@0 407
aoqi@0 408 #ifndef PRODUCT
aoqi@0 409 void print(outputStream* st) {
aoqi@0 410 for (int i = 0; i< _length; i++) {
aoqi@0 411 st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i));
aoqi@0 412 }
aoqi@0 413 }
aoqi@0 414 void print() { print(tty); }
aoqi@0 415 #endif // PRODUCT
aoqi@0 416 };
aoqi@0 417
aoqi@0 418
aoqi@0 419 #endif // SHARE_VM_UTILITIES_ARRAY_HPP

mercurial