Path: blob/master/src/hotspot/share/adlc/arena.hpp
41144 views
/*1* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_ADLC_ARENA_HPP25#define SHARE_ADLC_ARENA_HPP2627void* AllocateHeap(size_t size);28void* ReAllocateHeap(void* old_ptr, size_t size);2930// All classes in adlc may be derived31// from one of the following allocation classes:32//33// For objects allocated in the C-heap (managed by: malloc & free).34// - CHeapObj35//36// For classes used as name spaces.37// - AllStatic38//3940class CHeapObj {41public:42void* operator new(size_t size) throw();43void operator delete(void* p);44void* new_array(size_t size);45};4647// Base class for classes that constitute name spaces.4849class AllStatic {50public:51void* operator new(size_t size) throw();52void operator delete(void* p);53};545556//------------------------------Chunk------------------------------------------57// Linked list of raw memory chunks58class Chunk: public CHeapObj {59private:60// This ordinary operator delete is needed even though not used, so the61// below two-argument operator delete will be treated as a placement62// delete rather than an ordinary sized delete; see C++14 3.7.4.2/p2.63void operator delete(void* p);64public:65void* operator new(size_t size, size_t length) throw();66void operator delete(void* p, size_t length);67Chunk(size_t length);6869enum {70init_size = 1*1024, // Size of first chunk71size = 32*1024 // Default size of an Arena chunk (following the first)72};73Chunk* _next; // Next Chunk in list74size_t _len; // Size of this Chunk7576void chop(); // Chop this chunk77void next_chop(); // Chop next chunk7879// Boundaries of data area (possibly unused)80char* bottom() const { return ((char*) this) + sizeof(Chunk); }81char* top() const { return bottom() + _len; }82};838485//------------------------------Arena------------------------------------------86// Fast allocation of memory87class Arena: public CHeapObj {88protected:89friend class ResourceMark;90friend class HandleMark;91friend class NoHandleMark;92Chunk *_first; // First chunk93Chunk *_chunk; // current chunk94char *_hwm, *_max; // High water mark and max in current chunk95void* grow(size_t x); // Get a new Chunk of at least size x96size_t _size_in_bytes; // Size of arena (used for memory usage tracing)97public:98Arena();99Arena(size_t init_size);100Arena(Arena *old);101~Arena() { _first->chop(); }102char* hwm() const { return _hwm; }103104// Fast allocate in the arena. Common case is: pointer test + increment.105void* Amalloc(size_t x) {106#ifdef _LP64107x = (x + (8-1)) & ((unsigned)(-8));108#else109x = (x + (4-1)) & ((unsigned)(-4));110#endif111if (_hwm + x > _max) {112return grow(x);113} else {114char *old = _hwm;115_hwm += x;116return old;117}118}119// Further assume size is padded out to words120// Warning: in LP64, Amalloc_4 is really Amalloc_8121void *Amalloc_4(size_t x) {122assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );123if (_hwm + x > _max) {124return grow(x);125} else {126char *old = _hwm;127_hwm += x;128return old;129}130}131132// Fast delete in area. Common case is: NOP (except for storage reclaimed)133void Afree(void *ptr, size_t size) {134if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;135}136137void *Acalloc( size_t items, size_t x );138void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );139140// Reset this Arena to empty, and return this Arenas guts in a new Arena.141Arena *reset(void);142143// Determine if pointer belongs to this Arena or not.144bool contains( const void *ptr ) const;145146// Total of all chunks in use (not thread-safe)147size_t used() const;148149// Total # of bytes used150size_t size_in_bytes() const { return _size_in_bytes; }151void set_size_in_bytes(size_t size) { _size_in_bytes = size; }152};153154#endif // SHARE_ADLC_ARENA_HPP155156157