Path: blob/master/src/hotspot/share/adlc/arena.cpp
41144 views
/*1* Copyright (c) 1998, 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 "adlc.hpp"2526void* AllocateHeap(size_t size) {27unsigned char* ptr = (unsigned char*) malloc(size);28if (ptr == NULL && size != 0) {29fprintf(stderr, "Error: Out of memory in ADLC\n"); // logging can cause crash!30fflush(stderr);31exit(1);32}33return ptr;34}3536void* ReAllocateHeap(void* old_ptr, size_t size) {37unsigned char* ptr = (unsigned char*) realloc(old_ptr, size);38if (ptr == NULL && size != 0) {39fprintf(stderr, "Error: Out of memory in ADLC\n"); // logging can cause crash!40fflush(stderr);41exit(1);42}43return ptr;44}4546void* Chunk::operator new(size_t requested_size, size_t length) throw() {47return CHeapObj::operator new(requested_size + length);48}4950void Chunk::operator delete(void* p, size_t length) {51CHeapObj::operator delete(p);52}5354Chunk::Chunk(size_t length) {55_next = NULL; // Chain on the linked list56_len = length; // Save actual size57}5859//------------------------------chop-------------------------------------------60void Chunk::chop() {61Chunk *k = this;62while( k ) {63Chunk *tmp = k->_next;64// clear out this chunk (to detect allocation bugs)65memset(k, 0xBE, k->_len);66free(k); // Free chunk (was malloc'd)67k = tmp;68}69}7071void Chunk::next_chop() {72_next->chop();73_next = NULL;74}7576//------------------------------Arena------------------------------------------77Arena::Arena( size_t init_size ) {78init_size = (init_size+3) & ~3;79_first = _chunk = new (init_size) Chunk(init_size);80_hwm = _chunk->bottom(); // Save the cached hwm, max81_max = _chunk->top();82set_size_in_bytes(init_size);83}8485Arena::Arena() {86_first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size);87_hwm = _chunk->bottom(); // Save the cached hwm, max88_max = _chunk->top();89set_size_in_bytes(Chunk::init_size);90}9192Arena::Arena( Arena *a )93: _chunk(a->_chunk), _hwm(a->_hwm), _max(a->_max), _first(a->_first) {94set_size_in_bytes(a->size_in_bytes());95}9697//------------------------------used-------------------------------------------98// Total of all Chunks in arena99size_t Arena::used() const {100size_t sum = _chunk->_len - (_max-_hwm); // Size leftover in this Chunk101Chunk *k = _first;102while( k != _chunk) { // Whilst have Chunks in a row103sum += k->_len; // Total size of this Chunk104k = k->_next; // Bump along to next Chunk105}106return sum; // Return total consumed space.107}108109//------------------------------grow-------------------------------------------110// Grow a new Chunk111void* Arena::grow( size_t x ) {112// Get minimal required size. Either real big, or even bigger for giant objs113size_t len = max(x, Chunk::size);114115Chunk *k = _chunk; // Get filled-up chunk address116_chunk = new (len) Chunk(len);117118if( k ) k->_next = _chunk; // Append new chunk to end of linked list119else _first = _chunk;120_hwm = _chunk->bottom(); // Save the cached hwm, max121_max = _chunk->top();122set_size_in_bytes(size_in_bytes() + len);123void* result = _hwm;124_hwm += x;125return result;126}127128//------------------------------calloc-----------------------------------------129// Allocate zeroed storage in Arena130void *Arena::Acalloc( size_t items, size_t x ) {131size_t z = items*x; // Total size needed132void *ptr = Amalloc(z); // Get space133memset( ptr, 0, z ); // Zap space134return ptr; // Return space135}136137//------------------------------realloc----------------------------------------138// Reallocate storage in Arena.139void *Arena::Arealloc( void *old_ptr, size_t old_size, size_t new_size ) {140char *c_old = (char*)old_ptr; // Handy name141// Stupid fast special case142if( new_size <= old_size ) { // Shrink in-place143if( c_old+old_size == _hwm) // Attempt to free the excess bytes144_hwm = c_old+new_size; // Adjust hwm145return c_old;146}147148// See if we can resize in-place149if( (c_old+old_size == _hwm) && // Adjusting recent thing150(c_old+new_size <= _max) ) { // Still fits where it sits151_hwm = c_old+new_size; // Adjust hwm152return c_old; // Return old pointer153}154155// Oops, got to relocate guts156void *new_ptr = Amalloc(new_size);157memcpy( new_ptr, c_old, old_size );158Afree(c_old,old_size); // Mostly done to keep stats accurate159return new_ptr;160}161162//------------------------------reset------------------------------------------163// Reset this Arena to empty, and return this Arenas guts in a new Arena.164Arena *Arena::reset(void) {165Arena *a = new Arena(this); // New empty arena166_first = _chunk = NULL; // Normal, new-arena initialization167_hwm = _max = NULL;168return a; // Return Arena with guts169}170171//------------------------------contains---------------------------------------172// Determine if pointer belongs to this Arena or not.173bool Arena::contains( const void *ptr ) const {174if( (void*)_chunk->bottom() <= ptr && ptr < (void*)_hwm )175return true; // Check for in this chunk176for( Chunk *c = _first; c; c = c->_next )177if( (void*)c->bottom() <= ptr && ptr < (void*)c->top())178return true; // Check for every chunk in Arena179return false; // Not in any Chunk, so not in Arena180}181182//-----------------------------------------------------------------------------183// CHeapObj184185void* CHeapObj::operator new(size_t size) throw() {186return (void *) AllocateHeap(size);187}188189void CHeapObj::operator delete(void* p){190free(p);191}192193194