duke@435: /* duke@435: * Copyright 1998-2002 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: #include "adlc.hpp" duke@435: duke@435: void* Chunk::operator new(size_t requested_size, size_t length) { duke@435: return CHeapObj::operator new(requested_size + length); duke@435: } duke@435: duke@435: void Chunk::operator delete(void* p, size_t length) { duke@435: CHeapObj::operator delete(p); duke@435: } duke@435: duke@435: Chunk::Chunk(size_t length) { duke@435: _next = NULL; // Chain on the linked list duke@435: _len = length; // Save actual size duke@435: } duke@435: duke@435: //------------------------------chop------------------------------------------- duke@435: void Chunk::chop() { duke@435: Chunk *k = this; duke@435: while( k ) { duke@435: Chunk *tmp = k->_next; duke@435: // clear out this chunk (to detect allocation bugs) duke@435: memset(k, 0xBAADBABE, k->_len); duke@435: free(k); // Free chunk (was malloc'd) duke@435: k = tmp; duke@435: } duke@435: } duke@435: duke@435: void Chunk::next_chop() { duke@435: _next->chop(); duke@435: _next = NULL; duke@435: } duke@435: duke@435: //------------------------------Arena------------------------------------------ duke@435: Arena::Arena( size_t init_size ) { duke@435: init_size = (init_size+3) & ~3; duke@435: _first = _chunk = new (init_size) Chunk(init_size); duke@435: _hwm = _chunk->bottom(); // Save the cached hwm, max duke@435: _max = _chunk->top(); duke@435: set_size_in_bytes(init_size); duke@435: } duke@435: duke@435: Arena::Arena() { duke@435: _first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size); duke@435: _hwm = _chunk->bottom(); // Save the cached hwm, max duke@435: _max = _chunk->top(); duke@435: set_size_in_bytes(Chunk::init_size); duke@435: } duke@435: duke@435: Arena::Arena( Arena *a ) duke@435: : _chunk(a->_chunk), _hwm(a->_hwm), _max(a->_max), _first(a->_first) { duke@435: set_size_in_bytes(a->size_in_bytes()); duke@435: } duke@435: duke@435: //------------------------------used------------------------------------------- duke@435: // Total of all Chunks in arena duke@435: size_t Arena::used() const { duke@435: size_t sum = _chunk->_len - (_max-_hwm); // Size leftover in this Chunk duke@435: register Chunk *k = _first; duke@435: while( k != _chunk) { // Whilst have Chunks in a row duke@435: sum += k->_len; // Total size of this Chunk duke@435: k = k->_next; // Bump along to next Chunk duke@435: } duke@435: return sum; // Return total consumed space. duke@435: } duke@435: duke@435: //------------------------------grow------------------------------------------- duke@435: // Grow a new Chunk duke@435: void* Arena::grow( size_t x ) { duke@435: // Get minimal required size. Either real big, or even bigger for giant objs duke@435: size_t len = max(x, Chunk::size); duke@435: duke@435: register Chunk *k = _chunk; // Get filled-up chunk address duke@435: _chunk = new (len) Chunk(len); duke@435: duke@435: if( k ) k->_next = _chunk; // Append new chunk to end of linked list duke@435: else _first = _chunk; duke@435: _hwm = _chunk->bottom(); // Save the cached hwm, max duke@435: _max = _chunk->top(); duke@435: set_size_in_bytes(size_in_bytes() + len); duke@435: void* result = _hwm; duke@435: _hwm += x; duke@435: return result; duke@435: } duke@435: duke@435: //------------------------------calloc----------------------------------------- duke@435: // Allocate zeroed storage in Arena duke@435: void *Arena::Acalloc( size_t items, size_t x ) { duke@435: size_t z = items*x; // Total size needed duke@435: void *ptr = Amalloc(z); // Get space duke@435: memset( ptr, 0, z ); // Zap space duke@435: return ptr; // Return space duke@435: } duke@435: duke@435: //------------------------------realloc---------------------------------------- duke@435: // Reallocate storage in Arena. duke@435: void *Arena::Arealloc( void *old_ptr, size_t old_size, size_t new_size ) { duke@435: char *c_old = (char*)old_ptr; // Handy name duke@435: // Stupid fast special case duke@435: if( new_size <= old_size ) { // Shrink in-place duke@435: if( c_old+old_size == _hwm) // Attempt to free the excess bytes duke@435: _hwm = c_old+new_size; // Adjust hwm duke@435: return c_old; duke@435: } duke@435: duke@435: // See if we can resize in-place duke@435: if( (c_old+old_size == _hwm) && // Adjusting recent thing duke@435: (c_old+new_size <= _max) ) { // Still fits where it sits duke@435: _hwm = c_old+new_size; // Adjust hwm duke@435: return c_old; // Return old pointer duke@435: } duke@435: duke@435: // Oops, got to relocate guts duke@435: void *new_ptr = Amalloc(new_size); duke@435: memcpy( new_ptr, c_old, old_size ); duke@435: Afree(c_old,old_size); // Mostly done to keep stats accurate duke@435: return new_ptr; duke@435: } duke@435: duke@435: //------------------------------reset------------------------------------------ duke@435: // Reset this Arena to empty, and return this Arenas guts in a new Arena. duke@435: Arena *Arena::reset(void) { duke@435: Arena *a = new Arena(this); // New empty arena duke@435: _first = _chunk = NULL; // Normal, new-arena initialization duke@435: _hwm = _max = NULL; duke@435: return a; // Return Arena with guts duke@435: } duke@435: duke@435: //------------------------------contains--------------------------------------- duke@435: // Determine if pointer belongs to this Arena or not. duke@435: bool Arena::contains( const void *ptr ) const { duke@435: if( (void*)_chunk->bottom() <= ptr && ptr < (void*)_hwm ) duke@435: return true; // Check for in this chunk duke@435: for( Chunk *c = _first; c; c = c->_next ) duke@435: if( (void*)c->bottom() <= ptr && ptr < (void*)c->top()) duke@435: return true; // Check for every chunk in Arena duke@435: return false; // Not in any Chunk, so not in Arena duke@435: } duke@435: duke@435: //----------------------------------------------------------------------------- duke@435: // CHeapObj duke@435: duke@435: void* CHeapObj::operator new(size_t size){ duke@435: return (void *) malloc(size); duke@435: } duke@435: duke@435: void CHeapObj::operator delete(void* p){ duke@435: free(p); duke@435: }