src/share/vm/adlc/arena.cpp

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/adlc/arena.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,172 @@
     1.4 +/*
     1.5 + * Copyright 1998-2002 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "adlc.hpp"
    1.29 +
    1.30 +void* Chunk::operator new(size_t requested_size, size_t length) {
    1.31 +  return CHeapObj::operator new(requested_size + length);
    1.32 +}
    1.33 +
    1.34 +void  Chunk::operator delete(void* p, size_t length) {
    1.35 +  CHeapObj::operator delete(p);
    1.36 +}
    1.37 +
    1.38 +Chunk::Chunk(size_t length) {
    1.39 +  _next = NULL;         // Chain on the linked list
    1.40 +  _len  = length;       // Save actual size
    1.41 +}
    1.42 +
    1.43 +//------------------------------chop-------------------------------------------
    1.44 +void Chunk::chop() {
    1.45 +  Chunk *k = this;
    1.46 +  while( k ) {
    1.47 +    Chunk *tmp = k->_next;
    1.48 +    // clear out this chunk (to detect allocation bugs)
    1.49 +    memset(k, 0xBAADBABE, k->_len);
    1.50 +    free(k);                    // Free chunk (was malloc'd)
    1.51 +    k = tmp;
    1.52 +  }
    1.53 +}
    1.54 +
    1.55 +void Chunk::next_chop() {
    1.56 +  _next->chop();
    1.57 +  _next = NULL;
    1.58 +}
    1.59 +
    1.60 +//------------------------------Arena------------------------------------------
    1.61 +Arena::Arena( size_t init_size ) {
    1.62 +  init_size = (init_size+3) & ~3;
    1.63 +  _first = _chunk = new (init_size) Chunk(init_size);
    1.64 +  _hwm = _chunk->bottom();      // Save the cached hwm, max
    1.65 +  _max = _chunk->top();
    1.66 +  set_size_in_bytes(init_size);
    1.67 +}
    1.68 +
    1.69 +Arena::Arena() {
    1.70 +  _first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size);
    1.71 +  _hwm = _chunk->bottom();      // Save the cached hwm, max
    1.72 +  _max = _chunk->top();
    1.73 +  set_size_in_bytes(Chunk::init_size);
    1.74 +}
    1.75 +
    1.76 +Arena::Arena( Arena *a )
    1.77 +: _chunk(a->_chunk), _hwm(a->_hwm), _max(a->_max), _first(a->_first) {
    1.78 +  set_size_in_bytes(a->size_in_bytes());
    1.79 +}
    1.80 +
    1.81 +//------------------------------used-------------------------------------------
    1.82 +// Total of all Chunks in arena
    1.83 +size_t Arena::used() const {
    1.84 +  size_t sum = _chunk->_len - (_max-_hwm); // Size leftover in this Chunk
    1.85 +  register Chunk *k = _first;
    1.86 +  while( k != _chunk) {         // Whilst have Chunks in a row
    1.87 +    sum += k->_len;             // Total size of this Chunk
    1.88 +    k = k->_next;               // Bump along to next Chunk
    1.89 +  }
    1.90 +  return sum;                   // Return total consumed space.
    1.91 +}
    1.92 +
    1.93 +//------------------------------grow-------------------------------------------
    1.94 +// Grow a new Chunk
    1.95 +void* Arena::grow( size_t x ) {
    1.96 +  // Get minimal required size.  Either real big, or even bigger for giant objs
    1.97 +  size_t len = max(x, Chunk::size);
    1.98 +
    1.99 +  register Chunk *k = _chunk;   // Get filled-up chunk address
   1.100 +  _chunk = new (len) Chunk(len);
   1.101 +
   1.102 +  if( k ) k->_next = _chunk;    // Append new chunk to end of linked list
   1.103 +  else _first = _chunk;
   1.104 +  _hwm  = _chunk->bottom();     // Save the cached hwm, max
   1.105 +  _max =  _chunk->top();
   1.106 +  set_size_in_bytes(size_in_bytes() + len);
   1.107 +  void* result = _hwm;
   1.108 +  _hwm += x;
   1.109 +  return result;
   1.110 +}
   1.111 +
   1.112 +//------------------------------calloc-----------------------------------------
   1.113 +// Allocate zeroed storage in Arena
   1.114 +void *Arena::Acalloc( size_t items, size_t x ) {
   1.115 +  size_t z = items*x;   // Total size needed
   1.116 +  void *ptr = Amalloc(z);       // Get space
   1.117 +  memset( ptr, 0, z );          // Zap space
   1.118 +  return ptr;                   // Return space
   1.119 +}
   1.120 +
   1.121 +//------------------------------realloc----------------------------------------
   1.122 +// Reallocate storage in Arena.
   1.123 +void *Arena::Arealloc( void *old_ptr, size_t old_size, size_t new_size ) {
   1.124 +  char *c_old = (char*)old_ptr; // Handy name
   1.125 +  // Stupid fast special case
   1.126 +  if( new_size <= old_size ) {  // Shrink in-place
   1.127 +    if( c_old+old_size == _hwm) // Attempt to free the excess bytes
   1.128 +      _hwm = c_old+new_size;    // Adjust hwm
   1.129 +    return c_old;
   1.130 +  }
   1.131 +
   1.132 +  // See if we can resize in-place
   1.133 +  if( (c_old+old_size == _hwm) &&       // Adjusting recent thing
   1.134 +      (c_old+new_size <= _max) ) {      // Still fits where it sits
   1.135 +    _hwm = c_old+new_size;      // Adjust hwm
   1.136 +    return c_old;               // Return old pointer
   1.137 +  }
   1.138 +
   1.139 +  // Oops, got to relocate guts
   1.140 +  void *new_ptr = Amalloc(new_size);
   1.141 +  memcpy( new_ptr, c_old, old_size );
   1.142 +  Afree(c_old,old_size);        // Mostly done to keep stats accurate
   1.143 +  return new_ptr;
   1.144 +}
   1.145 +
   1.146 +//------------------------------reset------------------------------------------
   1.147 +// Reset this Arena to empty, and return this Arenas guts in a new Arena.
   1.148 +Arena *Arena::reset(void) {
   1.149 +  Arena *a = new Arena(this);   // New empty arena
   1.150 +  _first = _chunk = NULL;       // Normal, new-arena initialization
   1.151 +  _hwm = _max = NULL;
   1.152 +  return a;                     // Return Arena with guts
   1.153 +}
   1.154 +
   1.155 +//------------------------------contains---------------------------------------
   1.156 +// Determine if pointer belongs to this Arena or not.
   1.157 +bool Arena::contains( const void *ptr ) const {
   1.158 +  if( (void*)_chunk->bottom() <= ptr && ptr < (void*)_hwm )
   1.159 +    return true;                // Check for in this chunk
   1.160 +  for( Chunk *c = _first; c; c = c->_next )
   1.161 +    if( (void*)c->bottom() <= ptr && ptr < (void*)c->top())
   1.162 +      return true;              // Check for every chunk in Arena
   1.163 +  return false;                 // Not in any Chunk, so not in Arena
   1.164 +}
   1.165 +
   1.166 +//-----------------------------------------------------------------------------
   1.167 +// CHeapObj
   1.168 +
   1.169 +void* CHeapObj::operator new(size_t size){
   1.170 +  return (void *) malloc(size);
   1.171 +}
   1.172 +
   1.173 +void CHeapObj::operator delete(void* p){
   1.174 + free(p);
   1.175 +}

mercurial