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