src/share/vm/utilities/array.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
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"
aoqi@0 31
aoqi@0 32 // correct linkage required to compile w/o warnings
aoqi@0 33 // (must be on file level - cannot be local)
aoqi@0 34 extern "C" { typedef int (*ftype)(const void*, const void*); }
aoqi@0 35
aoqi@0 36
aoqi@0 37 class ResourceArray: public ResourceObj {
aoqi@0 38 protected:
aoqi@0 39 int _length; // the number of array elements
aoqi@0 40 void* _data; // the array memory
aoqi@0 41 #ifdef ASSERT
aoqi@0 42 int _nesting; // the resource area nesting level
aoqi@0 43 #endif
aoqi@0 44
aoqi@0 45 // creation
aoqi@0 46 ResourceArray() {
aoqi@0 47 _length = 0;
aoqi@0 48 _data = NULL;
aoqi@0 49 DEBUG_ONLY(init_nesting();)
aoqi@0 50 // client may call initialize, at most once
aoqi@0 51 }
aoqi@0 52
aoqi@0 53
aoqi@0 54 ResourceArray(size_t esize, int length) {
aoqi@0 55 DEBUG_ONLY(_data = NULL);
aoqi@0 56 initialize(esize, length);
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 void initialize(size_t esize, int length) {
aoqi@0 60 assert(length >= 0, "illegal length");
aoqi@0 61 assert(StressRewriter || _data == NULL, "must be new object");
aoqi@0 62 _length = length;
aoqi@0 63 _data = resource_allocate_bytes(esize * length);
aoqi@0 64 DEBUG_ONLY(init_nesting();)
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 #ifdef ASSERT
aoqi@0 68 void init_nesting();
aoqi@0 69 #endif
aoqi@0 70
aoqi@0 71 // helper functions
aoqi@0 72 void sort (size_t esize, ftype f); // sort the array
aoqi@0 73 void expand (size_t esize, int i, int& size);// expand the array to include slot i
aoqi@0 74 void remove_at(size_t esize, int i); // remove the element in slot i
aoqi@0 75
aoqi@0 76 public:
aoqi@0 77 // standard operations
aoqi@0 78 int length() const { return _length; }
aoqi@0 79 bool is_empty() const { return length() == 0; }
aoqi@0 80 };
aoqi@0 81
aoqi@0 82
aoqi@0 83 template <MEMFLAGS F>class CHeapArray: public CHeapObj<F> {
aoqi@0 84 protected:
aoqi@0 85 int _length; // the number of array elements
aoqi@0 86 void* _data; // the array memory
aoqi@0 87
aoqi@0 88 // creation
aoqi@0 89 CHeapArray() {
aoqi@0 90 _length = 0;
aoqi@0 91 _data = NULL;
aoqi@0 92 }
aoqi@0 93
aoqi@0 94
aoqi@0 95 CHeapArray(size_t esize, int length) {
aoqi@0 96 assert(length >= 0, "illegal length");
aoqi@0 97 _length = length;
aoqi@0 98 _data = (void*) NEW_C_HEAP_ARRAY(char *, esize * length, F);
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 void initialize(size_t esize, int length) {
aoqi@0 102 // In debug set array to 0?
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 #ifdef ASSERT
aoqi@0 106 void init_nesting();
aoqi@0 107 #endif
aoqi@0 108
aoqi@0 109 // helper functions
aoqi@0 110 void sort (size_t esize, ftype f); // sort the array
aoqi@0 111 void expand (size_t esize, int i, int& size);// expand the array to include slot i
aoqi@0 112 void remove_at(size_t esize, int i); // remove the element in slot i
aoqi@0 113
aoqi@0 114 public:
aoqi@0 115 // standard operations
aoqi@0 116 int length() const { return _length; }
aoqi@0 117 bool is_empty() const { return length() == 0; }
aoqi@0 118 };
aoqi@0 119
aoqi@0 120 #define define_generic_array(array_name,element_type, base_class) \
aoqi@0 121 class array_name: public base_class { \
aoqi@0 122 protected: \
aoqi@0 123 typedef element_type etype; \
aoqi@0 124 enum { esize = sizeof(etype) }; \
aoqi@0 125 \
aoqi@0 126 void base_remove_at(size_t size, int i) { base_class::remove_at(size, i); } \
aoqi@0 127 \
aoqi@0 128 public: \
aoqi@0 129 /* creation */ \
aoqi@0 130 array_name() : base_class() {} \
aoqi@0 131 explicit array_name(const int length) : base_class(esize, length) {} \
aoqi@0 132 array_name(const int length, const etype fx) { initialize(length, fx); } \
aoqi@0 133 void initialize(const int length) { base_class::initialize(esize, length); } \
aoqi@0 134 void initialize(const int length, const etype fx) { \
aoqi@0 135 initialize(length); \
aoqi@0 136 for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx; \
aoqi@0 137 } \
aoqi@0 138 \
aoqi@0 139 /* standard operations */ \
aoqi@0 140 etype& operator [] (const int i) const { \
aoqi@0 141 assert(0 <= i && i < length(), "index out of bounds"); \
aoqi@0 142 return ((etype*)_data)[i]; \
aoqi@0 143 } \
aoqi@0 144 \
aoqi@0 145 int index_of(const etype x) const { \
aoqi@0 146 int i = length(); \
aoqi@0 147 while (i-- > 0 && ((etype*)_data)[i] != x) ; \
aoqi@0 148 /* i < 0 || ((etype*)_data)_data[i] == x */ \
aoqi@0 149 return i; \
aoqi@0 150 } \
aoqi@0 151 \
aoqi@0 152 void sort(int f(etype*, etype*)) { base_class::sort(esize, (ftype)f); } \
aoqi@0 153 bool contains(const etype x) const { return index_of(x) >= 0; } \
aoqi@0 154 \
aoqi@0 155 /* deprecated operations - for compatibility with GrowableArray only */ \
aoqi@0 156 etype at(const int i) const { return (*this)[i]; } \
aoqi@0 157 void at_put(const int i, const etype x) { (*this)[i] = x; } \
aoqi@0 158 etype* adr_at(const int i) { return &(*this)[i]; } \
aoqi@0 159 int find(const etype x) { return index_of(x); } \
aoqi@0 160 }; \
aoqi@0 161
aoqi@0 162
aoqi@0 163 #define define_array(array_name,element_type) \
aoqi@0 164 define_generic_array(array_name, element_type, ResourceArray)
aoqi@0 165
aoqi@0 166
aoqi@0 167 #define define_stack(stack_name,array_name) \
aoqi@0 168 class stack_name: public array_name { \
aoqi@0 169 protected: \
aoqi@0 170 int _size; \
aoqi@0 171 \
aoqi@0 172 void grow(const int i, const etype fx) { \
aoqi@0 173 assert(i >= length(), "index too small"); \
aoqi@0 174 if (i >= size()) expand(esize, i, _size); \
aoqi@0 175 for (int j = length(); j <= i; j++) ((etype*)_data)[j] = fx; \
aoqi@0 176 _length = i+1; \
aoqi@0 177 } \
aoqi@0 178 \
aoqi@0 179 public: \
aoqi@0 180 /* creation */ \
aoqi@0 181 stack_name() : array_name() { _size = 0; } \
aoqi@0 182 stack_name(const int size) { initialize(size); } \
aoqi@0 183 stack_name(const int size, const etype fx) { initialize(size, fx); } \
aoqi@0 184 void initialize(const int size, const etype fx) { \
aoqi@0 185 _size = size; \
aoqi@0 186 array_name::initialize(size, fx); \
aoqi@0 187 /* _length == size, allocation and size are the same */ \
aoqi@0 188 } \
aoqi@0 189 void initialize(const int size) { \
aoqi@0 190 _size = size; \
aoqi@0 191 array_name::initialize(size); \
aoqi@0 192 _length = 0; /* reset length to zero; _size records the allocation */ \
aoqi@0 193 } \
aoqi@0 194 \
aoqi@0 195 /* standard operations */ \
aoqi@0 196 int size() const { return _size; } \
aoqi@0 197 \
aoqi@0 198 int push(const etype x) { \
aoqi@0 199 int len = length(); \
aoqi@0 200 if (len >= size()) expand(esize, len, _size); \
aoqi@0 201 ((etype*)_data)[len] = x; \
aoqi@0 202 _length = len+1; \
aoqi@0 203 return len; \
aoqi@0 204 } \
aoqi@0 205 \
aoqi@0 206 etype pop() { \
aoqi@0 207 assert(!is_empty(), "stack is empty"); \
aoqi@0 208 return ((etype*)_data)[--_length]; \
aoqi@0 209 } \
aoqi@0 210 \
aoqi@0 211 etype top() const { \
aoqi@0 212 assert(!is_empty(), "stack is empty"); \
aoqi@0 213 return ((etype*)_data)[length() - 1]; \
aoqi@0 214 } \
aoqi@0 215 \
aoqi@0 216 void push_all(const stack_name* stack) { \
aoqi@0 217 const int l = stack->length(); \
aoqi@0 218 for (int i = 0; i < l; i++) push(((etype*)(stack->_data))[i]); \
aoqi@0 219 } \
aoqi@0 220 \
aoqi@0 221 etype at_grow(const int i, const etype fx) { \
aoqi@0 222 if (i >= length()) grow(i, fx); \
aoqi@0 223 return ((etype*)_data)[i]; \
aoqi@0 224 } \
aoqi@0 225 \
aoqi@0 226 void at_put_grow(const int i, const etype x, const etype fx) { \
aoqi@0 227 if (i >= length()) grow(i, fx); \
aoqi@0 228 ((etype*)_data)[i] = x; \
aoqi@0 229 } \
aoqi@0 230 \
aoqi@0 231 void truncate(const int length) { \
aoqi@0 232 assert(0 <= length && length <= this->length(), "illegal length"); \
aoqi@0 233 _length = length; \
aoqi@0 234 } \
aoqi@0 235 \
aoqi@0 236 void remove_at(int i) { base_remove_at(esize, i); } \
aoqi@0 237 void remove(etype x) { remove_at(index_of(x)); } \
aoqi@0 238 \
aoqi@0 239 /* inserts the given element before the element at index i */ \
aoqi@0 240 void insert_before(const int i, const etype el) { \
aoqi@0 241 int len = length(); \
aoqi@0 242 int new_length = len + 1; \
aoqi@0 243 if (new_length >= size()) expand(esize, new_length, _size); \
aoqi@0 244 for (int j = len - 1; j >= i; j--) { \
aoqi@0 245 ((etype*)_data)[j + 1] = ((etype*)_data)[j]; \
aoqi@0 246 } \
aoqi@0 247 _length = new_length; \
aoqi@0 248 at_put(i, el); \
aoqi@0 249 } \
aoqi@0 250 \
aoqi@0 251 /* inserts contents of the given stack before the element at index i */ \
aoqi@0 252 void insert_before(const int i, const stack_name *st) { \
aoqi@0 253 if (st->length() == 0) return; \
aoqi@0 254 int len = length(); \
aoqi@0 255 int st_len = st->length(); \
aoqi@0 256 int new_length = len + st_len; \
aoqi@0 257 if (new_length >= size()) expand(esize, new_length, _size); \
aoqi@0 258 int j; \
aoqi@0 259 for (j = len - 1; j >= i; j--) { \
aoqi@0 260 ((etype*)_data)[j + st_len] = ((etype*)_data)[j]; \
aoqi@0 261 } \
aoqi@0 262 for (j = 0; j < st_len; j++) { \
aoqi@0 263 ((etype*)_data)[i + j] = ((etype*)st->_data)[j]; \
aoqi@0 264 } \
aoqi@0 265 _length = new_length; \
aoqi@0 266 } \
aoqi@0 267 \
aoqi@0 268 /* deprecated operations - for compatibility with GrowableArray only */ \
aoqi@0 269 int capacity() const { return size(); } \
aoqi@0 270 void clear() { truncate(0); } \
aoqi@0 271 void trunc_to(const int length) { truncate(length); } \
aoqi@0 272 int append(const etype x) { return push(x); } \
aoqi@0 273 void appendAll(const stack_name* stack) { push_all(stack); } \
aoqi@0 274 etype last() const { return top(); } \
aoqi@0 275 }; \
aoqi@0 276
aoqi@0 277
aoqi@0 278 #define define_resource_list(element_type) \
aoqi@0 279 define_generic_array(element_type##Array, element_type, ResourceArray) \
aoqi@0 280 define_stack(element_type##List, element_type##Array)
aoqi@0 281
aoqi@0 282 #define define_resource_pointer_list(element_type) \
aoqi@0 283 define_generic_array(element_type##Array, element_type *, ResourceArray) \
aoqi@0 284 define_stack(element_type##List, element_type##Array)
aoqi@0 285
aoqi@0 286 #define define_c_heap_list(element_type) \
aoqi@0 287 define_generic_array(element_type##Array, element_type, CHeapArray) \
aoqi@0 288 define_stack(element_type##List, element_type##Array)
aoqi@0 289
aoqi@0 290 #define define_c_heap_pointer_list(element_type) \
aoqi@0 291 define_generic_array(element_type##Array, element_type *, CHeapArray) \
aoqi@0 292 define_stack(element_type##List, element_type##Array)
aoqi@0 293
aoqi@0 294
aoqi@0 295 // Arrays for basic types
aoqi@0 296
aoqi@0 297 define_array(boolArray, bool) define_stack(boolStack, boolArray)
aoqi@0 298 define_array(intArray , int ) define_stack(intStack , intArray )
aoqi@0 299
aoqi@0 300 // Array for metadata allocation
aoqi@0 301
aoqi@0 302 template <typename T>
aoqi@0 303 class Array: public MetaspaceObj {
aoqi@0 304 friend class MetadataFactory;
aoqi@0 305 friend class VMStructs;
aoqi@0 306 friend class MethodHandleCompiler; // special case
aoqi@0 307 protected:
aoqi@0 308 int _length; // the number of array elements
aoqi@0 309 T _data[1]; // the array memory
aoqi@0 310
aoqi@0 311 void initialize(int length) {
aoqi@0 312 _length = length;
aoqi@0 313 }
aoqi@0 314
aoqi@0 315 private:
aoqi@0 316 // Turn off copy constructor and assignment operator.
aoqi@0 317 Array(const Array<T>&);
aoqi@0 318 void operator=(const Array<T>&);
aoqi@0 319
aoqi@0 320 void* operator new(size_t size, ClassLoaderData* loader_data, int length, bool read_only, TRAPS) throw() {
aoqi@0 321 size_t word_size = Array::size(length);
aoqi@0 322 return (void*) Metaspace::allocate(loader_data, word_size, read_only,
aoqi@0 323 MetaspaceObj::array_type(sizeof(T)), CHECK_NULL);
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 static size_t byte_sizeof(int length) { return sizeof(Array<T>) + MAX2(length - 1, 0) * sizeof(T); }
aoqi@0 327
aoqi@0 328 explicit Array(int length) : _length(length) {
aoqi@0 329 assert(length >= 0, "illegal length");
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 Array(int length, T init) : _length(length) {
aoqi@0 333 assert(length >= 0, "illegal length");
aoqi@0 334 for (int i = 0; i < length; i++) {
aoqi@0 335 _data[i] = init;
aoqi@0 336 }
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 public:
aoqi@0 340
aoqi@0 341 // standard operations
aoqi@0 342 int length() const { return _length; }
aoqi@0 343 T* data() { return _data; }
aoqi@0 344 bool is_empty() const { return length() == 0; }
aoqi@0 345
aoqi@0 346 int index_of(const T& x) const {
aoqi@0 347 int i = length();
aoqi@0 348 while (i-- > 0 && _data[i] != x) ;
aoqi@0 349
aoqi@0 350 return i;
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 // sort the array.
aoqi@0 354 bool contains(const T& x) const { return index_of(x) >= 0; }
aoqi@0 355
aoqi@0 356 T at(int i) const { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return _data[i]; }
aoqi@0 357 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 358 T* adr_at(const int i) { assert(i >= 0 && i< _length, err_msg("oob: 0 <= %d < %d", i, _length)); return &_data[i]; }
aoqi@0 359 int find(const T& x) { return index_of(x); }
aoqi@0 360
aoqi@0 361 T at_acquire(const int which) { return OrderAccess::load_acquire(adr_at(which)); }
aoqi@0 362 void release_at_put(int which, T contents) { OrderAccess::release_store(adr_at(which), contents); }
aoqi@0 363
aoqi@0 364 static int size(int length) {
aoqi@0 365 return align_size_up(byte_sizeof(length), BytesPerWord) / BytesPerWord;
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 int size() {
aoqi@0 369 return size(_length);
aoqi@0 370 }
aoqi@0 371
aoqi@0 372 static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); }
aoqi@0 373 // Note, this offset don't have to be wordSize aligned.
aoqi@0 374 static int base_offset_in_bytes() { return (int) (offset_of(Array<T>, _data)); };
aoqi@0 375
aoqi@0 376 // FIXME: How to handle this?
aoqi@0 377 void print_value_on(outputStream* st) const {
aoqi@0 378 st->print("Array<T>(" INTPTR_FORMAT ")", p2i(this));
aoqi@0 379 }
aoqi@0 380
aoqi@0 381 #ifndef PRODUCT
aoqi@0 382 void print(outputStream* st) {
aoqi@0 383 for (int i = 0; i< _length; i++) {
aoqi@0 384 st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i));
aoqi@0 385 }
aoqi@0 386 }
aoqi@0 387 void print() { print(tty); }
aoqi@0 388 #endif // PRODUCT
aoqi@0 389 };
aoqi@0 390
aoqi@0 391
aoqi@0 392 #endif // SHARE_VM_UTILITIES_ARRAY_HPP

mercurial