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