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