src/share/vm/memory/allocation.cpp

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

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6695
09619752c16d
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial