src/share/vm/utilities/array.hpp

Thu, 29 Aug 2013 18:56:29 -0400

author
coleenp
date
Thu, 29 Aug 2013 18:56:29 -0400
changeset 5614
9758d9f36299
parent 5208
a1ebd310d5c1
child 5787
de059a14e159
permissions
-rw-r--r--

8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
Summary: declare all user-defined operator new()s within Hotspot code with the empty throw() exception specification
Reviewed-by: coleenp, twisti, dholmes, hseigel, dcubed, kvn, ccheung
Contributed-by: lois.foltan@oracle.com

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

mercurial