1 /*
   2  * Copyright (c) 2017, 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_MEMORY_ARENA_HPP
  26 #define SHARE_MEMORY_ARENA_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "runtime/globals.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 #include "utilities/powerOfTwo.hpp"
  32 
  33 #include <new>
  34 
  35 // The byte alignment to be used by Arena::Amalloc.  See bugid 4169348.
  36 // Note: this value must be a power of 2
  37 
  38 #define ARENA_AMALLOC_ALIGNMENT (2*BytesPerWord)
  39 
  40 #define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1)
  41 #define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1))
  42 #define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK)
  43 
  44 //------------------------------Chunk------------------------------------------
  45 // Linked list of raw memory chunks
  46 class Chunk: CHeapObj<mtChunk> {
  47 
  48  private:
  49   Chunk*       _next;     // Next Chunk in list
  50   const size_t _len;      // Size of this Chunk
  51  public:
  52   void* operator new(size_t size, AllocFailType alloc_failmode, size_t length) throw();
  53   void  operator delete(void* p);
  54   Chunk(size_t length);
  55 
  56   enum {
  57     // default sizes; make them slightly smaller than 2**k to guard against
  58     // buddy-system style malloc implementations
  59 #ifdef _LP64
  60     slack      = 40,            // [RGV] Not sure if this is right, but make it
  61                                 //       a multiple of 8.
  62 #else
  63     slack      = 20,            // suspected sizeof(Chunk) + internal malloc headers
  64 #endif
  65 
  66     tiny_size  =  256  - slack, // Size of first chunk (tiny)
  67     init_size  =  1*K  - slack, // Size of first chunk (normal aka small)
  68     medium_size= 10*K  - slack, // Size of medium-sized chunk
  69     size       = 32*K  - slack, // Default size of an Arena chunk (following the first)
  70     non_pool_size = init_size + 32 // An initial size which is not one of above
  71   };
  72 
  73   void chop();                  // Chop this chunk
  74   void next_chop();             // Chop next chunk
  75   static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); }
  76   static size_t aligned_overhead_size(size_t byte_size) { return ARENA_ALIGN(byte_size); }
  77 
  78   size_t length() const         { return _len;  }
  79   Chunk* next() const           { return _next;  }
  80   void set_next(Chunk* n)       { _next = n;  }
  81   // Boundaries of data area (possibly unused)
  82   char* bottom() const          { return ((char*) this) + aligned_overhead_size();  }
  83   char* top()    const          { return bottom() + _len; }
  84   bool contains(char* p) const  { return bottom() <= p && p <= top(); }
  85 
  86   // Start the chunk_pool cleaner task
  87   static void start_chunk_pool_cleaner_task();
  88 
  89   static void clean_chunk_pool();
  90 };
  91 
  92 //------------------------------Arena------------------------------------------
  93 // Fast allocation of memory
  94 class Arena : public CHeapObj<mtNone> {
  95 protected:
  96   friend class ResourceMark;
  97   friend class HandleMark;
  98   friend class NoHandleMark;
  99   friend class VMStructs;
 100 
 101   MEMFLAGS    _flags;           // Memory tracking flags
 102 
 103   Chunk *_first;                // First chunk
 104   Chunk *_chunk;                // current chunk
 105   char *_hwm, *_max;            // High water mark and max in current chunk
 106   // Get a new Chunk of at least size x
 107   void* grow(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
 108   size_t _size_in_bytes;        // Size of arena (used for native memory tracking)
 109 
 110   debug_only(void* malloc(size_t size);)
 111   debug_only(void* internal_malloc_4(size_t x);)
 112 
 113   void signal_out_of_memory(size_t request, const char* whence) const;
 114 
 115   bool check_for_overflow(size_t request, const char* whence,
 116       AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) const {
 117     if (UINTPTR_MAX - request < (uintptr_t)_hwm) {
 118       if (alloc_failmode == AllocFailStrategy::RETURN_NULL) {
 119         return false;
 120       }
 121       signal_out_of_memory(request, whence);
 122     }
 123     return true;
 124  }
 125 
 126  public:
 127   Arena(MEMFLAGS memflag);
 128   Arena(MEMFLAGS memflag, size_t init_size);
 129   ~Arena();
 130   void  destruct_contents();
 131   char* hwm() const             { return _hwm; }
 132 
 133   // new operators
 134   void* operator new (size_t size) throw();
 135   void* operator new (size_t size, const std::nothrow_t& nothrow_constant) throw();
 136 
 137   // dynamic memory type tagging
 138   void* operator new(size_t size, MEMFLAGS flags) throw();
 139   void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags) throw();
 140   void  operator delete(void* p);
 141 
 142   // Fast allocate in the arena.  Common case is: pointer test + increment.
 143   void* Amalloc(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
 144     assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");
 145     x = ARENA_ALIGN(x);
 146     debug_only(if (UseMallocOnly) return malloc(x);)
 147     if (!check_for_overflow(x, "Arena::Amalloc", alloc_failmode))
 148       return NULL;
 149     if (_hwm + x > _max) {
 150       return grow(x, alloc_failmode);
 151     } else {
 152       char *old = _hwm;
 153       _hwm += x;
 154       return old;
 155     }
 156   }
 157   // Further assume size is padded out to words
 158   void *Amalloc_4(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
 159     assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
 160     debug_only(if (UseMallocOnly) return malloc(x);)
 161     if (!check_for_overflow(x, "Arena::Amalloc_4", alloc_failmode))
 162       return NULL;
 163     if (_hwm + x > _max) {
 164       return grow(x, alloc_failmode);
 165     } else {
 166       char *old = _hwm;
 167       _hwm += x;
 168       return old;
 169     }
 170   }
 171 
 172   // Allocate with 'double' alignment. It is 8 bytes on sparc.
 173   // In other cases Amalloc_D() should be the same as Amalloc_4().
 174   void* Amalloc_D(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
 175     assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
 176     debug_only(if (UseMallocOnly) return malloc(x);)
 177     if (!check_for_overflow(x, "Arena::Amalloc_D", alloc_failmode))
 178       return NULL;
 179     if (_hwm + x > _max) {
 180       return grow(x, alloc_failmode); // grow() returns a result aligned >= 8 bytes.
 181     } else {
 182       char *old = _hwm;
 183       _hwm += x;
 184       return old;
 185     }
 186   }
 187 
 188   // Fast delete in area.  Common case is: NOP (except for storage reclaimed)
 189   bool Afree(void *ptr, size_t size) {
 190 #ifdef ASSERT
 191     if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory
 192     if (UseMallocOnly) return true;
 193 #endif
 194     if (((char*)ptr) + size == _hwm) {
 195       _hwm = (char*)ptr;
 196       return true;
 197     } else {
 198       // Unable to fast free, so we just drop it.
 199       return false;
 200     }
 201   }
 202 
 203   void *Arealloc( void *old_ptr, size_t old_size, size_t new_size,
 204       AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
 205 
 206   // Move contents of this arena into an empty arena
 207   Arena *move_contents(Arena *empty_arena);
 208 
 209   // Determine if pointer belongs to this Arena or not.
 210   bool contains( const void *ptr ) const;
 211 
 212   // Total of all chunks in use (not thread-safe)
 213   size_t used() const;
 214 
 215   // Total # of bytes used
 216   size_t size_in_bytes() const         {  return _size_in_bytes; };
 217   void set_size_in_bytes(size_t size);
 218 
 219   static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2)  PRODUCT_RETURN;
 220   static void free_all(char** start, char** end)                                     PRODUCT_RETURN;
 221 
 222 private:
 223   // Reset this Arena to empty, access will trigger grow if necessary
 224   void   reset(void) {
 225     _first = _chunk = NULL;
 226     _hwm = _max = NULL;
 227     set_size_in_bytes(0);
 228   }
 229 };
 230 
 231 // One of the following macros must be used when allocating
 232 // an array or object from an arena
 233 #define NEW_ARENA_ARRAY(arena, type, size) \
 234   (type*) (arena)->Amalloc((size) * sizeof(type))
 235 
 236 #define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size)    \
 237   (type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \
 238                             (new_size) * sizeof(type) )
 239 
 240 #define FREE_ARENA_ARRAY(arena, type, old, size) \
 241   (arena)->Afree((char*)(old), (size) * sizeof(type))
 242 
 243 #define NEW_ARENA_OBJ(arena, type) \
 244   NEW_ARENA_ARRAY(arena, type, 1)
 245 
 246 #endif // SHARE_MEMORY_ARENA_HPP