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