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