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