duke@435: /* coleenp@5614: * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_ADLC_ARENA_HPP stefank@2314: #define SHARE_VM_ADLC_ARENA_HPP stefank@2314: duke@435: // All classes in the virtual machine must be subclassed duke@435: // by one of the following allocation classes: duke@435: // duke@435: // duke@435: // For objects allocated in the C-heap (managed by: free & malloc). duke@435: // - CHeapObj duke@435: // duke@435: // duke@435: // For embedded objects. duke@435: // - ValueObj duke@435: // duke@435: // For classes used as name spaces. duke@435: // - AllStatic duke@435: // duke@435: duke@435: class CHeapObj { duke@435: public: coleenp@5614: void* operator new(size_t size) throw(); duke@435: void operator delete(void* p); duke@435: void* new_array(size_t size); duke@435: }; duke@435: duke@435: duke@435: // Base class for objects used as value objects. duke@435: // Calling new or delete will result in fatal error. duke@435: duke@435: class ValueObj { duke@435: public: coleenp@5614: void* operator new(size_t size) throw(); duke@435: void operator delete(void* p); duke@435: }; duke@435: duke@435: // Base class for classes that constitute name spaces. duke@435: duke@435: class AllStatic { duke@435: public: coleenp@5614: void* operator new(size_t size) throw(); duke@435: void operator delete(void* p); duke@435: }; duke@435: duke@435: duke@435: //------------------------------Chunk------------------------------------------ duke@435: // Linked list of raw memory chunks duke@435: class Chunk: public CHeapObj { duke@435: public: coleenp@5614: void* operator new(size_t size, size_t length) throw(); duke@435: void operator delete(void* p, size_t length); duke@435: Chunk(size_t length); duke@435: duke@435: enum { duke@435: init_size = 1*1024, // Size of first chunk duke@435: size = 32*1024 // Default size of an Arena chunk (following the first) duke@435: }; duke@435: Chunk* _next; // Next Chunk in list duke@435: size_t _len; // Size of this Chunk duke@435: duke@435: void chop(); // Chop this chunk duke@435: void next_chop(); // Chop next chunk duke@435: duke@435: // Boundaries of data area (possibly unused) duke@435: char* bottom() const { return ((char*) this) + sizeof(Chunk); } duke@435: char* top() const { return bottom() + _len; } duke@435: }; duke@435: duke@435: duke@435: //------------------------------Arena------------------------------------------ duke@435: // Fast allocation of memory duke@435: class Arena: public CHeapObj { duke@435: protected: duke@435: friend class ResourceMark; duke@435: friend class HandleMark; duke@435: friend class NoHandleMark; duke@435: Chunk *_first; // First chunk duke@435: Chunk *_chunk; // current chunk duke@435: char *_hwm, *_max; // High water mark and max in current chunk duke@435: void* grow(size_t x); // Get a new Chunk of at least size x duke@435: size_t _size_in_bytes; // Size of arena (used for memory usage tracing) duke@435: public: duke@435: Arena(); duke@435: Arena(size_t init_size); duke@435: Arena(Arena *old); duke@435: ~Arena() { _first->chop(); } duke@435: char* hwm() const { return _hwm; } duke@435: duke@435: // Fast allocate in the arena. Common case is: pointer test + increment. duke@435: void* Amalloc(size_t x) { duke@435: #ifdef _LP64 duke@435: x = (x + (8-1)) & ((unsigned)(-8)); duke@435: #else duke@435: x = (x + (4-1)) & ((unsigned)(-4)); duke@435: #endif duke@435: if (_hwm + x > _max) { duke@435: return grow(x); duke@435: } else { duke@435: char *old = _hwm; duke@435: _hwm += x; duke@435: return old; duke@435: } duke@435: } duke@435: // Further assume size is padded out to words duke@435: // Warning: in LP64, Amalloc_4 is really Amalloc_8 duke@435: void *Amalloc_4(size_t x) { duke@435: assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" ); duke@435: if (_hwm + x > _max) { duke@435: return grow(x); duke@435: } else { duke@435: char *old = _hwm; duke@435: _hwm += x; duke@435: return old; duke@435: } duke@435: } duke@435: duke@435: // Fast delete in area. Common case is: NOP (except for storage reclaimed) duke@435: void Afree(void *ptr, size_t size) { duke@435: if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr; duke@435: } duke@435: duke@435: void *Acalloc( size_t items, size_t x ); duke@435: void *Arealloc( void *old_ptr, size_t old_size, size_t new_size ); duke@435: duke@435: // Reset this Arena to empty, and return this Arenas guts in a new Arena. duke@435: Arena *reset(void); duke@435: duke@435: // Determine if pointer belongs to this Arena or not. duke@435: bool contains( const void *ptr ) const; duke@435: duke@435: // Total of all chunks in use (not thread-safe) duke@435: size_t used() const; duke@435: duke@435: // Total # of bytes used duke@435: size_t size_in_bytes() const { return _size_in_bytes; } duke@435: void set_size_in_bytes(size_t size) { _size_in_bytes = size; } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_ADLC_ARENA_HPP