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