annotate src/share/vm/adlc/arena.cpp @ 7341:9a23a160ca57

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