annotate src/share/vm/adlc/arena.hpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 1998-2002 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 // All classes in the virtual machine must be subclassed
duke@0 26 // by one of the following allocation classes:
duke@0 27 //
duke@0 28 //
duke@0 29 // For objects allocated in the C-heap (managed by: free & malloc).
duke@0 30 // - CHeapObj
duke@0 31 //
duke@0 32 //
duke@0 33 // For embedded objects.
duke@0 34 // - ValueObj
duke@0 35 //
duke@0 36 // For classes used as name spaces.
duke@0 37 // - AllStatic
duke@0 38 //
duke@0 39
duke@0 40 class CHeapObj {
duke@0 41 public:
duke@0 42 void* operator new(size_t size);
duke@0 43 void operator delete(void* p);
duke@0 44 void* new_array(size_t size);
duke@0 45 };
duke@0 46
duke@0 47
duke@0 48 // Base class for objects used as value objects.
duke@0 49 // Calling new or delete will result in fatal error.
duke@0 50
duke@0 51 class ValueObj {
duke@0 52 public:
duke@0 53 void* operator new(size_t size);
duke@0 54 void operator delete(void* p);
duke@0 55 };
duke@0 56
duke@0 57 // Base class for classes that constitute name spaces.
duke@0 58
duke@0 59 class AllStatic {
duke@0 60 public:
duke@0 61 void* operator new(size_t size);
duke@0 62 void operator delete(void* p);
duke@0 63 };
duke@0 64
duke@0 65
duke@0 66 //------------------------------Chunk------------------------------------------
duke@0 67 // Linked list of raw memory chunks
duke@0 68 class Chunk: public CHeapObj {
duke@0 69 public:
duke@0 70 void* operator new(size_t size, size_t length);
duke@0 71 void operator delete(void* p, size_t length);
duke@0 72 Chunk(size_t length);
duke@0 73
duke@0 74 enum {
duke@0 75 init_size = 1*1024, // Size of first chunk
duke@0 76 size = 32*1024 // Default size of an Arena chunk (following the first)
duke@0 77 };
duke@0 78 Chunk* _next; // Next Chunk in list
duke@0 79 size_t _len; // Size of this Chunk
duke@0 80
duke@0 81 void chop(); // Chop this chunk
duke@0 82 void next_chop(); // Chop next chunk
duke@0 83
duke@0 84 // Boundaries of data area (possibly unused)
duke@0 85 char* bottom() const { return ((char*) this) + sizeof(Chunk); }
duke@0 86 char* top() const { return bottom() + _len; }
duke@0 87 };
duke@0 88
duke@0 89
duke@0 90 //------------------------------Arena------------------------------------------
duke@0 91 // Fast allocation of memory
duke@0 92 class Arena: public CHeapObj {
duke@0 93 protected:
duke@0 94 friend class ResourceMark;
duke@0 95 friend class HandleMark;
duke@0 96 friend class NoHandleMark;
duke@0 97 Chunk *_first; // First chunk
duke@0 98 Chunk *_chunk; // current chunk
duke@0 99 char *_hwm, *_max; // High water mark and max in current chunk
duke@0 100 void* grow(size_t x); // Get a new Chunk of at least size x
duke@0 101 size_t _size_in_bytes; // Size of arena (used for memory usage tracing)
duke@0 102 public:
duke@0 103 Arena();
duke@0 104 Arena(size_t init_size);
duke@0 105 Arena(Arena *old);
duke@0 106 ~Arena() { _first->chop(); }
duke@0 107 char* hwm() const { return _hwm; }
duke@0 108
duke@0 109 // Fast allocate in the arena. Common case is: pointer test + increment.
duke@0 110 void* Amalloc(size_t x) {
duke@0 111 #ifdef _LP64
duke@0 112 x = (x + (8-1)) & ((unsigned)(-8));
duke@0 113 #else
duke@0 114 x = (x + (4-1)) & ((unsigned)(-4));
duke@0 115 #endif
duke@0 116 if (_hwm + x > _max) {
duke@0 117 return grow(x);
duke@0 118 } else {
duke@0 119 char *old = _hwm;
duke@0 120 _hwm += x;
duke@0 121 return old;
duke@0 122 }
duke@0 123 }
duke@0 124 // Further assume size is padded out to words
duke@0 125 // Warning: in LP64, Amalloc_4 is really Amalloc_8
duke@0 126 void *Amalloc_4(size_t x) {
duke@0 127 assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
duke@0 128 if (_hwm + x > _max) {
duke@0 129 return grow(x);
duke@0 130 } else {
duke@0 131 char *old = _hwm;
duke@0 132 _hwm += x;
duke@0 133 return old;
duke@0 134 }
duke@0 135 }
duke@0 136
duke@0 137 // Fast delete in area. Common case is: NOP (except for storage reclaimed)
duke@0 138 void Afree(void *ptr, size_t size) {
duke@0 139 if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
duke@0 140 }
duke@0 141
duke@0 142 void *Acalloc( size_t items, size_t x );
duke@0 143 void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );
duke@0 144
duke@0 145 // Reset this Arena to empty, and return this Arenas guts in a new Arena.
duke@0 146 Arena *reset(void);
duke@0 147
duke@0 148 // Determine if pointer belongs to this Arena or not.
duke@0 149 bool contains( const void *ptr ) const;
duke@0 150
duke@0 151 // Total of all chunks in use (not thread-safe)
duke@0 152 size_t used() const;
duke@0 153
duke@0 154 // Total # of bytes used
duke@0 155 size_t size_in_bytes() const { return _size_in_bytes; }
duke@0 156 void set_size_in_bytes(size_t size) { _size_in_bytes = size; }
duke@0 157 };