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