allocation.hpp revision 3602:da91efe96a93
1/*
2 * Copyright (c) 1997, 2012, 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_MEMORY_ALLOCATION_HPP
26#define SHARE_VM_MEMORY_ALLOCATION_HPP
27
28#include "runtime/globals.hpp"
29#include "utilities/globalDefinitions.hpp"
30#ifdef COMPILER1
31#include "c1/c1_globals.hpp"
32#endif
33#ifdef COMPILER2
34#include "opto/c2_globals.hpp"
35#endif
36
37#include <new>
38
39#define ARENA_ALIGN_M1 (((size_t)(ARENA_AMALLOC_ALIGNMENT)) - 1)
40#define ARENA_ALIGN_MASK (~((size_t)ARENA_ALIGN_M1))
41#define ARENA_ALIGN(x) ((((size_t)(x)) + ARENA_ALIGN_M1) & ARENA_ALIGN_MASK)
42
43
44// noinline attribute
45#ifdef _WINDOWS
46  #define _NOINLINE_  __declspec(noinline)
47#else
48  #if __GNUC__ < 3    // gcc 2.x does not support noinline attribute
49    #define _NOINLINE_
50  #else
51    #define _NOINLINE_ __attribute__ ((noinline))
52  #endif
53#endif
54
55// All classes in the virtual machine must be subclassed
56// by one of the following allocation classes:
57//
58// For objects allocated in the resource area (see resourceArea.hpp).
59// - ResourceObj
60//
61// For objects allocated in the C-heap (managed by: free & malloc).
62// - CHeapObj
63//
64// For objects allocated on the stack.
65// - StackObj
66//
67// For embedded objects.
68// - ValueObj
69//
70// For classes used as name spaces.
71// - AllStatic
72//
73// For classes in Metaspace (class data)
74// - MetaspaceObj
75//
76// The printable subclasses are used for debugging and define virtual
77// member functions for printing. Classes that avoid allocating the
78// vtbl entries in the objects should therefore not be the printable
79// subclasses.
80//
81// The following macros and function should be used to allocate memory
82// directly in the resource area or in the C-heap:
83//
84//   NEW_RESOURCE_ARRAY(type,size)
85//   NEW_RESOURCE_OBJ(type)
86//   NEW_C_HEAP_ARRAY(type,size)
87//   NEW_C_HEAP_OBJ(type)
88//   char* AllocateHeap(size_t size, const char* name);
89//   void  FreeHeap(void* p);
90//
91// C-heap allocation can be traced using +PrintHeapAllocation.
92// malloc and free should therefore never called directly.
93
94// Base class for objects allocated in the C-heap.
95
96// In non product mode we introduce a super class for all allocation classes
97// that supports printing.
98// We avoid the superclass in product mode since some C++ compilers add
99// a word overhead for empty super classes.
100
101#ifdef PRODUCT
102#define ALLOCATION_SUPER_CLASS_SPEC
103#else
104#define ALLOCATION_SUPER_CLASS_SPEC : public AllocatedObj
105class AllocatedObj {
106 public:
107  // Printing support
108  void print() const;
109  void print_value() const;
110
111  virtual void print_on(outputStream* st) const;
112  virtual void print_value_on(outputStream* st) const;
113};
114#endif
115
116
117/*
118 * MemoryType bitmap layout:
119 * | 16 15 14 13 12 11 10 09 | 08 07 06 05 | 04 03 02 01 |
120 * |      memory type        |   object    | reserved    |
121 * |                         |     type    |             |
122 */
123enum MemoryType {
124  // Memory type by sub systems. It occupies lower byte.
125  mtNone              = 0x0000,  // undefined
126  mtClass             = 0x0100,  // memory class for Java classes
127  mtThread            = 0x0200,  // memory for thread objects
128  mtThreadStack       = 0x0300,
129  mtCode              = 0x0400,  // memory for generated code
130  mtGC                = 0x0500,  // memory for GC
131  mtCompiler          = 0x0600,  // memory for compiler
132  mtInternal          = 0x0700,  // memory used by VM, but does not belong to
133                                 // any of above categories, and not used for
134                                 // native memory tracking
135  mtOther             = 0x0800,  // memory not used by VM
136  mtSymbol            = 0x0900,  // symbol
137  mtNMT               = 0x0A00,  // memory used by native memory tracking
138  mtChunk             = 0x0B00,  // chunk that holds content of arenas
139  mtJavaHeap          = 0x0C00,  // Java heap
140  mtDontTrack         = 0x0D00,  // memory we donot or cannot track
141  mt_number_of_types  = 0x000C,  // number of memory types
142  mt_masks            = 0x7F00,
143
144  // object type mask
145  otArena             = 0x0010, // an arena object
146  otNMTRecorder       = 0x0020, // memory recorder object
147  ot_masks            = 0x00F0
148};
149
150#define IS_MEMORY_TYPE(flags, type) ((flags & mt_masks) == type)
151#define HAS_VALID_MEMORY_TYPE(flags)((flags & mt_masks) != mtNone)
152#define FLAGS_TO_MEMORY_TYPE(flags) (flags & mt_masks)
153
154#define IS_ARENA_OBJ(flags)         ((flags & ot_masks) == otArena)
155#define IS_NMT_RECORDER(flags)      ((flags & ot_masks) == otNMTRecorder)
156#define NMT_CAN_TRACK(flags)        (!IS_NMT_RECORDER(flags) && !(IS_MEMORY_TYPE(flags, mtDontTrack)))
157
158typedef unsigned short MEMFLAGS;
159
160extern bool NMT_track_callsite;
161
162// debug build does not inline
163#if defined(_DEBUG_)
164  #define CURRENT_PC       (NMT_track_callsite ? os::get_caller_pc(1) : 0)
165  #define CALLER_PC        (NMT_track_callsite ? os::get_caller_pc(2) : 0)
166  #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(3) : 0)
167#else
168  #define CURRENT_PC      (NMT_track_callsite? os::get_caller_pc(0) : 0)
169  #define CALLER_PC       (NMT_track_callsite ? os::get_caller_pc(1) : 0)
170  #define CALLER_CALLER_PC (NMT_track_callsite ? os::get_caller_pc(2) : 0)
171#endif
172
173
174
175template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
176 public:
177  _NOINLINE_ void* operator new(size_t size, address caller_pc = 0);
178  _NOINLINE_ void* operator new (size_t size, const std::nothrow_t&  nothrow_constant,
179                               address caller_pc = 0);
180
181  void  operator delete(void* p);
182};
183
184// Base class for objects allocated on the stack only.
185// Calling new or delete will result in fatal error.
186
187class StackObj ALLOCATION_SUPER_CLASS_SPEC {
188 public:
189  void* operator new(size_t size);
190  void  operator delete(void* p);
191};
192
193// Base class for objects used as value objects.
194// Calling new or delete will result in fatal error.
195//
196// Portability note: Certain compilers (e.g. gcc) will
197// always make classes bigger if it has a superclass, even
198// if the superclass does not have any virtual methods or
199// instance fields. The HotSpot implementation relies on this
200// not to happen. So never make a ValueObj class a direct subclass
201// of this object, but use the VALUE_OBJ_CLASS_SPEC class instead, e.g.,
202// like this:
203//
204//   class A VALUE_OBJ_CLASS_SPEC {
205//     ...
206//   }
207//
208// With gcc and possible other compilers the VALUE_OBJ_CLASS_SPEC can
209// be defined as a an empty string "".
210//
211class _ValueObj {
212 public:
213  void* operator new(size_t size);
214  void operator delete(void* p);
215};
216
217
218// Base class for objects stored in Metaspace.
219// Calling delete will result in fatal error.
220//
221// Do not inherit from something with a vptr because this class does
222// not introduce one.  This class is used to allocate both shared read-only
223// and shared read-write classes.
224//
225
226class ClassLoaderData;
227
228class MetaspaceObj {
229 public:
230  bool is_metadata() const;
231  bool is_shared() const;
232  void print_address_on(outputStream* st) const;  // nonvirtual address printing
233
234  void* operator new(size_t size, ClassLoaderData* loader_data,
235                     size_t word_size, bool read_only, Thread* thread);
236                     // can't use TRAPS from this header file.
237  void operator delete(void* p) { ShouldNotCallThis(); }
238};
239
240// Base class for classes that constitute name spaces.
241
242class AllStatic {
243 public:
244  AllStatic()  { ShouldNotCallThis(); }
245  ~AllStatic() { ShouldNotCallThis(); }
246};
247
248
249//------------------------------Chunk------------------------------------------
250// Linked list of raw memory chunks
251class Chunk: CHeapObj<mtChunk> {
252  friend class VMStructs;
253
254 protected:
255  Chunk*       _next;     // Next Chunk in list
256  const size_t _len;      // Size of this Chunk
257 public:
258  void* operator new(size_t size, size_t length);
259  void  operator delete(void* p);
260  Chunk(size_t length);
261
262  enum {
263    // default sizes; make them slightly smaller than 2**k to guard against
264    // buddy-system style malloc implementations
265#ifdef _LP64
266    slack      = 40,            // [RGV] Not sure if this is right, but make it
267                                //       a multiple of 8.
268#else
269    slack      = 20,            // suspected sizeof(Chunk) + internal malloc headers
270#endif
271
272    init_size  =  1*K  - slack, // Size of first chunk
273    medium_size= 10*K  - slack, // Size of medium-sized chunk
274    size       = 32*K  - slack, // Default size of an Arena chunk (following the first)
275    non_pool_size = init_size + 32 // An initial size which is not one of above
276  };
277
278  void chop();                  // Chop this chunk
279  void next_chop();             // Chop next chunk
280  static size_t aligned_overhead_size(void) { return ARENA_ALIGN(sizeof(Chunk)); }
281  static size_t aligned_overhead_size(size_t byte_size) { return ARENA_ALIGN(byte_size); }
282
283  size_t length() const         { return _len;  }
284  Chunk* next() const           { return _next;  }
285  void set_next(Chunk* n)       { _next = n;  }
286  // Boundaries of data area (possibly unused)
287  char* bottom() const          { return ((char*) this) + aligned_overhead_size();  }
288  char* top()    const          { return bottom() + _len; }
289  bool contains(char* p) const  { return bottom() <= p && p <= top(); }
290
291  // Start the chunk_pool cleaner task
292  static void start_chunk_pool_cleaner_task();
293
294  static void clean_chunk_pool();
295};
296
297//------------------------------Arena------------------------------------------
298// Fast allocation of memory
299class Arena : public CHeapObj<mtNone|otArena> {
300protected:
301  friend class ResourceMark;
302  friend class HandleMark;
303  friend class NoHandleMark;
304  friend class VMStructs;
305
306  Chunk *_first;                // First chunk
307  Chunk *_chunk;                // current chunk
308  char *_hwm, *_max;            // High water mark and max in current chunk
309  void* grow(size_t x);         // Get a new Chunk of at least size x
310  size_t _size_in_bytes;        // Size of arena (used for native memory tracking)
311
312  NOT_PRODUCT(static julong _bytes_allocated;) // total #bytes allocated since start
313  friend class AllocStats;
314  debug_only(void* malloc(size_t size);)
315  debug_only(void* internal_malloc_4(size_t x);)
316  NOT_PRODUCT(void inc_bytes_allocated(size_t x);)
317
318  void signal_out_of_memory(size_t request, const char* whence) const;
319
320  void check_for_overflow(size_t request, const char* whence) const {
321    if (UINTPTR_MAX - request < (uintptr_t)_hwm) {
322      signal_out_of_memory(request, whence);
323    }
324 }
325
326 public:
327  Arena();
328  Arena(size_t init_size);
329  Arena(Arena *old);
330  ~Arena();
331  void  destruct_contents();
332  char* hwm() const             { return _hwm; }
333
334  // new operators
335  void* operator new (size_t size);
336  void* operator new (size_t size, const std::nothrow_t& nothrow_constant);
337
338  // dynamic memory type tagging
339  void* operator new(size_t size, MEMFLAGS flags);
340  void* operator new(size_t size, const std::nothrow_t& nothrow_constant, MEMFLAGS flags);
341  void  operator delete(void* p);
342
343  // Fast allocate in the arena.  Common case is: pointer test + increment.
344  void* Amalloc(size_t x) {
345    assert(is_power_of_2(ARENA_AMALLOC_ALIGNMENT) , "should be a power of 2");
346    x = ARENA_ALIGN(x);
347    debug_only(if (UseMallocOnly) return malloc(x);)
348    check_for_overflow(x, "Arena::Amalloc");
349    NOT_PRODUCT(inc_bytes_allocated(x);)
350    if (_hwm + x > _max) {
351      return grow(x);
352    } else {
353      char *old = _hwm;
354      _hwm += x;
355      return old;
356    }
357  }
358  // Further assume size is padded out to words
359  void *Amalloc_4(size_t x) {
360    assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
361    debug_only(if (UseMallocOnly) return malloc(x);)
362    check_for_overflow(x, "Arena::Amalloc_4");
363    NOT_PRODUCT(inc_bytes_allocated(x);)
364    if (_hwm + x > _max) {
365      return grow(x);
366    } else {
367      char *old = _hwm;
368      _hwm += x;
369      return old;
370    }
371  }
372
373  // Allocate with 'double' alignment. It is 8 bytes on sparc.
374  // In other cases Amalloc_D() should be the same as Amalloc_4().
375  void* Amalloc_D(size_t x) {
376    assert( (x&(sizeof(char*)-1)) == 0, "misaligned size" );
377    debug_only(if (UseMallocOnly) return malloc(x);)
378#if defined(SPARC) && !defined(_LP64)
379#define DALIGN_M1 7
380    size_t delta = (((size_t)_hwm + DALIGN_M1) & ~DALIGN_M1) - (size_t)_hwm;
381    x += delta;
382#endif
383    check_for_overflow(x, "Arena::Amalloc_D");
384    NOT_PRODUCT(inc_bytes_allocated(x);)
385    if (_hwm + x > _max) {
386      return grow(x); // grow() returns a result aligned >= 8 bytes.
387    } else {
388      char *old = _hwm;
389      _hwm += x;
390#if defined(SPARC) && !defined(_LP64)
391      old += delta; // align to 8-bytes
392#endif
393      return old;
394    }
395  }
396
397  // Fast delete in area.  Common case is: NOP (except for storage reclaimed)
398  void Afree(void *ptr, size_t size) {
399#ifdef ASSERT
400    if (ZapResourceArea) memset(ptr, badResourceValue, size); // zap freed memory
401    if (UseMallocOnly) return;
402#endif
403    if (((char*)ptr) + size == _hwm) _hwm = (char*)ptr;
404  }
405
406  void *Arealloc( void *old_ptr, size_t old_size, size_t new_size );
407
408  // Move contents of this arena into an empty arena
409  Arena *move_contents(Arena *empty_arena);
410
411  // Determine if pointer belongs to this Arena or not.
412  bool contains( const void *ptr ) const;
413
414  // Total of all chunks in use (not thread-safe)
415  size_t used() const;
416
417  // Total # of bytes used
418  size_t size_in_bytes() const         {  return _size_in_bytes; };
419  void set_size_in_bytes(size_t size);
420
421  static void free_malloced_objects(Chunk* chunk, char* hwm, char* max, char* hwm2)  PRODUCT_RETURN;
422  static void free_all(char** start, char** end)                                     PRODUCT_RETURN;
423
424  // how many arena instances
425  NOT_PRODUCT(static volatile jint _instance_count;)
426private:
427  // Reset this Arena to empty, access will trigger grow if necessary
428  void   reset(void) {
429    _first = _chunk = NULL;
430    _hwm = _max = NULL;
431    set_size_in_bytes(0);
432  }
433};
434
435// One of the following macros must be used when allocating
436// an array or object from an arena
437#define NEW_ARENA_ARRAY(arena, type, size) \
438  (type*) (arena)->Amalloc((size) * sizeof(type))
439
440#define REALLOC_ARENA_ARRAY(arena, type, old, old_size, new_size)    \
441  (type*) (arena)->Arealloc((char*)(old), (old_size) * sizeof(type), \
442                            (new_size) * sizeof(type) )
443
444#define FREE_ARENA_ARRAY(arena, type, old, size) \
445  (arena)->Afree((char*)(old), (size) * sizeof(type))
446
447#define NEW_ARENA_OBJ(arena, type) \
448  NEW_ARENA_ARRAY(arena, type, 1)
449
450
451//%note allocation_1
452extern char* resource_allocate_bytes(size_t size);
453extern char* resource_allocate_bytes(Thread* thread, size_t size);
454extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size);
455extern void resource_free_bytes( char *old, size_t size );
456
457//----------------------------------------------------------------------
458// Base class for objects allocated in the resource area per default.
459// Optionally, objects may be allocated on the C heap with
460// new(ResourceObj::C_HEAP) Foo(...) or in an Arena with new (&arena)
461// ResourceObj's can be allocated within other objects, but don't use
462// new or delete (allocation_type is unknown).  If new is used to allocate,
463// use delete to deallocate.
464class ResourceObj ALLOCATION_SUPER_CLASS_SPEC {
465 public:
466  enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };
467  static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN;
468#ifdef ASSERT
469 private:
470  // When this object is allocated on stack the new() operator is not
471  // called but garbage on stack may look like a valid allocation_type.
472  // Store negated 'this' pointer when new() is called to distinguish cases.
473  // Use second array's element for verification value to distinguish garbage.
474  uintptr_t _allocation_t[2];
475  bool is_type_set() const;
476 public:
477  allocation_type get_allocation_type() const;
478  bool allocated_on_stack()    const { return get_allocation_type() == STACK_OR_EMBEDDED; }
479  bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; }
480  bool allocated_on_C_heap()   const { return get_allocation_type() == C_HEAP; }
481  bool allocated_on_arena()    const { return get_allocation_type() == ARENA; }
482  ResourceObj(); // default construtor
483  ResourceObj(const ResourceObj& r); // default copy construtor
484  ResourceObj& operator=(const ResourceObj& r); // default copy assignment
485  ~ResourceObj();
486#endif // ASSERT
487
488 public:
489  void* operator new(size_t size, allocation_type type, MEMFLAGS flags);
490  void* operator new(size_t size, Arena *arena) {
491      address res = (address)arena->Amalloc(size);
492      DEBUG_ONLY(set_allocation_type(res, ARENA);)
493      return res;
494  }
495  void* operator new(size_t size) {
496      address res = (address)resource_allocate_bytes(size);
497      DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
498      return res;
499  }
500  void  operator delete(void* p);
501};
502
503// One of the following macros must be used when allocating an array
504// or object to determine whether it should reside in the C heap on in
505// the resource area.
506
507#define NEW_RESOURCE_ARRAY(type, size)\
508  (type*) resource_allocate_bytes((size) * sizeof(type))
509
510#define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
511  (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
512
513#define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
514  (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type) )
515
516#define FREE_RESOURCE_ARRAY(type, old, size)\
517  resource_free_bytes((char*)(old), (size) * sizeof(type))
518
519#define FREE_FAST(old)\
520    /* nop */
521
522#define NEW_RESOURCE_OBJ(type)\
523  NEW_RESOURCE_ARRAY(type, 1)
524
525#define NEW_C_HEAP_ARRAY(type, size, memflags)\
526  (type*) (AllocateHeap((size) * sizeof(type), memflags))
527
528#define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
529  (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags))
530
531#define FREE_C_HEAP_ARRAY(type,old,memflags) \
532  FreeHeap((char*)(old), memflags)
533
534#define NEW_C_HEAP_OBJ(type, memflags)\
535  NEW_C_HEAP_ARRAY(type, 1, memflags)
536
537
538#define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
539  (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
540
541#define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\
542  (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc))
543
544#define NEW_C_HEAP_OBJ2(type, memflags, pc)\
545  NEW_C_HEAP_ARRAY2(type, 1, memflags, pc)
546
547
548extern bool warn_new_operator;
549
550// for statistics
551#ifndef PRODUCT
552class AllocStats : StackObj {
553  julong start_mallocs, start_frees;
554  julong start_malloc_bytes, start_mfree_bytes, start_res_bytes;
555 public:
556  AllocStats();
557
558  julong num_mallocs();    // since creation of receiver
559  julong alloc_bytes();
560  julong num_frees();
561  julong free_bytes();
562  julong resource_bytes();
563  void   print();
564};
565#endif
566
567
568//------------------------------ReallocMark---------------------------------
569// Code which uses REALLOC_RESOURCE_ARRAY should check an associated
570// ReallocMark, which is declared in the same scope as the reallocated
571// pointer.  Any operation that could __potentially__ cause a reallocation
572// should check the ReallocMark.
573class ReallocMark: public StackObj {
574protected:
575  NOT_PRODUCT(int _nesting;)
576
577public:
578  ReallocMark()   PRODUCT_RETURN;
579  void check()    PRODUCT_RETURN;
580};
581
582#endif // SHARE_VM_MEMORY_ALLOCATION_HPP
583