src/share/vm/memory/allocation.cpp

Mon, 24 Jun 2013 18:55:46 -0400

author
coleenp
date
Mon, 24 Jun 2013 18:55:46 -0400
changeset 5307
e0c9a1d29eb4
parent 5208
a1ebd310d5c1
child 5321
2b9380b0bf0b
permissions
-rw-r--r--

8016325: JVM hangs verifying system dictionary
Summary: Minimize redundant verifications of Klasses.
Reviewed-by: hseigel, jmasa

duke@435 1 /*
minqi@5103 2 * Copyright (c) 1997, 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 #include "precompiled.hpp"
stefank@2314 26 #include "memory/allocation.hpp"
stefank@2314 27 #include "memory/allocation.inline.hpp"
coleenp@4037 28 #include "memory/genCollectedHeap.hpp"
coleenp@4037 29 #include "memory/metaspaceShared.hpp"
stefank@2314 30 #include "memory/resourceArea.hpp"
coleenp@4037 31 #include "memory/universe.hpp"
zgu@3900 32 #include "runtime/atomic.hpp"
stefank@2314 33 #include "runtime/os.hpp"
stefank@2314 34 #include "runtime/task.hpp"
stefank@2314 35 #include "runtime/threadCritical.hpp"
zgu@3900 36 #include "services/memTracker.hpp"
stefank@2314 37 #include "utilities/ostream.hpp"
zgu@3900 38
stefank@2314 39 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 40 # include "os_linux.inline.hpp"
stefank@2314 41 #endif
stefank@2314 42 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 43 # include "os_solaris.inline.hpp"
stefank@2314 44 #endif
stefank@2314 45 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 46 # include "os_windows.inline.hpp"
stefank@2314 47 #endif
never@3156 48 #ifdef TARGET_OS_FAMILY_bsd
never@3156 49 # include "os_bsd.inline.hpp"
never@3156 50 #endif
duke@435 51
minqi@5103 52 void* StackObj::operator new(size_t size) { ShouldNotCallThis(); return 0; }
minqi@5103 53 void StackObj::operator delete(void* p) { ShouldNotCallThis(); }
minqi@5103 54 void* StackObj::operator new [](size_t size) { ShouldNotCallThis(); return 0; }
minqi@5103 55 void StackObj::operator delete [](void* p) { ShouldNotCallThis(); }
minqi@5103 56
minqi@5103 57 void* _ValueObj::operator new(size_t size) { ShouldNotCallThis(); return 0; }
minqi@5103 58 void _ValueObj::operator delete(void* p) { ShouldNotCallThis(); }
minqi@5103 59 void* _ValueObj::operator new [](size_t size) { ShouldNotCallThis(); return 0; }
minqi@5103 60 void _ValueObj::operator delete [](void* p) { ShouldNotCallThis(); }
duke@435 61
coleenp@4037 62 void* MetaspaceObj::operator new(size_t size, ClassLoaderData* loader_data,
iklam@5208 63 size_t word_size, bool read_only,
iklam@5208 64 MetaspaceObj::Type type, TRAPS) {
coleenp@4037 65 // Klass has it's own operator new
coleenp@4037 66 return Metaspace::allocate(loader_data, word_size, read_only,
iklam@5208 67 type, CHECK_NULL);
coleenp@4037 68 }
coleenp@4037 69
coleenp@4037 70 bool MetaspaceObj::is_shared() const {
coleenp@4037 71 return MetaspaceShared::is_in_shared_space(this);
coleenp@4037 72 }
coleenp@4037 73
coleenp@4037 74
coleenp@4295 75 bool MetaspaceObj::is_metaspace_object() const {
coleenp@4295 76 return Metaspace::contains((void*)this);
coleenp@4295 77 }
coleenp@4295 78
coleenp@4037 79 void MetaspaceObj::print_address_on(outputStream* st) const {
coleenp@4037 80 st->print(" {"INTPTR_FORMAT"}", this);
coleenp@4037 81 }
coleenp@4037 82
zgu@3900 83 void* ResourceObj::operator new(size_t size, allocation_type type, MEMFLAGS flags) {
duke@435 84 address res;
duke@435 85 switch (type) {
duke@435 86 case C_HEAP:
zgu@3900 87 res = (address)AllocateHeap(size, flags, CALLER_PC);
kvn@2040 88 DEBUG_ONLY(set_allocation_type(res, C_HEAP);)
duke@435 89 break;
duke@435 90 case RESOURCE_AREA:
kvn@2043 91 // new(size) sets allocation type RESOURCE_AREA.
duke@435 92 res = (address)operator new(size);
duke@435 93 break;
duke@435 94 default:
duke@435 95 ShouldNotReachHere();
duke@435 96 }
duke@435 97 return res;
duke@435 98 }
duke@435 99
minqi@5103 100 void* ResourceObj::operator new [](size_t size, allocation_type type, MEMFLAGS flags) {
minqi@5103 101 return (address) operator new(size, type, flags);
minqi@5103 102 }
minqi@5103 103
nloodin@4183 104 void* ResourceObj::operator new(size_t size, const std::nothrow_t& nothrow_constant,
nloodin@4183 105 allocation_type type, MEMFLAGS flags) {
nloodin@4183 106 //should only call this with std::nothrow, use other operator new() otherwise
nloodin@4183 107 address res;
nloodin@4183 108 switch (type) {
nloodin@4183 109 case C_HEAP:
nloodin@4183 110 res = (address)AllocateHeap(size, flags, CALLER_PC, AllocFailStrategy::RETURN_NULL);
nloodin@4183 111 DEBUG_ONLY(if (res!= NULL) set_allocation_type(res, C_HEAP);)
nloodin@4183 112 break;
nloodin@4183 113 case RESOURCE_AREA:
nloodin@4183 114 // new(size) sets allocation type RESOURCE_AREA.
nloodin@4183 115 res = (address)operator new(size, std::nothrow);
nloodin@4183 116 break;
nloodin@4183 117 default:
nloodin@4183 118 ShouldNotReachHere();
nloodin@4183 119 }
nloodin@4183 120 return res;
nloodin@4183 121 }
nloodin@4183 122
minqi@5103 123 void* ResourceObj::operator new [](size_t size, const std::nothrow_t& nothrow_constant,
minqi@5103 124 allocation_type type, MEMFLAGS flags) {
minqi@5103 125 return (address)operator new(size, nothrow_constant, type, flags);
minqi@5103 126 }
nloodin@4183 127
duke@435 128 void ResourceObj::operator delete(void* p) {
duke@435 129 assert(((ResourceObj *)p)->allocated_on_C_heap(),
duke@435 130 "delete only allowed for C_HEAP objects");
kvn@2357 131 DEBUG_ONLY(((ResourceObj *)p)->_allocation_t[0] = (uintptr_t)badHeapOopVal;)
duke@435 132 FreeHeap(p);
duke@435 133 }
duke@435 134
minqi@5103 135 void ResourceObj::operator delete [](void* p) {
minqi@5103 136 operator delete(p);
minqi@5103 137 }
minqi@5103 138
kvn@2040 139 #ifdef ASSERT
kvn@2040 140 void ResourceObj::set_allocation_type(address res, allocation_type type) {
kvn@2040 141 // Set allocation type in the resource object
kvn@2040 142 uintptr_t allocation = (uintptr_t)res;
kvn@2043 143 assert((allocation & allocation_mask) == 0, "address should be aligned to 4 bytes at least");
kvn@2040 144 assert(type <= allocation_mask, "incorrect allocation type");
kvn@2357 145 ResourceObj* resobj = (ResourceObj *)res;
kvn@2357 146 resobj->_allocation_t[0] = ~(allocation + type);
kvn@2357 147 if (type != STACK_OR_EMBEDDED) {
kvn@2357 148 // Called from operator new() and CollectionSetChooser(),
kvn@2357 149 // set verification value.
kvn@2357 150 resobj->_allocation_t[1] = (uintptr_t)&(resobj->_allocation_t[1]) + type;
kvn@2357 151 }
kvn@2040 152 }
kvn@2040 153
kvn@2043 154 ResourceObj::allocation_type ResourceObj::get_allocation_type() const {
kvn@2357 155 assert(~(_allocation_t[0] | allocation_mask) == (uintptr_t)this, "lost resource object");
kvn@2357 156 return (allocation_type)((~_allocation_t[0]) & allocation_mask);
kvn@2357 157 }
kvn@2357 158
kvn@2357 159 bool ResourceObj::is_type_set() const {
kvn@2357 160 allocation_type type = (allocation_type)(_allocation_t[1] & allocation_mask);
kvn@2357 161 return get_allocation_type() == type &&
kvn@2357 162 (_allocation_t[1] - type) == (uintptr_t)(&_allocation_t[1]);
kvn@2040 163 }
kvn@2040 164
kvn@2043 165 ResourceObj::ResourceObj() { // default constructor
kvn@2357 166 if (~(_allocation_t[0] | allocation_mask) != (uintptr_t)this) {
kvn@2357 167 // Operator new() is not called for allocations
kvn@2357 168 // on stack and for embedded objects.
kvn@2040 169 set_allocation_type((address)this, STACK_OR_EMBEDDED);
kvn@2357 170 } else if (allocated_on_stack()) { // STACK_OR_EMBEDDED
kvn@2357 171 // For some reason we got a value which resembles
kvn@2357 172 // an embedded or stack object (operator new() does not
kvn@2357 173 // set such type). Keep it since it is valid value
kvn@2357 174 // (even if it was garbage).
kvn@2357 175 // Ignore garbage in other fields.
kvn@2357 176 } else if (is_type_set()) {
kvn@2357 177 // Operator new() was called and type was set.
kvn@2357 178 assert(!allocated_on_stack(),
kvn@2357 179 err_msg("not embedded or stack, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",
kvn@2357 180 this, get_allocation_type(), _allocation_t[0], _allocation_t[1]));
kvn@2040 181 } else {
kvn@2357 182 // Operator new() was not called.
kvn@2357 183 // Assume that it is embedded or stack object.
kvn@2357 184 set_allocation_type((address)this, STACK_OR_EMBEDDED);
kvn@2040 185 }
kvn@2357 186 _allocation_t[1] = 0; // Zap verification value
kvn@2040 187 }
kvn@2040 188
kvn@2043 189 ResourceObj::ResourceObj(const ResourceObj& r) { // default copy constructor
kvn@2040 190 // Used in ClassFileParser::parse_constant_pool_entries() for ClassFileStream.
kvn@2357 191 // Note: garbage may resembles valid value.
kvn@2357 192 assert(~(_allocation_t[0] | allocation_mask) != (uintptr_t)this || !is_type_set(),
kvn@2357 193 err_msg("embedded or stack only, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",
kvn@2357 194 this, get_allocation_type(), _allocation_t[0], _allocation_t[1]));
kvn@2040 195 set_allocation_type((address)this, STACK_OR_EMBEDDED);
kvn@2357 196 _allocation_t[1] = 0; // Zap verification value
kvn@2040 197 }
kvn@2040 198
kvn@2040 199 ResourceObj& ResourceObj::operator=(const ResourceObj& r) { // default copy assignment
kvn@2040 200 // Used in InlineTree::ok_to_inline() for WarmCallInfo.
kvn@2357 201 assert(allocated_on_stack(),
kvn@2357 202 err_msg("copy only into local, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",
kvn@2357 203 this, get_allocation_type(), _allocation_t[0], _allocation_t[1]));
kvn@2357 204 // Keep current _allocation_t value;
kvn@2040 205 return *this;
kvn@2040 206 }
kvn@2040 207
kvn@2040 208 ResourceObj::~ResourceObj() {
kvn@2043 209 // allocated_on_C_heap() also checks that encoded (in _allocation) address == this.
kvn@2357 210 if (!allocated_on_C_heap()) { // ResourceObj::delete() will zap _allocation for C_heap.
kvn@2357 211 _allocation_t[0] = (uintptr_t)badHeapOopVal; // zap type
kvn@2040 212 }
kvn@2040 213 }
kvn@2040 214 #endif // ASSERT
kvn@2040 215
kvn@2040 216
duke@435 217 void trace_heap_malloc(size_t size, const char* name, void* p) {
duke@435 218 // A lock is not needed here - tty uses a lock internally
kvn@2557 219 tty->print_cr("Heap malloc " INTPTR_FORMAT " " SIZE_FORMAT " %s", p, size, name == NULL ? "" : name);
duke@435 220 }
duke@435 221
duke@435 222
duke@435 223 void trace_heap_free(void* p) {
duke@435 224 // A lock is not needed here - tty uses a lock internally
duke@435 225 tty->print_cr("Heap free " INTPTR_FORMAT, p);
duke@435 226 }
duke@435 227
duke@435 228 //--------------------------------------------------------------------------------------
duke@435 229 // ChunkPool implementation
duke@435 230
duke@435 231 // MT-safe pool of chunks to reduce malloc/free thrashing
duke@435 232 // NB: not using Mutex because pools are used before Threads are initialized
zgu@3900 233 class ChunkPool: public CHeapObj<mtInternal> {
duke@435 234 Chunk* _first; // first cached Chunk; its first word points to next chunk
duke@435 235 size_t _num_chunks; // number of unused chunks in pool
duke@435 236 size_t _num_used; // number of chunks currently checked out
duke@435 237 const size_t _size; // size of each chunk (must be uniform)
duke@435 238
duke@435 239 // Our three static pools
duke@435 240 static ChunkPool* _large_pool;
duke@435 241 static ChunkPool* _medium_pool;
duke@435 242 static ChunkPool* _small_pool;
duke@435 243
duke@435 244 // return first element or null
duke@435 245 void* get_first() {
duke@435 246 Chunk* c = _first;
duke@435 247 if (_first) {
duke@435 248 _first = _first->next();
duke@435 249 _num_chunks--;
duke@435 250 }
duke@435 251 return c;
duke@435 252 }
duke@435 253
duke@435 254 public:
duke@435 255 // All chunks in a ChunkPool has the same size
duke@435 256 ChunkPool(size_t size) : _size(size) { _first = NULL; _num_chunks = _num_used = 0; }
duke@435 257
duke@435 258 // Allocate a new chunk from the pool (might expand the pool)
zgu@3900 259 _NOINLINE_ void* allocate(size_t bytes) {
duke@435 260 assert(bytes == _size, "bad size");
duke@435 261 void* p = NULL;
zgu@3900 262 // No VM lock can be taken inside ThreadCritical lock, so os::malloc
zgu@3900 263 // should be done outside ThreadCritical lock due to NMT
duke@435 264 { ThreadCritical tc;
duke@435 265 _num_used++;
duke@435 266 p = get_first();
duke@435 267 }
zgu@3900 268 if (p == NULL) p = os::malloc(bytes, mtChunk, CURRENT_PC);
duke@435 269 if (p == NULL)
ccheung@4993 270 vm_exit_out_of_memory(bytes, OOM_MALLOC_ERROR, "ChunkPool::allocate");
duke@435 271
duke@435 272 return p;
duke@435 273 }
duke@435 274
duke@435 275 // Return a chunk to the pool
duke@435 276 void free(Chunk* chunk) {
duke@435 277 assert(chunk->length() + Chunk::aligned_overhead_size() == _size, "bad size");
duke@435 278 ThreadCritical tc;
duke@435 279 _num_used--;
duke@435 280
duke@435 281 // Add chunk to list
duke@435 282 chunk->set_next(_first);
duke@435 283 _first = chunk;
duke@435 284 _num_chunks++;
duke@435 285 }
duke@435 286
duke@435 287 // Prune the pool
duke@435 288 void free_all_but(size_t n) {
zgu@3900 289 Chunk* cur = NULL;
zgu@3900 290 Chunk* next;
zgu@3900 291 {
duke@435 292 // if we have more than n chunks, free all of them
duke@435 293 ThreadCritical tc;
duke@435 294 if (_num_chunks > n) {
duke@435 295 // free chunks at end of queue, for better locality
zgu@3900 296 cur = _first;
duke@435 297 for (size_t i = 0; i < (n - 1) && cur != NULL; i++) cur = cur->next();
duke@435 298
duke@435 299 if (cur != NULL) {
zgu@3900 300 next = cur->next();
duke@435 301 cur->set_next(NULL);
duke@435 302 cur = next;
duke@435 303
zgu@3900 304 _num_chunks = n;
zgu@3900 305 }
zgu@3900 306 }
zgu@3900 307 }
zgu@3900 308
zgu@3900 309 // Free all remaining chunks, outside of ThreadCritical
zgu@3900 310 // to avoid deadlock with NMT
duke@435 311 while(cur != NULL) {
duke@435 312 next = cur->next();
zgu@3900 313 os::free(cur, mtChunk);
duke@435 314 cur = next;
duke@435 315 }
duke@435 316 }
duke@435 317
duke@435 318 // Accessors to preallocated pool's
duke@435 319 static ChunkPool* large_pool() { assert(_large_pool != NULL, "must be initialized"); return _large_pool; }
duke@435 320 static ChunkPool* medium_pool() { assert(_medium_pool != NULL, "must be initialized"); return _medium_pool; }
duke@435 321 static ChunkPool* small_pool() { assert(_small_pool != NULL, "must be initialized"); return _small_pool; }
duke@435 322
duke@435 323 static void initialize() {
duke@435 324 _large_pool = new ChunkPool(Chunk::size + Chunk::aligned_overhead_size());
duke@435 325 _medium_pool = new ChunkPool(Chunk::medium_size + Chunk::aligned_overhead_size());
duke@435 326 _small_pool = new ChunkPool(Chunk::init_size + Chunk::aligned_overhead_size());
duke@435 327 }
bobv@2036 328
bobv@2036 329 static void clean() {
bobv@2036 330 enum { BlocksToKeep = 5 };
bobv@2036 331 _small_pool->free_all_but(BlocksToKeep);
bobv@2036 332 _medium_pool->free_all_but(BlocksToKeep);
bobv@2036 333 _large_pool->free_all_but(BlocksToKeep);
bobv@2036 334 }
duke@435 335 };
duke@435 336
duke@435 337 ChunkPool* ChunkPool::_large_pool = NULL;
duke@435 338 ChunkPool* ChunkPool::_medium_pool = NULL;
duke@435 339 ChunkPool* ChunkPool::_small_pool = NULL;
duke@435 340
duke@435 341 void chunkpool_init() {
duke@435 342 ChunkPool::initialize();
duke@435 343 }
duke@435 344
bobv@2036 345 void
bobv@2036 346 Chunk::clean_chunk_pool() {
bobv@2036 347 ChunkPool::clean();
bobv@2036 348 }
bobv@2036 349
duke@435 350
duke@435 351 //--------------------------------------------------------------------------------------
duke@435 352 // ChunkPoolCleaner implementation
bobv@2036 353 //
duke@435 354
duke@435 355 class ChunkPoolCleaner : public PeriodicTask {
bobv@2036 356 enum { CleaningInterval = 5000 }; // cleaning interval in ms
duke@435 357
duke@435 358 public:
duke@435 359 ChunkPoolCleaner() : PeriodicTask(CleaningInterval) {}
duke@435 360 void task() {
bobv@2036 361 ChunkPool::clean();
duke@435 362 }
duke@435 363 };
duke@435 364
duke@435 365 //--------------------------------------------------------------------------------------
duke@435 366 // Chunk implementation
duke@435 367
duke@435 368 void* Chunk::operator new(size_t requested_size, size_t length) {
duke@435 369 // requested_size is equal to sizeof(Chunk) but in order for the arena
duke@435 370 // allocations to come out aligned as expected the size must be aligned
minqi@5103 371 // to expected arena alignment.
duke@435 372 // expect requested_size but if sizeof(Chunk) doesn't match isn't proper size we must align it.
duke@435 373 assert(ARENA_ALIGN(requested_size) == aligned_overhead_size(), "Bad alignment");
duke@435 374 size_t bytes = ARENA_ALIGN(requested_size) + length;
duke@435 375 switch (length) {
duke@435 376 case Chunk::size: return ChunkPool::large_pool()->allocate(bytes);
duke@435 377 case Chunk::medium_size: return ChunkPool::medium_pool()->allocate(bytes);
duke@435 378 case Chunk::init_size: return ChunkPool::small_pool()->allocate(bytes);
duke@435 379 default: {
zgu@3900 380 void *p = os::malloc(bytes, mtChunk, CALLER_PC);
duke@435 381 if (p == NULL)
ccheung@4993 382 vm_exit_out_of_memory(bytes, OOM_MALLOC_ERROR, "Chunk::new");
duke@435 383 return p;
duke@435 384 }
duke@435 385 }
duke@435 386 }
duke@435 387
duke@435 388 void Chunk::operator delete(void* p) {
duke@435 389 Chunk* c = (Chunk*)p;
duke@435 390 switch (c->length()) {
duke@435 391 case Chunk::size: ChunkPool::large_pool()->free(c); break;
duke@435 392 case Chunk::medium_size: ChunkPool::medium_pool()->free(c); break;
duke@435 393 case Chunk::init_size: ChunkPool::small_pool()->free(c); break;
zgu@3900 394 default: os::free(c, mtChunk);
duke@435 395 }
duke@435 396 }
duke@435 397
duke@435 398 Chunk::Chunk(size_t length) : _len(length) {
duke@435 399 _next = NULL; // Chain on the linked list
duke@435 400 }
duke@435 401
duke@435 402
duke@435 403 void Chunk::chop() {
duke@435 404 Chunk *k = this;
duke@435 405 while( k ) {
duke@435 406 Chunk *tmp = k->next();
duke@435 407 // clear out this chunk (to detect allocation bugs)
duke@435 408 if (ZapResourceArea) memset(k->bottom(), badResourceValue, k->length());
duke@435 409 delete k; // Free chunk (was malloc'd)
duke@435 410 k = tmp;
duke@435 411 }
duke@435 412 }
duke@435 413
duke@435 414 void Chunk::next_chop() {
duke@435 415 _next->chop();
duke@435 416 _next = NULL;
duke@435 417 }
duke@435 418
duke@435 419
duke@435 420 void Chunk::start_chunk_pool_cleaner_task() {
duke@435 421 #ifdef ASSERT
duke@435 422 static bool task_created = false;
duke@435 423 assert(!task_created, "should not start chuck pool cleaner twice");
duke@435 424 task_created = true;
duke@435 425 #endif
duke@435 426 ChunkPoolCleaner* cleaner = new ChunkPoolCleaner();
duke@435 427 cleaner->enroll();
duke@435 428 }
duke@435 429
duke@435 430 //------------------------------Arena------------------------------------------
zgu@3900 431 NOT_PRODUCT(volatile jint Arena::_instance_count = 0;)
duke@435 432
duke@435 433 Arena::Arena(size_t init_size) {
duke@435 434 size_t round_size = (sizeof (char *)) - 1;
duke@435 435 init_size = (init_size+round_size) & ~round_size;
duke@435 436 _first = _chunk = new (init_size) Chunk(init_size);
duke@435 437 _hwm = _chunk->bottom(); // Save the cached hwm, max
duke@435 438 _max = _chunk->top();
duke@435 439 set_size_in_bytes(init_size);
zgu@3900 440 NOT_PRODUCT(Atomic::inc(&_instance_count);)
duke@435 441 }
duke@435 442
duke@435 443 Arena::Arena() {
duke@435 444 _first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size);
duke@435 445 _hwm = _chunk->bottom(); // Save the cached hwm, max
duke@435 446 _max = _chunk->top();
duke@435 447 set_size_in_bytes(Chunk::init_size);
zgu@3900 448 NOT_PRODUCT(Atomic::inc(&_instance_count);)
duke@435 449 }
duke@435 450
duke@435 451 Arena *Arena::move_contents(Arena *copy) {
duke@435 452 copy->destruct_contents();
duke@435 453 copy->_chunk = _chunk;
duke@435 454 copy->_hwm = _hwm;
duke@435 455 copy->_max = _max;
duke@435 456 copy->_first = _first;
zgu@4193 457
zgu@4193 458 // workaround rare racing condition, which could double count
zgu@4193 459 // the arena size by native memory tracking
zgu@4193 460 size_t size = size_in_bytes();
zgu@4193 461 set_size_in_bytes(0);
zgu@4193 462 copy->set_size_in_bytes(size);
duke@435 463 // Destroy original arena
duke@435 464 reset();
duke@435 465 return copy; // Return Arena with contents
duke@435 466 }
duke@435 467
duke@435 468 Arena::~Arena() {
duke@435 469 destruct_contents();
zgu@3900 470 NOT_PRODUCT(Atomic::dec(&_instance_count);)
zgu@3900 471 }
zgu@3900 472
zgu@3900 473 void* Arena::operator new(size_t size) {
zgu@3900 474 assert(false, "Use dynamic memory type binding");
zgu@3900 475 return NULL;
zgu@3900 476 }
zgu@3900 477
zgu@3900 478 void* Arena::operator new (size_t size, const std::nothrow_t& nothrow_constant) {
zgu@3900 479 assert(false, "Use dynamic memory type binding");
zgu@3900 480 return NULL;
zgu@3900 481 }
zgu@3900 482
zgu@3900 483 // dynamic memory type binding
zgu@3900 484 void* Arena::operator new(size_t size, MEMFLAGS flags) {
zgu@3900 485 #ifdef ASSERT
zgu@3900 486 void* p = (void*)AllocateHeap(size, flags|otArena, CALLER_PC);
zgu@3900 487 if (PrintMallocFree) trace_heap_malloc(size, "Arena-new", p);
zgu@3900 488 return p;
zgu@3900 489 #else
zgu@3900 490 return (void *) AllocateHeap(size, flags|otArena, CALLER_PC);
zgu@3900 491 #endif
zgu@3900 492 }
zgu@3900 493
zgu@3900 494 void* Arena::operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags) {
zgu@3900 495 #ifdef ASSERT
zgu@3900 496 void* p = os::malloc(size, flags|otArena, CALLER_PC);
zgu@3900 497 if (PrintMallocFree) trace_heap_malloc(size, "Arena-new", p);
zgu@3900 498 return p;
zgu@3900 499 #else
zgu@3900 500 return os::malloc(size, flags|otArena, CALLER_PC);
zgu@3900 501 #endif
zgu@3900 502 }
zgu@3900 503
zgu@3900 504 void Arena::operator delete(void* p) {
zgu@3900 505 FreeHeap(p);
duke@435 506 }
duke@435 507
duke@435 508 // Destroy this arenas contents and reset to empty
duke@435 509 void Arena::destruct_contents() {
duke@435 510 if (UseMallocOnly && _first != NULL) {
duke@435 511 char* end = _first->next() ? _first->top() : _hwm;
duke@435 512 free_malloced_objects(_first, _first->bottom(), end, _hwm);
duke@435 513 }
zgu@4193 514 // reset size before chop to avoid a rare racing condition
zgu@4193 515 // that can have total arena memory exceed total chunk memory
zgu@4193 516 set_size_in_bytes(0);
duke@435 517 _first->chop();
duke@435 518 reset();
duke@435 519 }
duke@435 520
zgu@3900 521 // This is high traffic method, but many calls actually don't
zgu@3900 522 // change the size
zgu@3900 523 void Arena::set_size_in_bytes(size_t size) {
zgu@3900 524 if (_size_in_bytes != size) {
zgu@3900 525 _size_in_bytes = size;
zgu@3900 526 MemTracker::record_arena_size((address)this, size);
zgu@3900 527 }
zgu@3900 528 }
duke@435 529
duke@435 530 // Total of all Chunks in arena
duke@435 531 size_t Arena::used() const {
duke@435 532 size_t sum = _chunk->length() - (_max-_hwm); // Size leftover in this Chunk
duke@435 533 register Chunk *k = _first;
duke@435 534 while( k != _chunk) { // Whilst have Chunks in a row
duke@435 535 sum += k->length(); // Total size of this Chunk
duke@435 536 k = k->next(); // Bump along to next Chunk
duke@435 537 }
duke@435 538 return sum; // Return total consumed space.
duke@435 539 }
duke@435 540
kamg@2589 541 void Arena::signal_out_of_memory(size_t sz, const char* whence) const {
ccheung@4993 542 vm_exit_out_of_memory(sz, OOM_MALLOC_ERROR, whence);
kamg@2589 543 }
duke@435 544
duke@435 545 // Grow a new Chunk
nloodin@4183 546 void* Arena::grow(size_t x, AllocFailType alloc_failmode) {
duke@435 547 // Get minimal required size. Either real big, or even bigger for giant objs
duke@435 548 size_t len = MAX2(x, (size_t) Chunk::size);
duke@435 549
duke@435 550 Chunk *k = _chunk; // Get filled-up chunk address
duke@435 551 _chunk = new (len) Chunk(len);
duke@435 552
kamg@2589 553 if (_chunk == NULL) {
nloodin@4183 554 if (alloc_failmode == AllocFailStrategy::EXIT_OOM) {
nloodin@4183 555 signal_out_of_memory(len * Chunk::aligned_overhead_size(), "Arena::grow");
nloodin@4183 556 }
nloodin@4183 557 return NULL;
kamg@2589 558 }
duke@435 559 if (k) k->set_next(_chunk); // Append new chunk to end of linked list
duke@435 560 else _first = _chunk;
duke@435 561 _hwm = _chunk->bottom(); // Save the cached hwm, max
duke@435 562 _max = _chunk->top();
duke@435 563 set_size_in_bytes(size_in_bytes() + len);
duke@435 564 void* result = _hwm;
duke@435 565 _hwm += x;
duke@435 566 return result;
duke@435 567 }
duke@435 568
duke@435 569
duke@435 570
duke@435 571 // Reallocate storage in Arena.
nloodin@4183 572 void *Arena::Arealloc(void* old_ptr, size_t old_size, size_t new_size, AllocFailType alloc_failmode) {
duke@435 573 assert(new_size >= 0, "bad size");
duke@435 574 if (new_size == 0) return NULL;
duke@435 575 #ifdef ASSERT
duke@435 576 if (UseMallocOnly) {
duke@435 577 // always allocate a new object (otherwise we'll free this one twice)
nloodin@4183 578 char* copy = (char*)Amalloc(new_size, alloc_failmode);
nloodin@4183 579 if (copy == NULL) {
nloodin@4183 580 return NULL;
nloodin@4183 581 }
duke@435 582 size_t n = MIN2(old_size, new_size);
duke@435 583 if (n > 0) memcpy(copy, old_ptr, n);
duke@435 584 Afree(old_ptr,old_size); // Mostly done to keep stats accurate
duke@435 585 return copy;
duke@435 586 }
duke@435 587 #endif
duke@435 588 char *c_old = (char*)old_ptr; // Handy name
duke@435 589 // Stupid fast special case
duke@435 590 if( new_size <= old_size ) { // Shrink in-place
duke@435 591 if( c_old+old_size == _hwm) // Attempt to free the excess bytes
duke@435 592 _hwm = c_old+new_size; // Adjust hwm
duke@435 593 return c_old;
duke@435 594 }
duke@435 595
duke@435 596 // make sure that new_size is legal
duke@435 597 size_t corrected_new_size = ARENA_ALIGN(new_size);
duke@435 598
duke@435 599 // See if we can resize in-place
duke@435 600 if( (c_old+old_size == _hwm) && // Adjusting recent thing
duke@435 601 (c_old+corrected_new_size <= _max) ) { // Still fits where it sits
duke@435 602 _hwm = c_old+corrected_new_size; // Adjust hwm
duke@435 603 return c_old; // Return old pointer
duke@435 604 }
duke@435 605
duke@435 606 // Oops, got to relocate guts
nloodin@4183 607 void *new_ptr = Amalloc(new_size, alloc_failmode);
nloodin@4183 608 if (new_ptr == NULL) {
nloodin@4183 609 return NULL;
nloodin@4183 610 }
duke@435 611 memcpy( new_ptr, c_old, old_size );
duke@435 612 Afree(c_old,old_size); // Mostly done to keep stats accurate
duke@435 613 return new_ptr;
duke@435 614 }
duke@435 615
duke@435 616
duke@435 617 // Determine if pointer belongs to this Arena or not.
duke@435 618 bool Arena::contains( const void *ptr ) const {
duke@435 619 #ifdef ASSERT
duke@435 620 if (UseMallocOnly) {
duke@435 621 // really slow, but not easy to make fast
duke@435 622 if (_chunk == NULL) return false;
duke@435 623 char** bottom = (char**)_chunk->bottom();
duke@435 624 for (char** p = (char**)_hwm - 1; p >= bottom; p--) {
duke@435 625 if (*p == ptr) return true;
duke@435 626 }
duke@435 627 for (Chunk *c = _first; c != NULL; c = c->next()) {
duke@435 628 if (c == _chunk) continue; // current chunk has been processed
duke@435 629 char** bottom = (char**)c->bottom();
duke@435 630 for (char** p = (char**)c->top() - 1; p >= bottom; p--) {
duke@435 631 if (*p == ptr) return true;
duke@435 632 }
duke@435 633 }
duke@435 634 return false;
duke@435 635 }
duke@435 636 #endif
duke@435 637 if( (void*)_chunk->bottom() <= ptr && ptr < (void*)_hwm )
duke@435 638 return true; // Check for in this chunk
duke@435 639 for (Chunk *c = _first; c; c = c->next()) {
duke@435 640 if (c == _chunk) continue; // current chunk has been processed
duke@435 641 if ((void*)c->bottom() <= ptr && ptr < (void*)c->top()) {
duke@435 642 return true; // Check for every chunk in Arena
duke@435 643 }
duke@435 644 }
duke@435 645 return false; // Not in any Chunk, so not in Arena
duke@435 646 }
duke@435 647
duke@435 648
duke@435 649 #ifdef ASSERT
duke@435 650 void* Arena::malloc(size_t size) {
duke@435 651 assert(UseMallocOnly, "shouldn't call");
duke@435 652 // use malloc, but save pointer in res. area for later freeing
duke@435 653 char** save = (char**)internal_malloc_4(sizeof(char*));
zgu@3900 654 return (*save = (char*)os::malloc(size, mtChunk));
duke@435 655 }
duke@435 656
duke@435 657 // for debugging with UseMallocOnly
duke@435 658 void* Arena::internal_malloc_4(size_t x) {
duke@435 659 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
kamg@2589 660 check_for_overflow(x, "Arena::internal_malloc_4");
duke@435 661 if (_hwm + x > _max) {
duke@435 662 return grow(x);
duke@435 663 } else {
duke@435 664 char *old = _hwm;
duke@435 665 _hwm += x;
duke@435 666 return old;
duke@435 667 }
duke@435 668 }
duke@435 669 #endif
duke@435 670
duke@435 671
duke@435 672 //--------------------------------------------------------------------------------------
duke@435 673 // Non-product code
duke@435 674
duke@435 675 #ifndef PRODUCT
duke@435 676 // The global operator new should never be called since it will usually indicate
duke@435 677 // a memory leak. Use CHeapObj as the base class of such objects to make it explicit
duke@435 678 // that they're allocated on the C heap.
duke@435 679 // Commented out in product version to avoid conflicts with third-party C++ native code.
minqi@5103 680 // On certain platforms, such as Mac OS X (Darwin), in debug version, new is being called
minqi@5103 681 // from jdk source and causing data corruption. Such as
minqi@5103 682 // Java_sun_security_ec_ECKeyPairGenerator_generateECKeyPair
minqi@5103 683 // define ALLOW_OPERATOR_NEW_USAGE for platform on which global operator new allowed.
minqi@5103 684 //
minqi@5103 685 #ifndef ALLOW_OPERATOR_NEW_USAGE
duke@435 686 void* operator new(size_t size){
minqi@5103 687 assert(false, "Should not call global operator new");
minqi@5103 688 return 0;
duke@435 689 }
minqi@5103 690
minqi@5103 691 void* operator new [](size_t size){
minqi@5103 692 assert(false, "Should not call global operator new[]");
minqi@5103 693 return 0;
minqi@5103 694 }
minqi@5103 695
minqi@5103 696 void* operator new(size_t size, const std::nothrow_t& nothrow_constant){
minqi@5103 697 assert(false, "Should not call global operator new");
minqi@5103 698 return 0;
minqi@5103 699 }
minqi@5103 700
minqi@5103 701 void* operator new [](size_t size, std::nothrow_t& nothrow_constant){
minqi@5103 702 assert(false, "Should not call global operator new[]");
minqi@5103 703 return 0;
minqi@5103 704 }
minqi@5103 705
minqi@5103 706 void operator delete(void* p) {
minqi@5103 707 assert(false, "Should not call global delete");
minqi@5103 708 }
minqi@5103 709
minqi@5103 710 void operator delete [](void* p) {
minqi@5103 711 assert(false, "Should not call global delete []");
minqi@5103 712 }
minqi@5103 713 #endif // ALLOW_OPERATOR_NEW_USAGE
duke@435 714
duke@435 715 void AllocatedObj::print() const { print_on(tty); }
duke@435 716 void AllocatedObj::print_value() const { print_value_on(tty); }
duke@435 717
duke@435 718 void AllocatedObj::print_on(outputStream* st) const {
duke@435 719 st->print_cr("AllocatedObj(" INTPTR_FORMAT ")", this);
duke@435 720 }
duke@435 721
duke@435 722 void AllocatedObj::print_value_on(outputStream* st) const {
duke@435 723 st->print("AllocatedObj(" INTPTR_FORMAT ")", this);
duke@435 724 }
duke@435 725
kvn@2557 726 julong Arena::_bytes_allocated = 0;
kvn@2557 727
kvn@2557 728 void Arena::inc_bytes_allocated(size_t x) { inc_stat_counter(&_bytes_allocated, x); }
duke@435 729
duke@435 730 AllocStats::AllocStats() {
kvn@2557 731 start_mallocs = os::num_mallocs;
kvn@2557 732 start_frees = os::num_frees;
duke@435 733 start_malloc_bytes = os::alloc_bytes;
kvn@2557 734 start_mfree_bytes = os::free_bytes;
kvn@2557 735 start_res_bytes = Arena::_bytes_allocated;
duke@435 736 }
duke@435 737
kvn@2557 738 julong AllocStats::num_mallocs() { return os::num_mallocs - start_mallocs; }
kvn@2557 739 julong AllocStats::alloc_bytes() { return os::alloc_bytes - start_malloc_bytes; }
kvn@2557 740 julong AllocStats::num_frees() { return os::num_frees - start_frees; }
kvn@2557 741 julong AllocStats::free_bytes() { return os::free_bytes - start_mfree_bytes; }
kvn@2557 742 julong AllocStats::resource_bytes() { return Arena::_bytes_allocated - start_res_bytes; }
duke@435 743 void AllocStats::print() {
kvn@2557 744 tty->print_cr(UINT64_FORMAT " mallocs (" UINT64_FORMAT "MB), "
kvn@2557 745 UINT64_FORMAT" frees (" UINT64_FORMAT "MB), " UINT64_FORMAT "MB resrc",
kvn@2557 746 num_mallocs(), alloc_bytes()/M, num_frees(), free_bytes()/M, resource_bytes()/M);
duke@435 747 }
duke@435 748
duke@435 749
duke@435 750 // debugging code
duke@435 751 inline void Arena::free_all(char** start, char** end) {
duke@435 752 for (char** p = start; p < end; p++) if (*p) os::free(*p);
duke@435 753 }
duke@435 754
duke@435 755 void Arena::free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) {
duke@435 756 assert(UseMallocOnly, "should not call");
duke@435 757 // free all objects malloced since resource mark was created; resource area
duke@435 758 // contains their addresses
duke@435 759 if (chunk->next()) {
duke@435 760 // this chunk is full, and some others too
duke@435 761 for (Chunk* c = chunk->next(); c != NULL; c = c->next()) {
duke@435 762 char* top = c->top();
duke@435 763 if (c->next() == NULL) {
duke@435 764 top = hwm2; // last junk is only used up to hwm2
duke@435 765 assert(c->contains(hwm2), "bad hwm2");
duke@435 766 }
duke@435 767 free_all((char**)c->bottom(), (char**)top);
duke@435 768 }
duke@435 769 assert(chunk->contains(hwm), "bad hwm");
duke@435 770 assert(chunk->contains(max), "bad max");
duke@435 771 free_all((char**)hwm, (char**)max);
duke@435 772 } else {
duke@435 773 // this chunk was partially used
duke@435 774 assert(chunk->contains(hwm), "bad hwm");
duke@435 775 assert(chunk->contains(hwm2), "bad hwm2");
duke@435 776 free_all((char**)hwm, (char**)hwm2);
duke@435 777 }
duke@435 778 }
duke@435 779
duke@435 780
duke@435 781 ReallocMark::ReallocMark() {
duke@435 782 #ifdef ASSERT
duke@435 783 Thread *thread = ThreadLocalStorage::get_thread_slow();
duke@435 784 _nesting = thread->resource_area()->nesting();
duke@435 785 #endif
duke@435 786 }
duke@435 787
duke@435 788 void ReallocMark::check() {
duke@435 789 #ifdef ASSERT
duke@435 790 if (_nesting != Thread::current()->resource_area()->nesting()) {
duke@435 791 fatal("allocation bug: array could grow within nested ResourceMark");
duke@435 792 }
duke@435 793 #endif
duke@435 794 }
duke@435 795
duke@435 796 #endif // Non-product

mercurial