src/share/vm/adlc/arena.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial