src/share/vm/adlc/arena.hpp

Wed, 22 Jan 2014 17:42:23 -0800

author
kvn
date
Wed, 22 Jan 2014 17:42:23 -0800
changeset 6503
a9becfeecd1b
parent 5614
9758d9f36299
child 6876
710a3c8b516e
child 9323
6688d6c6a225
permissions
-rw-r--r--

Merge

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

mercurial