1 /*
   2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "adlc.hpp"
  26 
  27 void* Chunk::operator new(size_t requested_size, size_t length) throw() {
  28   return CHeapObj::operator new(requested_size + length);
  29 }
  30 
  31 void  Chunk::operator delete(void* p) {
  32         CHeapObj::operator delete(p);
  33 }
  34 
  35 void  Chunk::operator delete(void* p, size_t length) {
  36   CHeapObj::operator delete(p);
  37 }
  38 
  39 Chunk::Chunk(size_t length) {
  40   _next = NULL;         // Chain on the linked list
  41   _len  = length;       // Save actual size
  42 }
  43 
  44 //------------------------------chop-------------------------------------------
  45 void Chunk::chop() {
  46   Chunk *k = this;
  47   while( k ) {
  48     Chunk *tmp = k->_next;
  49     // clear out this chunk (to detect allocation bugs)
  50     memset(k, 0xBAADBABE, k->_len);
  51     free(k);                    // Free chunk (was malloc'd)
  52     k = tmp;
  53   }
  54 }
  55 
  56 void Chunk::next_chop() {
  57   _next->chop();
  58   _next = NULL;
  59 }
  60 
  61 //------------------------------Arena------------------------------------------
  62 Arena::Arena( size_t init_size ) {
  63   init_size = (init_size+3) & ~3;
  64   _first = _chunk = new (init_size) Chunk(init_size);
  65   _hwm = _chunk->bottom();      // Save the cached hwm, max
  66   _max = _chunk->top();
  67   set_size_in_bytes(init_size);
  68 }
  69 
  70 Arena::Arena() {
  71   _first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size);
  72   _hwm = _chunk->bottom();      // Save the cached hwm, max
  73   _max = _chunk->top();
  74   set_size_in_bytes(Chunk::init_size);
  75 }
  76 
  77 Arena::Arena( Arena *a )
  78 : _chunk(a->_chunk), _hwm(a->_hwm), _max(a->_max), _first(a->_first) {
  79   set_size_in_bytes(a->size_in_bytes());
  80 }
  81 
  82 //------------------------------used-------------------------------------------
  83 // Total of all Chunks in arena
  84 size_t Arena::used() const {
  85   size_t sum = _chunk->_len - (_max-_hwm); // Size leftover in this Chunk
  86   register Chunk *k = _first;
  87   while( k != _chunk) {         // Whilst have Chunks in a row
  88     sum += k->_len;             // Total size of this Chunk
  89     k = k->_next;               // Bump along to next Chunk
  90   }
  91   return sum;                   // Return total consumed space.
  92 }
  93 
  94 //------------------------------grow-------------------------------------------
  95 // Grow a new Chunk
  96 void* Arena::grow( size_t x ) {
  97   // Get minimal required size.  Either real big, or even bigger for giant objs
  98   size_t len = max(x, Chunk::size);
  99 
 100   register Chunk *k = _chunk;   // Get filled-up chunk address
 101   _chunk = new (len) Chunk(len);
 102 
 103   if( k ) k->_next = _chunk;    // Append new chunk to end of linked list
 104   else _first = _chunk;
 105   _hwm  = _chunk->bottom();     // Save the cached hwm, max
 106   _max =  _chunk->top();
 107   set_size_in_bytes(size_in_bytes() + len);
 108   void* result = _hwm;
 109   _hwm += x;
 110   return result;
 111 }
 112 
 113 //------------------------------calloc-----------------------------------------
 114 // Allocate zeroed storage in Arena
 115 void *Arena::Acalloc( size_t items, size_t x ) {
 116   size_t z = items*x;   // Total size needed
 117   void *ptr = Amalloc(z);       // Get space
 118   memset( ptr, 0, z );          // Zap space
 119   return ptr;                   // Return space
 120 }
 121 
 122 //------------------------------realloc----------------------------------------
 123 // Reallocate storage in Arena.
 124 void *Arena::Arealloc( void *old_ptr, size_t old_size, size_t new_size ) {
 125   char *c_old = (char*)old_ptr; // Handy name
 126   // Stupid fast special case
 127   if( new_size <= old_size ) {  // Shrink in-place
 128     if( c_old+old_size == _hwm) // Attempt to free the excess bytes
 129       _hwm = c_old+new_size;    // Adjust hwm
 130     return c_old;
 131   }
 132 
 133   // See if we can resize in-place
 134   if( (c_old+old_size == _hwm) &&       // Adjusting recent thing
 135       (c_old+new_size <= _max) ) {      // Still fits where it sits
 136     _hwm = c_old+new_size;      // Adjust hwm
 137     return c_old;               // Return old pointer
 138   }
 139 
 140   // Oops, got to relocate guts
 141   void *new_ptr = Amalloc(new_size);
 142   memcpy( new_ptr, c_old, old_size );
 143   Afree(c_old,old_size);        // Mostly done to keep stats accurate
 144   return new_ptr;
 145 }
 146 
 147 //------------------------------reset------------------------------------------
 148 // Reset this Arena to empty, and return this Arenas guts in a new Arena.
 149 Arena *Arena::reset(void) {
 150   Arena *a = new Arena(this);   // New empty arena
 151   _first = _chunk = NULL;       // Normal, new-arena initialization
 152   _hwm = _max = NULL;
 153   return a;                     // Return Arena with guts
 154 }
 155 
 156 //------------------------------contains---------------------------------------
 157 // Determine if pointer belongs to this Arena or not.
 158 bool Arena::contains( const void *ptr ) const {
 159   if( (void*)_chunk->bottom() <= ptr && ptr < (void*)_hwm )
 160     return true;                // Check for in this chunk
 161   for( Chunk *c = _first; c; c = c->_next )
 162     if( (void*)c->bottom() <= ptr && ptr < (void*)c->top())
 163       return true;              // Check for every chunk in Arena
 164   return false;                 // Not in any Chunk, so not in Arena
 165 }
 166 
 167 //-----------------------------------------------------------------------------
 168 // CHeapObj
 169 
 170 void* CHeapObj::operator new(size_t size) throw() {
 171   return (void *) malloc(size);
 172 }
 173 
 174 void CHeapObj::operator delete(void* p){
 175  free(p);
 176 }