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