src/hotspot/share/adlc/arena.hpp
author jcm
Thu, 15 Nov 2018 21:26:35 -0800
changeset 52583 a3aa8d5380d9
parent 49392 2956d0ece7a9
child 53244 9807daeb47c4
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
/*
49023
6b8fb182bb17 8196880: VS2017 Addition of Global Delete Operator with Size Parameter Conflicts with Arena's Chunk Provided One
lfoltan
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
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_ADLC_ARENA_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_ADLC_ARENA_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
52583
a3aa8d5380d9 8212779: ADL Parser does not check allocation return values in all cases
jcm
parents: 49392
diff changeset
    28
void* AllocateHeap(size_t size);
a3aa8d5380d9 8212779: ADL Parser does not check allocation return values in all cases
jcm
parents: 49392
diff changeset
    29
49392
2956d0ece7a9 8199282: Remove ValueObj class for allocation subclassing for gc code
coleenp
parents: 49023
diff changeset
    30
// All classes in adlc may be derived
2956d0ece7a9 8199282: Remove ValueObj class for allocation subclassing for gc code
coleenp
parents: 49023
diff changeset
    31
// from one of the following allocation classes:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
//
49392
2956d0ece7a9 8199282: Remove ValueObj class for allocation subclassing for gc code
coleenp
parents: 49023
diff changeset
    33
// For objects allocated in the C-heap (managed by: malloc & free).
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// - CHeapObj
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// For classes used as name spaces.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// - AllStatic
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
class CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
 public:
19696
bd5a0131bde1 8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents: 7397
diff changeset
    42
  void* operator new(size_t size) throw();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  void  operator delete(void* p);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  void* new_array(size_t size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
// Base class for classes that constitute name spaces.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
class AllStatic {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
 public:
19696
bd5a0131bde1 8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents: 7397
diff changeset
    51
  void* operator new(size_t size) throw();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  void operator delete(void* p);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
//------------------------------Chunk------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
// Linked list of raw memory chunks
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
class Chunk: public CHeapObj {
49023
6b8fb182bb17 8196880: VS2017 Addition of Global Delete Operator with Size Parameter Conflicts with Arena's Chunk Provided One
lfoltan
parents: 47216
diff changeset
    59
 private:
6b8fb182bb17 8196880: VS2017 Addition of Global Delete Operator with Size Parameter Conflicts with Arena's Chunk Provided One
lfoltan
parents: 47216
diff changeset
    60
  // This ordinary operator delete is needed even though not used, so the
6b8fb182bb17 8196880: VS2017 Addition of Global Delete Operator with Size Parameter Conflicts with Arena's Chunk Provided One
lfoltan
parents: 47216
diff changeset
    61
  // below two-argument operator delete will be treated as a placement
6b8fb182bb17 8196880: VS2017 Addition of Global Delete Operator with Size Parameter Conflicts with Arena's Chunk Provided One
lfoltan
parents: 47216
diff changeset
    62
  // delete rather than an ordinary sized delete; see C++14 3.7.4.2/p2.
6b8fb182bb17 8196880: VS2017 Addition of Global Delete Operator with Size Parameter Conflicts with Arena's Chunk Provided One
lfoltan
parents: 47216
diff changeset
    63
  void operator delete(void* p);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
 public:
19696
bd5a0131bde1 8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents: 7397
diff changeset
    65
  void* operator new(size_t size, size_t length) throw();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  void  operator delete(void* p, size_t length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  Chunk(size_t length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
      init_size =  1*1024,      // Size of first chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
      size      = 32*1024       // Default size of an Arena chunk (following the first)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  Chunk*       _next;           // Next Chunk in list
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  size_t       _len;            // Size of this Chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  void chop();                  // Chop this chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  void next_chop();             // Chop next chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  // Boundaries of data area (possibly unused)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  char* bottom() const { return ((char*) this) + sizeof(Chunk);  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  char* top()    const { return bottom() + _len; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
//------------------------------Arena------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
// Fast allocation of memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
class Arena: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  friend class ResourceMark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  friend class HandleMark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  friend class NoHandleMark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  Chunk *_first;                // First chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  Chunk *_chunk;                // current chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  char *_hwm, *_max;            // High water mark and max in current chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  void* grow(size_t x);         // Get a new Chunk of at least size x
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  size_t _size_in_bytes;          // Size of arena (used for memory usage tracing)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  Arena();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  Arena(size_t init_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  Arena(Arena *old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  ~Arena()                      { _first->chop(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  char* hwm() const             { return _hwm; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // Fast allocate in the arena.  Common case is: pointer test + increment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  void* Amalloc(size_t x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
#ifdef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    x = (x + (8-1)) & ((unsigned)(-8));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    x = (x + (4-1)) & ((unsigned)(-4));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    if (_hwm + x > _max) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
      return grow(x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      char *old = _hwm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
      _hwm += x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
      return old;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  // Further assume size is padded out to words
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  // Warning:  in LP64, Amalloc_4 is really Amalloc_8
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  void *Amalloc_4(size_t x) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    if (_hwm + x > _max) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      return grow(x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      char *old = _hwm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
      _hwm += x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
      return old;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  // Fast delete in area.  Common case is: NOP (except for storage reclaimed)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  void Afree(void *ptr, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  void *Acalloc( size_t items, size_t x );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  // Reset this Arena to empty, and return this Arenas guts in a new Arena.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  Arena *reset(void);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  // Determine if pointer belongs to this Arena or not.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  bool contains( const void *ptr ) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  // Total of all chunks in use (not thread-safe)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  size_t used() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  // Total # of bytes used
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  size_t size_in_bytes() const         {  return _size_in_bytes; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  void   set_size_in_bytes(size_t size)  { _size_in_bytes = size;   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   153
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   154
#endif // SHARE_VM_ADLC_ARENA_HPP