1 /* 2 * Copyright (c) 1998, 2018, 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* AllocateHeap(size_t size, AllocFailType alloc_fail_mode) { 28 unsigned char* ptr = (unsigned char*) malloc(size); 29 if (ptr == NULL && size != 0 && alloc_fail_mode == AllocFailStrategy::EXIT_OOM) { 30 exit(1); 31 } 32 return ptr; 33 } 34 35 void* Chunk::operator new(size_t requested_size, size_t length) throw() { 36 return CHeapObj::operator new(requested_size + length); 37 } 38 39 void Chunk::operator delete(void* p, size_t length) { 40 CHeapObj::operator delete(p); 41 } 42 43 Chunk::Chunk(size_t length) { 44 _next = NULL; // Chain on the linked list 45 _len = length; // Save actual size 46 } 47 48 //------------------------------chop------------------------------------------- 49 void Chunk::chop() { 50 Chunk *k = this; 51 while( k ) { 52 Chunk *tmp = k->_next; 53 // clear out this chunk (to detect allocation bugs) 54 memset(k, 0xBE, k->_len); 55 free(k); // Free chunk (was malloc'd) 56 k = tmp; 57 } 58 } 59 60 void Chunk::next_chop() { 61 _next->chop(); 62 _next = NULL; 63 } 64 65 //------------------------------Arena------------------------------------------ 66 Arena::Arena( size_t init_size ) { 67 init_size = (init_size+3) & ~3; 68 _first = _chunk = new (init_size) Chunk(init_size); 69 _hwm = _chunk->bottom(); // Save the cached hwm, max 70 _max = _chunk->top(); 71 set_size_in_bytes(init_size); 72 } 73 74 Arena::Arena() { 75 _first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size); 76 _hwm = _chunk->bottom(); // Save the cached hwm, max 77 _max = _chunk->top(); 78 set_size_in_bytes(Chunk::init_size); 79 } 80 81 Arena::Arena( Arena *a ) 82 : _chunk(a->_chunk), _hwm(a->_hwm), _max(a->_max), _first(a->_first) { 83 set_size_in_bytes(a->size_in_bytes()); 84 } 85 86 //------------------------------used------------------------------------------- 87 // Total of all Chunks in arena 88 size_t Arena::used() const { 89 size_t sum = _chunk->_len - (_max-_hwm); // Size leftover in this Chunk 90 Chunk *k = _first; 91 while( k != _chunk) { // Whilst have Chunks in a row 92 sum += k->_len; // Total size of this Chunk 93 k = k->_next; // Bump along to next Chunk 94 } 95 return sum; // Return total consumed space. 96 } 97 98 //------------------------------grow------------------------------------------- 99 // Grow a new Chunk 100 void* Arena::grow( size_t x ) { 101 // Get minimal required size. Either real big, or even bigger for giant objs 102 size_t len = max(x, Chunk::size); 103 104 Chunk *k = _chunk; // Get filled-up chunk address 105 _chunk = new (len) Chunk(len); 106 107 if( k ) k->_next = _chunk; // Append new chunk to end of linked list 108 else _first = _chunk; 109 _hwm = _chunk->bottom(); // Save the cached hwm, max 110 _max = _chunk->top(); 111 set_size_in_bytes(size_in_bytes() + len); 112 void* result = _hwm; 113 _hwm += x; 114 return result; 115 } 116 117 //------------------------------calloc----------------------------------------- 118 // Allocate zeroed storage in Arena 119 void *Arena::Acalloc( size_t items, size_t x ) { 120 size_t z = items*x; // Total size needed 121 void *ptr = Amalloc(z); // Get space 122 memset( ptr, 0, z ); // Zap space 123 return ptr; // Return space 124 } 125 126 //------------------------------realloc---------------------------------------- 127 // Reallocate storage in Arena. 128 void *Arena::Arealloc( void *old_ptr, size_t old_size, size_t new_size ) { 129 char *c_old = (char*)old_ptr; // Handy name 130 // Stupid fast special case 131 if( new_size <= old_size ) { // Shrink in-place 132 if( c_old+old_size == _hwm) // Attempt to free the excess bytes 133 _hwm = c_old+new_size; // Adjust hwm 134 return c_old; 135 } 136 137 // See if we can resize in-place 138 if( (c_old+old_size == _hwm) && // Adjusting recent thing 139 (c_old+new_size <= _max) ) { // Still fits where it sits 140 _hwm = c_old+new_size; // Adjust hwm 141 return c_old; // Return old pointer 142 } 143 144 // Oops, got to relocate guts 145 void *new_ptr = Amalloc(new_size); 146 memcpy( new_ptr, c_old, old_size ); 147 Afree(c_old,old_size); // Mostly done to keep stats accurate 148 return new_ptr; 149 } 150 151 //------------------------------reset------------------------------------------ 152 // Reset this Arena to empty, and return this Arenas guts in a new Arena. 153 Arena *Arena::reset(void) { 154 Arena *a = new Arena(this); // New empty arena 155 _first = _chunk = NULL; // Normal, new-arena initialization 156 _hwm = _max = NULL; 157 return a; // Return Arena with guts 158 } 159 160 //------------------------------contains--------------------------------------- 161 // Determine if pointer belongs to this Arena or not. 162 bool Arena::contains( const void *ptr ) const { 163 if( (void*)_chunk->bottom() <= ptr && ptr < (void*)_hwm ) 164 return true; // Check for in this chunk 165 for( Chunk *c = _first; c; c = c->_next ) 166 if( (void*)c->bottom() <= ptr && ptr < (void*)c->top()) 167 return true; // Check for every chunk in Arena 168 return false; // Not in any Chunk, so not in Arena 169 } 170 171 //----------------------------------------------------------------------------- 172 // CHeapObj 173 174 void* CHeapObj::operator new(size_t size) throw() { 175 return (void *) AllocateHeap(size); 176 } 177 178 void CHeapObj::operator delete(void* p){ 179 free(p); 180 }