src/share/vm/utilities/growableArray.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, 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_GROWABLEARRAY_HPP
aoqi@0 26 #define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "memory/allocation.inline.hpp"
aoqi@0 30 #include "utilities/debug.hpp"
aoqi@0 31 #include "utilities/globalDefinitions.hpp"
aoqi@0 32 #include "utilities/top.hpp"
aoqi@0 33
aoqi@0 34 // A growable array.
aoqi@0 35
aoqi@0 36 /*************************************************************************/
aoqi@0 37 /* */
aoqi@0 38 /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING */
aoqi@0 39 /* */
aoqi@0 40 /* Should you use GrowableArrays to contain handles you must be certain */
aoqi@0 41 /* the the GrowableArray does not outlive the HandleMark that contains */
aoqi@0 42 /* the handles. Since GrowableArrays are typically resource allocated */
aoqi@0 43 /* the following is an example of INCORRECT CODE, */
aoqi@0 44 /* */
aoqi@0 45 /* ResourceMark rm; */
aoqi@0 46 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size); */
aoqi@0 47 /* if (blah) { */
aoqi@0 48 /* while (...) { */
aoqi@0 49 /* HandleMark hm; */
aoqi@0 50 /* ... */
aoqi@0 51 /* Handle h(THREAD, some_oop); */
aoqi@0 52 /* arr->append(h); */
aoqi@0 53 /* } */
aoqi@0 54 /* } */
aoqi@0 55 /* if (arr->length() != 0 ) { */
aoqi@0 56 /* oop bad_oop = arr->at(0)(); // Handle is BAD HERE. */
aoqi@0 57 /* ... */
aoqi@0 58 /* } */
aoqi@0 59 /* */
aoqi@0 60 /* If the GrowableArrays you are creating is C_Heap allocated then it */
aoqi@0 61 /* hould not old handles since the handles could trivially try and */
aoqi@0 62 /* outlive their HandleMark. In some situations you might need to do */
aoqi@0 63 /* this and it would be legal but be very careful and see if you can do */
aoqi@0 64 /* the code in some other manner. */
aoqi@0 65 /* */
aoqi@0 66 /*************************************************************************/
aoqi@0 67
aoqi@0 68 // To call default constructor the placement operator new() is used.
aoqi@0 69 // It should be empty (it only returns the passed void* pointer).
aoqi@0 70 // The definition of placement operator new(size_t, void*) in the <new>.
aoqi@0 71
aoqi@0 72 #include <new>
aoqi@0 73
aoqi@0 74 // Need the correct linkage to call qsort without warnings
aoqi@0 75 extern "C" {
aoqi@0 76 typedef int (*_sort_Fn)(const void *, const void *);
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 class GenericGrowableArray : public ResourceObj {
aoqi@0 80 friend class VMStructs;
aoqi@0 81
aoqi@0 82 protected:
aoqi@0 83 int _len; // current length
aoqi@0 84 int _max; // maximum length
aoqi@0 85 Arena* _arena; // Indicates where allocation occurs:
aoqi@0 86 // 0 means default ResourceArea
aoqi@0 87 // 1 means on C heap
aoqi@0 88 // otherwise, allocate in _arena
aoqi@0 89
aoqi@0 90 MEMFLAGS _memflags; // memory type if allocation in C heap
aoqi@0 91
aoqi@0 92 #ifdef ASSERT
aoqi@0 93 int _nesting; // resource area nesting at creation
aoqi@0 94 void set_nesting();
aoqi@0 95 void check_nesting();
aoqi@0 96 #else
aoqi@0 97 #define set_nesting();
aoqi@0 98 #define check_nesting();
aoqi@0 99 #endif
aoqi@0 100
aoqi@0 101 // Where are we going to allocate memory?
aoqi@0 102 bool on_C_heap() { return _arena == (Arena*)1; }
aoqi@0 103 bool on_stack () { return _arena == NULL; }
aoqi@0 104 bool on_arena () { return _arena > (Arena*)1; }
aoqi@0 105
aoqi@0 106 // This GA will use the resource stack for storage if c_heap==false,
aoqi@0 107 // Else it will use the C heap. Use clear_and_deallocate to avoid leaks.
aoqi@0 108 GenericGrowableArray(int initial_size, int initial_len, bool c_heap, MEMFLAGS flags = mtNone) {
aoqi@0 109 _len = initial_len;
aoqi@0 110 _max = initial_size;
aoqi@0 111 _memflags = flags;
aoqi@0 112
aoqi@0 113 // memory type has to be specified for C heap allocation
aoqi@0 114 assert(!(c_heap && flags == mtNone), "memory type not specified for C heap object");
aoqi@0 115
aoqi@0 116 assert(_len >= 0 && _len <= _max, "initial_len too big");
aoqi@0 117 _arena = (c_heap ? (Arena*)1 : NULL);
aoqi@0 118 set_nesting();
aoqi@0 119 assert(!on_C_heap() || allocated_on_C_heap(), "growable array must be on C heap if elements are");
aoqi@0 120 assert(!on_stack() ||
aoqi@0 121 (allocated_on_res_area() || allocated_on_stack()),
aoqi@0 122 "growable array must be on stack if elements are not on arena and not on C heap");
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 // This GA will use the given arena for storage.
aoqi@0 126 // Consider using new(arena) GrowableArray<T> to allocate the header.
aoqi@0 127 GenericGrowableArray(Arena* arena, int initial_size, int initial_len) {
aoqi@0 128 _len = initial_len;
aoqi@0 129 _max = initial_size;
aoqi@0 130 assert(_len >= 0 && _len <= _max, "initial_len too big");
aoqi@0 131 _arena = arena;
aoqi@0 132 _memflags = mtNone;
aoqi@0 133
aoqi@0 134 assert(on_arena(), "arena has taken on reserved value 0 or 1");
aoqi@0 135 // Relax next assert to allow object allocation on resource area,
aoqi@0 136 // on stack or embedded into an other object.
aoqi@0 137 assert(allocated_on_arena() || allocated_on_stack(),
aoqi@0 138 "growable array must be on arena or on stack if elements are on arena");
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 void* raw_allocate(int elementSize);
aoqi@0 142
aoqi@0 143 // some uses pass the Thread explicitly for speed (4990299 tuning)
aoqi@0 144 void* raw_allocate(Thread* thread, int elementSize) {
aoqi@0 145 assert(on_stack(), "fast ResourceObj path only");
aoqi@0 146 return (void*)resource_allocate_bytes(thread, elementSize * _max);
aoqi@0 147 }
aoqi@0 148 };
aoqi@0 149
aoqi@0 150 template<class E> class GrowableArray : public GenericGrowableArray {
aoqi@0 151 friend class VMStructs;
aoqi@0 152
aoqi@0 153 private:
aoqi@0 154 E* _data; // data array
aoqi@0 155
aoqi@0 156 void grow(int j);
aoqi@0 157 void raw_at_put_grow(int i, const E& p, const E& fill);
aoqi@0 158 void clear_and_deallocate();
aoqi@0 159 public:
aoqi@0 160 GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
aoqi@0 161 _data = (E*)raw_allocate(thread, sizeof(E));
aoqi@0 162 for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 GrowableArray(int initial_size, bool C_heap = false, MEMFLAGS F = mtInternal)
aoqi@0 166 : GenericGrowableArray(initial_size, 0, C_heap, F) {
aoqi@0 167 _data = (E*)raw_allocate(sizeof(E));
aoqi@0 168 for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false, MEMFLAGS memflags = mtInternal)
aoqi@0 172 : GenericGrowableArray(initial_size, initial_len, C_heap, memflags) {
aoqi@0 173 _data = (E*)raw_allocate(sizeof(E));
aoqi@0 174 int i = 0;
aoqi@0 175 for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
aoqi@0 176 for (; i < _max; i++) ::new ((void*)&_data[i]) E();
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 GrowableArray(Arena* arena, int initial_size, int initial_len, const E& filler) : GenericGrowableArray(arena, initial_size, initial_len) {
aoqi@0 180 _data = (E*)raw_allocate(sizeof(E));
aoqi@0 181 int i = 0;
aoqi@0 182 for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
aoqi@0 183 for (; i < _max; i++) ::new ((void*)&_data[i]) E();
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 GrowableArray() : GenericGrowableArray(2, 0, false) {
aoqi@0 187 _data = (E*)raw_allocate(sizeof(E));
aoqi@0 188 ::new ((void*)&_data[0]) E();
aoqi@0 189 ::new ((void*)&_data[1]) E();
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 // Does nothing for resource and arena objects
aoqi@0 193 ~GrowableArray() { if (on_C_heap()) clear_and_deallocate(); }
aoqi@0 194
aoqi@0 195 void clear() { _len = 0; }
aoqi@0 196 int length() const { return _len; }
aoqi@0 197 int max_length() const { return _max; }
aoqi@0 198 void trunc_to(int l) { assert(l <= _len,"cannot increase length"); _len = l; }
aoqi@0 199 bool is_empty() const { return _len == 0; }
aoqi@0 200 bool is_nonempty() const { return _len != 0; }
aoqi@0 201 bool is_full() const { return _len == _max; }
aoqi@0 202 DEBUG_ONLY(E* data_addr() const { return _data; })
aoqi@0 203
aoqi@0 204 void print();
aoqi@0 205
aoqi@0 206 int append(const E& elem) {
aoqi@0 207 check_nesting();
aoqi@0 208 if (_len == _max) grow(_len);
aoqi@0 209 int idx = _len++;
aoqi@0 210 _data[idx] = elem;
aoqi@0 211 return idx;
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 bool append_if_missing(const E& elem) {
aoqi@0 215 // Returns TRUE if elem is added.
aoqi@0 216 bool missed = !contains(elem);
aoqi@0 217 if (missed) append(elem);
aoqi@0 218 return missed;
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 E& at(int i) {
aoqi@0 222 assert(0 <= i && i < _len, "illegal index");
aoqi@0 223 return _data[i];
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 E const& at(int i) const {
aoqi@0 227 assert(0 <= i && i < _len, "illegal index");
aoqi@0 228 return _data[i];
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 E* adr_at(int i) const {
aoqi@0 232 assert(0 <= i && i < _len, "illegal index");
aoqi@0 233 return &_data[i];
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 E first() const {
aoqi@0 237 assert(_len > 0, "empty list");
aoqi@0 238 return _data[0];
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 E top() const {
aoqi@0 242 assert(_len > 0, "empty list");
aoqi@0 243 return _data[_len-1];
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 void push(const E& elem) { append(elem); }
aoqi@0 247
aoqi@0 248 E pop() {
aoqi@0 249 assert(_len > 0, "empty list");
aoqi@0 250 return _data[--_len];
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 void at_put(int i, const E& elem) {
aoqi@0 254 assert(0 <= i && i < _len, "illegal index");
aoqi@0 255 _data[i] = elem;
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 E at_grow(int i, const E& fill = E()) {
aoqi@0 259 assert(0 <= i, "negative index");
aoqi@0 260 check_nesting();
aoqi@0 261 if (i >= _len) {
aoqi@0 262 if (i >= _max) grow(i);
aoqi@0 263 for (int j = _len; j <= i; j++)
aoqi@0 264 _data[j] = fill;
aoqi@0 265 _len = i+1;
aoqi@0 266 }
aoqi@0 267 return _data[i];
aoqi@0 268 }
aoqi@0 269
aoqi@0 270 void at_put_grow(int i, const E& elem, const E& fill = E()) {
aoqi@0 271 assert(0 <= i, "negative index");
aoqi@0 272 check_nesting();
aoqi@0 273 raw_at_put_grow(i, elem, fill);
aoqi@0 274 }
aoqi@0 275
aoqi@0 276 bool contains(const E& elem) const {
aoqi@0 277 for (int i = 0; i < _len; i++) {
aoqi@0 278 if (_data[i] == elem) return true;
aoqi@0 279 }
aoqi@0 280 return false;
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 int find(const E& elem) const {
aoqi@0 284 for (int i = 0; i < _len; i++) {
aoqi@0 285 if (_data[i] == elem) return i;
aoqi@0 286 }
aoqi@0 287 return -1;
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 int find_from_end(const E& elem) const {
aoqi@0 291 for (int i = _len-1; i >= 0; i--) {
aoqi@0 292 if (_data[i] == elem) return i;
aoqi@0 293 }
aoqi@0 294 return -1;
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 int find(void* token, bool f(void*, E)) const {
aoqi@0 298 for (int i = 0; i < _len; i++) {
aoqi@0 299 if (f(token, _data[i])) return i;
aoqi@0 300 }
aoqi@0 301 return -1;
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 int find_from_end(void* token, bool f(void*, E)) const {
aoqi@0 305 // start at the end of the array
aoqi@0 306 for (int i = _len-1; i >= 0; i--) {
aoqi@0 307 if (f(token, _data[i])) return i;
aoqi@0 308 }
aoqi@0 309 return -1;
aoqi@0 310 }
aoqi@0 311
aoqi@0 312 void remove(const E& elem) {
aoqi@0 313 for (int i = 0; i < _len; i++) {
aoqi@0 314 if (_data[i] == elem) {
aoqi@0 315 for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
aoqi@0 316 _len--;
aoqi@0 317 return;
aoqi@0 318 }
aoqi@0 319 }
aoqi@0 320 ShouldNotReachHere();
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 // The order is preserved.
aoqi@0 324 void remove_at(int index) {
aoqi@0 325 assert(0 <= index && index < _len, "illegal index");
aoqi@0 326 for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
aoqi@0 327 _len--;
aoqi@0 328 }
aoqi@0 329
aoqi@0 330 // The order is changed.
aoqi@0 331 void delete_at(int index) {
aoqi@0 332 assert(0 <= index && index < _len, "illegal index");
aoqi@0 333 if (index < --_len) {
aoqi@0 334 // Replace removed element with last one.
aoqi@0 335 _data[index] = _data[_len];
aoqi@0 336 }
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 // inserts the given element before the element at index i
aoqi@0 340 void insert_before(const int idx, const E& elem) {
aoqi@0 341 check_nesting();
aoqi@0 342 if (_len == _max) grow(_len);
aoqi@0 343 for (int j = _len - 1; j >= idx; j--) {
aoqi@0 344 _data[j + 1] = _data[j];
aoqi@0 345 }
aoqi@0 346 _len++;
aoqi@0 347 _data[idx] = elem;
aoqi@0 348 }
aoqi@0 349
aoqi@0 350 void appendAll(const GrowableArray<E>* l) {
aoqi@0 351 for (int i = 0; i < l->_len; i++) {
aoqi@0 352 raw_at_put_grow(_len, l->_data[i], 0);
aoqi@0 353 }
aoqi@0 354 }
aoqi@0 355
aoqi@0 356 void sort(int f(E*,E*)) {
aoqi@0 357 qsort(_data, length(), sizeof(E), (_sort_Fn)f);
aoqi@0 358 }
aoqi@0 359 // sort by fixed-stride sub arrays:
aoqi@0 360 void sort(int f(E*,E*), int stride) {
aoqi@0 361 qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
aoqi@0 362 }
aoqi@0 363 };
aoqi@0 364
aoqi@0 365 // Global GrowableArray methods (one instance in the library per each 'E' type).
aoqi@0 366
aoqi@0 367 template<class E> void GrowableArray<E>::grow(int j) {
aoqi@0 368 // grow the array by doubling its size (amortized growth)
aoqi@0 369 int old_max = _max;
aoqi@0 370 if (_max == 0) _max = 1; // prevent endless loop
aoqi@0 371 while (j >= _max) _max = _max*2;
aoqi@0 372 // j < _max
aoqi@0 373 E* newData = (E*)raw_allocate(sizeof(E));
aoqi@0 374 int i = 0;
aoqi@0 375 for ( ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]);
aoqi@0 376 for ( ; i < _max; i++) ::new ((void*)&newData[i]) E();
aoqi@0 377 for (i = 0; i < old_max; i++) _data[i].~E();
aoqi@0 378 if (on_C_heap() && _data != NULL) {
aoqi@0 379 FreeHeap(_data);
aoqi@0 380 }
aoqi@0 381 _data = newData;
aoqi@0 382 }
aoqi@0 383
aoqi@0 384 template<class E> void GrowableArray<E>::raw_at_put_grow(int i, const E& p, const E& fill) {
aoqi@0 385 if (i >= _len) {
aoqi@0 386 if (i >= _max) grow(i);
aoqi@0 387 for (int j = _len; j < i; j++)
aoqi@0 388 _data[j] = fill;
aoqi@0 389 _len = i+1;
aoqi@0 390 }
aoqi@0 391 _data[i] = p;
aoqi@0 392 }
aoqi@0 393
aoqi@0 394 // This function clears and deallocate the data in the growable array that
aoqi@0 395 // has been allocated on the C heap. It's not public - called by the
aoqi@0 396 // destructor.
aoqi@0 397 template<class E> void GrowableArray<E>::clear_and_deallocate() {
aoqi@0 398 assert(on_C_heap(),
aoqi@0 399 "clear_and_deallocate should only be called when on C heap");
aoqi@0 400 clear();
aoqi@0 401 if (_data != NULL) {
aoqi@0 402 for (int i = 0; i < _max; i++) _data[i].~E();
aoqi@0 403 FreeHeap(_data);
aoqi@0 404 _data = NULL;
aoqi@0 405 }
aoqi@0 406 }
aoqi@0 407
aoqi@0 408 template<class E> void GrowableArray<E>::print() {
aoqi@0 409 tty->print("Growable Array " INTPTR_FORMAT, this);
aoqi@0 410 tty->print(": length %ld (_max %ld) { ", _len, _max);
aoqi@0 411 for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
aoqi@0 412 tty->print("}\n");
aoqi@0 413 }
aoqi@0 414
aoqi@0 415 #endif // SHARE_VM_UTILITIES_GROWABLEARRAY_HPP

mercurial