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