src/share/vm/adlc/arena.cpp

Mon, 25 Jun 2012 21:33:35 -0400

author
coleenp
date
Mon, 25 Jun 2012 21:33:35 -0400
changeset 3875
246d977b51f2
parent 2314
f95d63e2154a
child 5614
9758d9f36299
permissions
-rw-r--r--

7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
Summary: Cannot delete _buckets and HashtableEntries in shared space (CDS)
Reviewed-by: acorn, kvn, dlong, dcubed, kamg

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

mercurial