src/share/vm/adlc/arena.hpp

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 #ifndef SHARE_VM_ADLC_ARENA_HPP
aoqi@0 26 #define SHARE_VM_ADLC_ARENA_HPP
aoqi@0 27
aoqi@0 28 // All classes in the virtual machine must be subclassed
aoqi@0 29 // by one of the following allocation classes:
aoqi@0 30 //
aoqi@0 31 //
aoqi@0 32 // For objects allocated in the C-heap (managed by: free & malloc).
aoqi@0 33 // - CHeapObj
aoqi@0 34 //
aoqi@0 35 //
aoqi@0 36 // For embedded objects.
aoqi@0 37 // - ValueObj
aoqi@0 38 //
aoqi@0 39 // For classes used as name spaces.
aoqi@0 40 // - AllStatic
aoqi@0 41 //
aoqi@0 42
aoqi@0 43 class CHeapObj {
aoqi@0 44 public:
aoqi@0 45 void* operator new(size_t size) throw();
aoqi@0 46 void operator delete(void* p);
aoqi@0 47 void* new_array(size_t size);
aoqi@0 48 };
aoqi@0 49
aoqi@0 50
aoqi@0 51 // Base class for objects used as value objects.
aoqi@0 52 // Calling new or delete will result in fatal error.
aoqi@0 53
aoqi@0 54 class ValueObj {
aoqi@0 55 public:
aoqi@0 56 void* operator new(size_t size) throw();
aoqi@0 57 void operator delete(void* p);
aoqi@0 58 };
aoqi@0 59
aoqi@0 60 // Base class for classes that constitute name spaces.
aoqi@0 61
aoqi@0 62 class AllStatic {
aoqi@0 63 public:
aoqi@0 64 void* operator new(size_t size) throw();
aoqi@0 65 void operator delete(void* p);
aoqi@0 66 };
aoqi@0 67
aoqi@0 68
aoqi@0 69 //------------------------------Chunk------------------------------------------
aoqi@0 70 // Linked list of raw memory chunks
aoqi@0 71 class Chunk: public CHeapObj {
aoqi@0 72 public:
aoqi@0 73 void* operator new(size_t size, size_t length) throw();
aoqi@0 74 void operator delete(void* p, size_t length);
aoqi@0 75 Chunk(size_t length);
aoqi@0 76
aoqi@0 77 enum {
aoqi@0 78 init_size = 1*1024, // Size of first chunk
aoqi@0 79 size = 32*1024 // Default size of an Arena chunk (following the first)
aoqi@0 80 };
aoqi@0 81 Chunk* _next; // Next Chunk in list
aoqi@0 82 size_t _len; // Size of this Chunk
aoqi@0 83
aoqi@0 84 void chop(); // Chop this chunk
aoqi@0 85 void next_chop(); // Chop next chunk
aoqi@0 86
aoqi@0 87 // Boundaries of data area (possibly unused)
aoqi@0 88 char* bottom() const { return ((char*) this) + sizeof(Chunk); }
aoqi@0 89 char* top() const { return bottom() + _len; }
aoqi@0 90 };
aoqi@0 91
aoqi@0 92
aoqi@0 93 //------------------------------Arena------------------------------------------
aoqi@0 94 // Fast allocation of memory
aoqi@0 95 class Arena: public CHeapObj {
aoqi@0 96 protected:
aoqi@0 97 friend class ResourceMark;
aoqi@0 98 friend class HandleMark;
aoqi@0 99 friend class NoHandleMark;
aoqi@0 100 Chunk *_first; // First chunk
aoqi@0 101 Chunk *_chunk; // current chunk
aoqi@0 102 char *_hwm, *_max; // High water mark and max in current chunk
aoqi@0 103 void* grow(size_t x); // Get a new Chunk of at least size x
aoqi@0 104 size_t _size_in_bytes; // Size of arena (used for memory usage tracing)
aoqi@0 105 public:
aoqi@0 106 Arena();
aoqi@0 107 Arena(size_t init_size);
aoqi@0 108 Arena(Arena *old);
aoqi@0 109 ~Arena() { _first->chop(); }
aoqi@0 110 char* hwm() const { return _hwm; }
aoqi@0 111
aoqi@0 112 // Fast allocate in the arena. Common case is: pointer test + increment.
aoqi@0 113 void* Amalloc(size_t x) {
aoqi@0 114 #ifdef _LP64
aoqi@0 115 x = (x + (8-1)) & ((unsigned)(-8));
aoqi@0 116 #else
aoqi@0 117 x = (x + (4-1)) & ((unsigned)(-4));
aoqi@0 118 #endif
aoqi@0 119 if (_hwm + x > _max) {
aoqi@0 120 return grow(x);
aoqi@0 121 } else {
aoqi@0 122 char *old = _hwm;
aoqi@0 123 _hwm += x;
aoqi@0 124 return old;
aoqi@0 125 }
aoqi@0 126 }
aoqi@0 127 // Further assume size is padded out to words
aoqi@0 128 // Warning: in LP64, Amalloc_4 is really Amalloc_8
aoqi@0 129 void *Amalloc_4(size_t x) {
aoqi@0 130 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
aoqi@0 131 if (_hwm + x > _max) {
aoqi@0 132 return grow(x);
aoqi@0 133 } else {
aoqi@0 134 char *old = _hwm;
aoqi@0 135 _hwm += x;
aoqi@0 136 return old;
aoqi@0 137 }
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 // Fast delete in area. Common case is: NOP (except for storage reclaimed)
aoqi@0 141 void Afree(void *ptr, size_t size) {
aoqi@0 142 if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 void *Acalloc( size_t items, size_t x );
aoqi@0 146 void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );
aoqi@0 147
aoqi@0 148 // Reset this Arena to empty, and return this Arenas guts in a new Arena.
aoqi@0 149 Arena *reset(void);
aoqi@0 150
aoqi@0 151 // Determine if pointer belongs to this Arena or not.
aoqi@0 152 bool contains( const void *ptr ) const;
aoqi@0 153
aoqi@0 154 // Total of all chunks in use (not thread-safe)
aoqi@0 155 size_t used() const;
aoqi@0 156
aoqi@0 157 // Total # of bytes used
aoqi@0 158 size_t size_in_bytes() const { return _size_in_bytes; }
aoqi@0 159 void set_size_in_bytes(size_t size) { _size_in_bytes = size; }
aoqi@0 160 };
aoqi@0 161
aoqi@0 162 #endif // SHARE_VM_ADLC_ARENA_HPP

mercurial