Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/adlc/arena.cpp
32285 views
/*1* Copyright (c) 1998, 2013, 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 "adlc.hpp"2526void* Chunk::operator new(size_t requested_size, size_t length) throw() {27return CHeapObj::operator new(requested_size + length);28}2930void Chunk::operator delete(void* p, size_t length) {31CHeapObj::operator delete(p);32}3334Chunk::Chunk(size_t length) {35_next = NULL; // Chain on the linked list36_len = length; // Save actual size37}3839//------------------------------chop-------------------------------------------40void Chunk::chop() {41Chunk *k = this;42while( k ) {43Chunk *tmp = k->_next;44// clear out this chunk (to detect allocation bugs)45memset(k, 0xBAADBABE, k->_len);46free(k); // Free chunk (was malloc'd)47k = tmp;48}49}5051void Chunk::next_chop() {52_next->chop();53_next = NULL;54}5556//------------------------------Arena------------------------------------------57Arena::Arena( size_t init_size ) {58init_size = (init_size+3) & ~3;59_first = _chunk = new (init_size) Chunk(init_size);60_hwm = _chunk->bottom(); // Save the cached hwm, max61_max = _chunk->top();62set_size_in_bytes(init_size);63}6465Arena::Arena() {66_first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size);67_hwm = _chunk->bottom(); // Save the cached hwm, max68_max = _chunk->top();69set_size_in_bytes(Chunk::init_size);70}7172Arena::Arena( Arena *a )73: _chunk(a->_chunk), _hwm(a->_hwm), _max(a->_max), _first(a->_first) {74set_size_in_bytes(a->size_in_bytes());75}7677//------------------------------used-------------------------------------------78// Total of all Chunks in arena79size_t Arena::used() const {80size_t sum = _chunk->_len - (_max-_hwm); // Size leftover in this Chunk81register Chunk *k = _first;82while( k != _chunk) { // Whilst have Chunks in a row83sum += k->_len; // Total size of this Chunk84k = k->_next; // Bump along to next Chunk85}86return sum; // Return total consumed space.87}8889//------------------------------grow-------------------------------------------90// Grow a new Chunk91void* Arena::grow( size_t x ) {92// Get minimal required size. Either real big, or even bigger for giant objs93size_t len = max(x, Chunk::size);9495register Chunk *k = _chunk; // Get filled-up chunk address96_chunk = new (len) Chunk(len);9798if( k ) k->_next = _chunk; // Append new chunk to end of linked list99else _first = _chunk;100_hwm = _chunk->bottom(); // Save the cached hwm, max101_max = _chunk->top();102set_size_in_bytes(size_in_bytes() + len);103void* result = _hwm;104_hwm += x;105return result;106}107108//------------------------------calloc-----------------------------------------109// Allocate zeroed storage in Arena110void *Arena::Acalloc( size_t items, size_t x ) {111size_t z = items*x; // Total size needed112void *ptr = Amalloc(z); // Get space113memset( ptr, 0, z ); // Zap space114return ptr; // Return space115}116117//------------------------------realloc----------------------------------------118// Reallocate storage in Arena.119void *Arena::Arealloc( void *old_ptr, size_t old_size, size_t new_size ) {120char *c_old = (char*)old_ptr; // Handy name121// Stupid fast special case122if( new_size <= old_size ) { // Shrink in-place123if( c_old+old_size == _hwm) // Attempt to free the excess bytes124_hwm = c_old+new_size; // Adjust hwm125return c_old;126}127128// See if we can resize in-place129if( (c_old+old_size == _hwm) && // Adjusting recent thing130(c_old+new_size <= _max) ) { // Still fits where it sits131_hwm = c_old+new_size; // Adjust hwm132return c_old; // Return old pointer133}134135// Oops, got to relocate guts136void *new_ptr = Amalloc(new_size);137memcpy( new_ptr, c_old, old_size );138Afree(c_old,old_size); // Mostly done to keep stats accurate139return new_ptr;140}141142//------------------------------reset------------------------------------------143// Reset this Arena to empty, and return this Arenas guts in a new Arena.144Arena *Arena::reset(void) {145Arena *a = new Arena(this); // New empty arena146_first = _chunk = NULL; // Normal, new-arena initialization147_hwm = _max = NULL;148return a; // Return Arena with guts149}150151//------------------------------contains---------------------------------------152// Determine if pointer belongs to this Arena or not.153bool Arena::contains( const void *ptr ) const {154if( (void*)_chunk->bottom() <= ptr && ptr < (void*)_hwm )155return true; // Check for in this chunk156for( Chunk *c = _first; c; c = c->_next )157if( (void*)c->bottom() <= ptr && ptr < (void*)c->top())158return true; // Check for every chunk in Arena159return false; // Not in any Chunk, so not in Arena160}161162//-----------------------------------------------------------------------------163// CHeapObj164165void* CHeapObj::operator new(size_t size) throw() {166return (void *) malloc(size);167}168169void CHeapObj::operator delete(void* p){170free(p);171}172173174