Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/allocation.cpp
32285 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.hpp"26#include "memory/allocation.inline.hpp"27#include "memory/genCollectedHeap.hpp"28#include "memory/metaspaceShared.hpp"29#include "memory/resourceArea.hpp"30#include "memory/universe.hpp"31#include "runtime/atomic.hpp"32#include "runtime/os.hpp"33#include "runtime/task.hpp"34#include "runtime/threadCritical.hpp"35#include "services/memTracker.hpp"36#include "utilities/ostream.hpp"3738#ifdef TARGET_OS_FAMILY_linux39# include "os_linux.inline.hpp"40#endif41#ifdef TARGET_OS_FAMILY_solaris42# include "os_solaris.inline.hpp"43#endif44#ifdef TARGET_OS_FAMILY_windows45# include "os_windows.inline.hpp"46#endif47#ifdef TARGET_OS_FAMILY_aix48# include "os_aix.inline.hpp"49#endif50#ifdef TARGET_OS_FAMILY_bsd51# include "os_bsd.inline.hpp"52#endif5354void* StackObj::operator new(size_t size) throw() { ShouldNotCallThis(); return 0; }55void StackObj::operator delete(void* p) { ShouldNotCallThis(); }56void* StackObj::operator new [](size_t size) throw() { ShouldNotCallThis(); return 0; }57void StackObj::operator delete [](void* p) { ShouldNotCallThis(); }5859void* _ValueObj::operator new(size_t size) throw() { ShouldNotCallThis(); return 0; }60void _ValueObj::operator delete(void* p) { ShouldNotCallThis(); }61void* _ValueObj::operator new [](size_t size) throw() { ShouldNotCallThis(); return 0; }62void _ValueObj::operator delete [](void* p) { ShouldNotCallThis(); }6364void* MetaspaceObj::operator new(size_t size, ClassLoaderData* loader_data,65size_t word_size, bool read_only,66MetaspaceObj::Type type, TRAPS) throw() {67// Klass has it's own operator new68return Metaspace::allocate(loader_data, word_size, read_only, type, THREAD);69}7071bool MetaspaceObj::is_shared() const {72return MetaspaceShared::is_in_shared_space(this);73}7475bool MetaspaceObj::is_metaspace_object() const {76return Metaspace::contains((void*)this);77}7879void MetaspaceObj::print_address_on(outputStream* st) const {80st->print(" {" INTPTR_FORMAT "}", p2i(this));81}8283void* ResourceObj::operator new(size_t size, allocation_type type, MEMFLAGS flags) throw() {84address res = NULL;85switch (type) {86case C_HEAP:87res = (address)AllocateHeap(size, flags, CALLER_PC);88DEBUG_ONLY(set_allocation_type(res, C_HEAP);)89break;90case RESOURCE_AREA:91// new(size) sets allocation type RESOURCE_AREA.92res = (address)operator new(size);93break;94default:95ShouldNotReachHere();96}97return res;98}99100void* ResourceObj::operator new [](size_t size, allocation_type type, MEMFLAGS flags) throw() {101return (address) operator new(size, type, flags);102}103104void* ResourceObj::operator new(size_t size, const std::nothrow_t& nothrow_constant,105allocation_type type, MEMFLAGS flags) throw() {106// should only call this with std::nothrow, use other operator new() otherwise107address res = NULL;108switch (type) {109case C_HEAP:110res = (address)AllocateHeap(size, flags, CALLER_PC, AllocFailStrategy::RETURN_NULL);111DEBUG_ONLY(if (res!= NULL) set_allocation_type(res, C_HEAP);)112break;113case RESOURCE_AREA:114// new(size) sets allocation type RESOURCE_AREA.115res = (address)operator new(size, std::nothrow);116break;117default:118ShouldNotReachHere();119}120return res;121}122123void* ResourceObj::operator new [](size_t size, const std::nothrow_t& nothrow_constant,124allocation_type type, MEMFLAGS flags) throw() {125return (address)operator new(size, nothrow_constant, type, flags);126}127128void ResourceObj::operator delete(void* p) {129assert(((ResourceObj *)p)->allocated_on_C_heap(),130"delete only allowed for C_HEAP objects");131DEBUG_ONLY(((ResourceObj *)p)->_allocation_t[0] = (uintptr_t)badHeapOopVal;)132FreeHeap(p);133}134135void ResourceObj::operator delete [](void* p) {136operator delete(p);137}138139#ifdef ASSERT140void ResourceObj::set_allocation_type(address res, allocation_type type) {141// Set allocation type in the resource object142uintptr_t allocation = (uintptr_t)res;143assert((allocation & allocation_mask) == 0, err_msg("address should be aligned to 4 bytes at least: " INTPTR_FORMAT, p2i(res)));144assert(type <= allocation_mask, "incorrect allocation type");145ResourceObj* resobj = (ResourceObj *)res;146resobj->_allocation_t[0] = ~(allocation + type);147if (type != STACK_OR_EMBEDDED) {148// Called from operator new() and CollectionSetChooser(),149// set verification value.150resobj->_allocation_t[1] = (uintptr_t)&(resobj->_allocation_t[1]) + type;151}152}153154ResourceObj::allocation_type ResourceObj::get_allocation_type() const {155assert(~(_allocation_t[0] | allocation_mask) == (uintptr_t)this, "lost resource object");156return (allocation_type)((~_allocation_t[0]) & allocation_mask);157}158159bool ResourceObj::is_type_set() const {160allocation_type type = (allocation_type)(_allocation_t[1] & allocation_mask);161return get_allocation_type() == type &&162(_allocation_t[1] - type) == (uintptr_t)(&_allocation_t[1]);163}164165ResourceObj::ResourceObj() { // default constructor166if (~(_allocation_t[0] | allocation_mask) != (uintptr_t)this) {167// Operator new() is not called for allocations168// on stack and for embedded objects.169set_allocation_type((address)this, STACK_OR_EMBEDDED);170} else if (allocated_on_stack()) { // STACK_OR_EMBEDDED171// For some reason we got a value which resembles172// an embedded or stack object (operator new() does not173// set such type). Keep it since it is valid value174// (even if it was garbage).175// Ignore garbage in other fields.176} else if (is_type_set()) {177// Operator new() was called and type was set.178assert(!allocated_on_stack(),179err_msg("not embedded or stack, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",180p2i(this), get_allocation_type(), _allocation_t[0], _allocation_t[1]));181} else {182// Operator new() was not called.183// Assume that it is embedded or stack object.184set_allocation_type((address)this, STACK_OR_EMBEDDED);185}186_allocation_t[1] = 0; // Zap verification value187}188189ResourceObj::ResourceObj(const ResourceObj& r) { // default copy constructor190// Used in ClassFileParser::parse_constant_pool_entries() for ClassFileStream.191// Note: garbage may resembles valid value.192assert(~(_allocation_t[0] | allocation_mask) != (uintptr_t)this || !is_type_set(),193err_msg("embedded or stack only, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",194p2i(this), get_allocation_type(), _allocation_t[0], _allocation_t[1]));195set_allocation_type((address)this, STACK_OR_EMBEDDED);196_allocation_t[1] = 0; // Zap verification value197}198199ResourceObj& ResourceObj::operator=(const ResourceObj& r) { // default copy assignment200// Used in InlineTree::ok_to_inline() for WarmCallInfo.201assert(allocated_on_stack(),202err_msg("copy only into local, this(" PTR_FORMAT ") type %d a[0]=(" PTR_FORMAT ") a[1]=(" PTR_FORMAT ")",203p2i(this), get_allocation_type(), _allocation_t[0], _allocation_t[1]));204// Keep current _allocation_t value;205return *this;206}207208ResourceObj::~ResourceObj() {209// allocated_on_C_heap() also checks that encoded (in _allocation) address == this.210if (!allocated_on_C_heap()) { // ResourceObj::delete() will zap _allocation for C_heap.211_allocation_t[0] = (uintptr_t)badHeapOopVal; // zap type212}213}214#endif // ASSERT215216217void trace_heap_malloc(size_t size, const char* name, void* p) {218// A lock is not needed here - tty uses a lock internally219tty->print_cr("Heap malloc " INTPTR_FORMAT " " SIZE_FORMAT " %s", p2i(p), size, name == NULL ? "" : name);220}221222223void trace_heap_free(void* p) {224// A lock is not needed here - tty uses a lock internally225tty->print_cr("Heap free " INTPTR_FORMAT, p2i(p));226}227228//--------------------------------------------------------------------------------------229// ChunkPool implementation230231// MT-safe pool of chunks to reduce malloc/free thrashing232// NB: not using Mutex because pools are used before Threads are initialized233class ChunkPool: public CHeapObj<mtInternal> {234Chunk* _first; // first cached Chunk; its first word points to next chunk235size_t _num_chunks; // number of unused chunks in pool236size_t _num_used; // number of chunks currently checked out237const size_t _size; // size of each chunk (must be uniform)238239// Our four static pools240static ChunkPool* _large_pool;241static ChunkPool* _medium_pool;242static ChunkPool* _small_pool;243static ChunkPool* _tiny_pool;244245// return first element or null246void* get_first() {247Chunk* c = _first;248if (_first) {249_first = _first->next();250_num_chunks--;251}252return c;253}254255public:256// All chunks in a ChunkPool has the same size257ChunkPool(size_t size) : _size(size) { _first = NULL; _num_chunks = _num_used = 0; }258259// Allocate a new chunk from the pool (might expand the pool)260_NOINLINE_ void* allocate(size_t bytes, AllocFailType alloc_failmode) {261assert(bytes == _size, "bad size");262void* p = NULL;263// No VM lock can be taken inside ThreadCritical lock, so os::malloc264// should be done outside ThreadCritical lock due to NMT265{ ThreadCritical tc;266_num_used++;267p = get_first();268}269if (p == NULL) p = os::malloc(bytes, mtChunk, CURRENT_PC);270if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {271vm_exit_out_of_memory(bytes, OOM_MALLOC_ERROR, "ChunkPool::allocate");272}273return p;274}275276// Return a chunk to the pool277void free(Chunk* chunk) {278assert(chunk->length() + Chunk::aligned_overhead_size() == _size, "bad size");279ThreadCritical tc;280_num_used--;281282// Add chunk to list283chunk->set_next(_first);284_first = chunk;285_num_chunks++;286}287288// Prune the pool289void free_all_but(size_t n) {290Chunk* cur = NULL;291Chunk* next;292{293// if we have more than n chunks, free all of them294ThreadCritical tc;295if (_num_chunks > n) {296// free chunks at end of queue, for better locality297cur = _first;298for (size_t i = 0; i < (n - 1) && cur != NULL; i++) cur = cur->next();299300if (cur != NULL) {301next = cur->next();302cur->set_next(NULL);303cur = next;304305_num_chunks = n;306}307}308}309310// Free all remaining chunks, outside of ThreadCritical311// to avoid deadlock with NMT312while(cur != NULL) {313next = cur->next();314os::free(cur, mtChunk);315cur = next;316}317}318319// Accessors to preallocated pool's320static ChunkPool* large_pool() { assert(_large_pool != NULL, "must be initialized"); return _large_pool; }321static ChunkPool* medium_pool() { assert(_medium_pool != NULL, "must be initialized"); return _medium_pool; }322static ChunkPool* small_pool() { assert(_small_pool != NULL, "must be initialized"); return _small_pool; }323static ChunkPool* tiny_pool() { assert(_tiny_pool != NULL, "must be initialized"); return _tiny_pool; }324325static void initialize() {326_large_pool = new ChunkPool(Chunk::size + Chunk::aligned_overhead_size());327_medium_pool = new ChunkPool(Chunk::medium_size + Chunk::aligned_overhead_size());328_small_pool = new ChunkPool(Chunk::init_size + Chunk::aligned_overhead_size());329_tiny_pool = new ChunkPool(Chunk::tiny_size + Chunk::aligned_overhead_size());330}331332static void clean() {333enum { BlocksToKeep = 5 };334_tiny_pool->free_all_but(BlocksToKeep);335_small_pool->free_all_but(BlocksToKeep);336_medium_pool->free_all_but(BlocksToKeep);337_large_pool->free_all_but(BlocksToKeep);338}339};340341ChunkPool* ChunkPool::_large_pool = NULL;342ChunkPool* ChunkPool::_medium_pool = NULL;343ChunkPool* ChunkPool::_small_pool = NULL;344ChunkPool* ChunkPool::_tiny_pool = NULL;345346void chunkpool_init() {347ChunkPool::initialize();348}349350void351Chunk::clean_chunk_pool() {352ChunkPool::clean();353}354355356//--------------------------------------------------------------------------------------357// ChunkPoolCleaner implementation358//359360class ChunkPoolCleaner : public PeriodicTask {361enum { CleaningInterval = 5000 }; // cleaning interval in ms362363public:364ChunkPoolCleaner() : PeriodicTask(CleaningInterval) {}365void task() {366ChunkPool::clean();367}368};369370//--------------------------------------------------------------------------------------371// Chunk implementation372373void* Chunk::operator new (size_t requested_size, AllocFailType alloc_failmode, size_t length) throw() {374// requested_size is equal to sizeof(Chunk) but in order for the arena375// allocations to come out aligned as expected the size must be aligned376// to expected arena alignment.377// expect requested_size but if sizeof(Chunk) doesn't match isn't proper size we must align it.378assert(ARENA_ALIGN(requested_size) == aligned_overhead_size(), "Bad alignment");379size_t bytes = ARENA_ALIGN(requested_size) + length;380switch (length) {381case Chunk::size: return ChunkPool::large_pool()->allocate(bytes, alloc_failmode);382case Chunk::medium_size: return ChunkPool::medium_pool()->allocate(bytes, alloc_failmode);383case Chunk::init_size: return ChunkPool::small_pool()->allocate(bytes, alloc_failmode);384case Chunk::tiny_size: return ChunkPool::tiny_pool()->allocate(bytes, alloc_failmode);385default: {386void* p = os::malloc(bytes, mtChunk, CALLER_PC);387if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {388vm_exit_out_of_memory(bytes, OOM_MALLOC_ERROR, "Chunk::new");389}390return p;391}392}393}394395void Chunk::operator delete(void* p) {396Chunk* c = (Chunk*)p;397switch (c->length()) {398case Chunk::size: ChunkPool::large_pool()->free(c); break;399case Chunk::medium_size: ChunkPool::medium_pool()->free(c); break;400case Chunk::init_size: ChunkPool::small_pool()->free(c); break;401case Chunk::tiny_size: ChunkPool::tiny_pool()->free(c); break;402default: os::free(c, mtChunk);403}404}405406Chunk::Chunk(size_t length) : _len(length) {407_next = NULL; // Chain on the linked list408}409410411void Chunk::chop() {412Chunk *k = this;413while( k ) {414Chunk *tmp = k->next();415// clear out this chunk (to detect allocation bugs)416if (ZapResourceArea) memset(k->bottom(), badResourceValue, k->length());417delete k; // Free chunk (was malloc'd)418k = tmp;419}420}421422void Chunk::next_chop() {423_next->chop();424_next = NULL;425}426427428void Chunk::start_chunk_pool_cleaner_task() {429#ifdef ASSERT430static bool task_created = false;431assert(!task_created, "should not start chuck pool cleaner twice");432task_created = true;433#endif434ChunkPoolCleaner* cleaner = new ChunkPoolCleaner();435cleaner->enroll();436}437438//------------------------------Arena------------------------------------------439Arena::Arena(MEMFLAGS flag, size_t init_size) : _flags(flag), _size_in_bytes(0) {440size_t round_size = (sizeof (char *)) - 1;441init_size = (init_size+round_size) & ~round_size;442_first = _chunk = new (AllocFailStrategy::EXIT_OOM, init_size) Chunk(init_size);443_hwm = _chunk->bottom(); // Save the cached hwm, max444_max = _chunk->top();445MemTracker::record_new_arena(flag);446set_size_in_bytes(init_size);447}448449Arena::Arena(MEMFLAGS flag) : _flags(flag), _size_in_bytes(0) {450_first = _chunk = new (AllocFailStrategy::EXIT_OOM, Chunk::init_size) Chunk(Chunk::init_size);451_hwm = _chunk->bottom(); // Save the cached hwm, max452_max = _chunk->top();453MemTracker::record_new_arena(flag);454set_size_in_bytes(Chunk::init_size);455}456457Arena *Arena::move_contents(Arena *copy) {458copy->destruct_contents();459copy->_chunk = _chunk;460copy->_hwm = _hwm;461copy->_max = _max;462copy->_first = _first;463464// workaround rare racing condition, which could double count465// the arena size by native memory tracking466size_t size = size_in_bytes();467set_size_in_bytes(0);468copy->set_size_in_bytes(size);469// Destroy original arena470reset();471return copy; // Return Arena with contents472}473474Arena::~Arena() {475destruct_contents();476MemTracker::record_arena_free(_flags);477}478479void* Arena::operator new(size_t size) throw() {480assert(false, "Use dynamic memory type binding");481return NULL;482}483484void* Arena::operator new (size_t size, const std::nothrow_t& nothrow_constant) throw() {485assert(false, "Use dynamic memory type binding");486return NULL;487}488489// dynamic memory type binding490void* Arena::operator new(size_t size, MEMFLAGS flags) throw() {491#ifdef ASSERT492void* p = (void*)AllocateHeap(size, flags, CALLER_PC);493if (PrintMallocFree) trace_heap_malloc(size, "Arena-new", p);494return p;495#else496return (void *) AllocateHeap(size, flags, CALLER_PC);497#endif498}499500void* Arena::operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags) throw() {501#ifdef ASSERT502void* p = os::malloc(size, flags, CALLER_PC);503if (PrintMallocFree) trace_heap_malloc(size, "Arena-new", p);504return p;505#else506return os::malloc(size, flags, CALLER_PC);507#endif508}509510void Arena::operator delete(void* p) {511FreeHeap(p);512}513514// Destroy this arenas contents and reset to empty515void Arena::destruct_contents() {516if (UseMallocOnly && _first != NULL) {517char* end = _first->next() ? _first->top() : _hwm;518free_malloced_objects(_first, _first->bottom(), end, _hwm);519}520// reset size before chop to avoid a rare racing condition521// that can have total arena memory exceed total chunk memory522set_size_in_bytes(0);523_first->chop();524reset();525}526527// This is high traffic method, but many calls actually don't528// change the size529void Arena::set_size_in_bytes(size_t size) {530if (_size_in_bytes != size) {531ssize_t delta = size - size_in_bytes();532_size_in_bytes = size;533MemTracker::record_arena_size_change(delta, _flags);534}535}536537// Total of all Chunks in arena538size_t Arena::used() const {539size_t sum = _chunk->length() - (_max-_hwm); // Size leftover in this Chunk540register Chunk *k = _first;541while( k != _chunk) { // Whilst have Chunks in a row542sum += k->length(); // Total size of this Chunk543k = k->next(); // Bump along to next Chunk544}545return sum; // Return total consumed space.546}547548void Arena::signal_out_of_memory(size_t sz, const char* whence) const {549vm_exit_out_of_memory(sz, OOM_MALLOC_ERROR, whence);550}551552// Grow a new Chunk553void* Arena::grow(size_t x, AllocFailType alloc_failmode) {554// Get minimal required size. Either real big, or even bigger for giant objs555size_t len = MAX2(x, (size_t) Chunk::size);556557Chunk *k = _chunk; // Get filled-up chunk address558_chunk = new (alloc_failmode, len) Chunk(len);559560if (_chunk == NULL) {561_chunk = k; // restore the previous value of _chunk562return NULL;563}564if (k) k->set_next(_chunk); // Append new chunk to end of linked list565else _first = _chunk;566_hwm = _chunk->bottom(); // Save the cached hwm, max567_max = _chunk->top();568set_size_in_bytes(size_in_bytes() + len);569void* result = _hwm;570_hwm += x;571return result;572}573574575576// Reallocate storage in Arena.577void *Arena::Arealloc(void* old_ptr, size_t old_size, size_t new_size, AllocFailType alloc_failmode) {578assert(new_size >= 0, "bad size");579if (new_size == 0) return NULL;580#ifdef ASSERT581if (UseMallocOnly) {582// always allocate a new object (otherwise we'll free this one twice)583char* copy = (char*)Amalloc(new_size, alloc_failmode);584if (copy == NULL) {585return NULL;586}587size_t n = MIN2(old_size, new_size);588if (n > 0) memcpy(copy, old_ptr, n);589Afree(old_ptr,old_size); // Mostly done to keep stats accurate590return copy;591}592#endif593char *c_old = (char*)old_ptr; // Handy name594// Stupid fast special case595if( new_size <= old_size ) { // Shrink in-place596if( c_old+old_size == _hwm) // Attempt to free the excess bytes597_hwm = c_old+new_size; // Adjust hwm598return c_old;599}600601// make sure that new_size is legal602size_t corrected_new_size = ARENA_ALIGN(new_size);603604// See if we can resize in-place605if( (c_old+old_size == _hwm) && // Adjusting recent thing606(c_old+corrected_new_size <= _max) ) { // Still fits where it sits607_hwm = c_old+corrected_new_size; // Adjust hwm608return c_old; // Return old pointer609}610611// Oops, got to relocate guts612void *new_ptr = Amalloc(new_size, alloc_failmode);613if (new_ptr == NULL) {614return NULL;615}616memcpy( new_ptr, c_old, old_size );617Afree(c_old,old_size); // Mostly done to keep stats accurate618return new_ptr;619}620621622// Determine if pointer belongs to this Arena or not.623bool Arena::contains( const void *ptr ) const {624#ifdef ASSERT625if (UseMallocOnly) {626// really slow, but not easy to make fast627if (_chunk == NULL) return false;628char** bottom = (char**)_chunk->bottom();629for (char** p = (char**)_hwm - 1; p >= bottom; p--) {630if (*p == ptr) return true;631}632for (Chunk *c = _first; c != NULL; c = c->next()) {633if (c == _chunk) continue; // current chunk has been processed634char** bottom = (char**)c->bottom();635for (char** p = (char**)c->top() - 1; p >= bottom; p--) {636if (*p == ptr) return true;637}638}639return false;640}641#endif642if( (void*)_chunk->bottom() <= ptr && ptr < (void*)_hwm )643return true; // Check for in this chunk644for (Chunk *c = _first; c; c = c->next()) {645if (c == _chunk) continue; // current chunk has been processed646if ((void*)c->bottom() <= ptr && ptr < (void*)c->top()) {647return true; // Check for every chunk in Arena648}649}650return false; // Not in any Chunk, so not in Arena651}652653654#ifdef ASSERT655void* Arena::malloc(size_t size) {656assert(UseMallocOnly, "shouldn't call");657// use malloc, but save pointer in res. area for later freeing658char** save = (char**)internal_malloc_4(sizeof(char*));659return (*save = (char*)os::malloc(size, mtChunk));660}661662// for debugging with UseMallocOnly663void* Arena::internal_malloc_4(size_t x) {664assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );665check_for_overflow(x, "Arena::internal_malloc_4");666if (_hwm + x > _max) {667return grow(x);668} else {669char *old = _hwm;670_hwm += x;671return old;672}673}674#endif675676677//--------------------------------------------------------------------------------------678// Non-product code679680#ifndef PRODUCT681// The global operator new should never be called since it will usually indicate682// a memory leak. Use CHeapObj as the base class of such objects to make it explicit683// that they're allocated on the C heap.684// Commented out in product version to avoid conflicts with third-party C++ native code.685// On certain platforms, such as Mac OS X (Darwin), in debug version, new is being called686// from jdk source and causing data corruption. Such as687// Java_sun_security_ec_ECKeyPairGenerator_generateECKeyPair688// define ALLOW_OPERATOR_NEW_USAGE for platform on which global operator new allowed.689//690#ifndef ALLOW_OPERATOR_NEW_USAGE691void* operator new(size_t size) throw() {692assert(false, "Should not call global operator new");693return 0;694}695696void* operator new [](size_t size) throw() {697assert(false, "Should not call global operator new[]");698return 0;699}700701void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {702assert(false, "Should not call global operator new");703return 0;704}705706void* operator new [](size_t size, std::nothrow_t& nothrow_constant) throw() {707assert(false, "Should not call global operator new[]");708return 0;709}710711void operator delete(void* p) {712assert(false, "Should not call global delete");713}714715void operator delete [](void* p) {716assert(false, "Should not call global delete []");717}718#endif // ALLOW_OPERATOR_NEW_USAGE719720void AllocatedObj::print() const { print_on(tty); }721void AllocatedObj::print_value() const { print_value_on(tty); }722723void AllocatedObj::print_on(outputStream* st) const {724st->print_cr("AllocatedObj(" INTPTR_FORMAT ")", p2i(this));725}726727void AllocatedObj::print_value_on(outputStream* st) const {728st->print("AllocatedObj(" INTPTR_FORMAT ")", p2i(this));729}730731julong Arena::_bytes_allocated = 0;732733void Arena::inc_bytes_allocated(size_t x) { inc_stat_counter(&_bytes_allocated, x); }734735AllocStats::AllocStats() {736start_mallocs = os::num_mallocs;737start_frees = os::num_frees;738start_malloc_bytes = os::alloc_bytes;739start_mfree_bytes = os::free_bytes;740start_res_bytes = Arena::_bytes_allocated;741}742743julong AllocStats::num_mallocs() { return os::num_mallocs - start_mallocs; }744julong AllocStats::alloc_bytes() { return os::alloc_bytes - start_malloc_bytes; }745julong AllocStats::num_frees() { return os::num_frees - start_frees; }746julong AllocStats::free_bytes() { return os::free_bytes - start_mfree_bytes; }747julong AllocStats::resource_bytes() { return Arena::_bytes_allocated - start_res_bytes; }748void AllocStats::print() {749tty->print_cr(UINT64_FORMAT " mallocs (" UINT64_FORMAT "MB), "750UINT64_FORMAT " frees (" UINT64_FORMAT "MB), " UINT64_FORMAT "MB resrc",751num_mallocs(), alloc_bytes()/M, num_frees(), free_bytes()/M, resource_bytes()/M);752}753754755// debugging code756inline void Arena::free_all(char** start, char** end) {757for (char** p = start; p < end; p++) if (*p) os::free(*p);758}759760void Arena::free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2) {761assert(UseMallocOnly, "should not call");762// free all objects malloced since resource mark was created; resource area763// contains their addresses764if (chunk->next()) {765// this chunk is full, and some others too766for (Chunk* c = chunk->next(); c != NULL; c = c->next()) {767char* top = c->top();768if (c->next() == NULL) {769top = hwm2; // last junk is only used up to hwm2770assert(c->contains(hwm2), "bad hwm2");771}772free_all((char**)c->bottom(), (char**)top);773}774assert(chunk->contains(hwm), "bad hwm");775assert(chunk->contains(max), "bad max");776free_all((char**)hwm, (char**)max);777} else {778// this chunk was partially used779assert(chunk->contains(hwm), "bad hwm");780assert(chunk->contains(hwm2), "bad hwm2");781free_all((char**)hwm, (char**)hwm2);782}783}784785786ReallocMark::ReallocMark() {787#ifdef ASSERT788Thread *thread = ThreadLocalStorage::get_thread_slow();789_nesting = thread->resource_area()->nesting();790#endif791}792793void ReallocMark::check() {794#ifdef ASSERT795if (_nesting != Thread::current()->resource_area()->nesting()) {796fatal("allocation bug: array could grow within nested ResourceMark");797}798#endif799}800801#endif // Non-product802803804