src/share/vm/memory/allocation.hpp

changeset 4962
6f817ce50129
parent 4901
83f27710f5f7
child 4964
5b6512efcdc4
child 4967
5a9fa2ba85f0
equal deleted inserted replaced
4961:7815eaceaa8c 4962:6f817ce50129
1 /* 1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
84 // member functions for printing. Classes that avoid allocating the 84 // member functions for printing. Classes that avoid allocating the
85 // vtbl entries in the objects should therefore not be the printable 85 // vtbl entries in the objects should therefore not be the printable
86 // subclasses. 86 // subclasses.
87 // 87 //
88 // The following macros and function should be used to allocate memory 88 // The following macros and function should be used to allocate memory
89 // directly in the resource area or in the C-heap: 89 // directly in the resource area or in the C-heap, The _OBJECT variants
90 // of the NEW_C_HEAP macros are used when a constructor and destructor
91 // must be invoked for the object(s) and the objects are not inherited
92 // from CHeapObj. The preferable way to allocate objects is using the
93 // new operator.
94 //
95 // WARNING: The array variant must only be used for a homogenous array
96 // where all objects are of the exact type specified. If subtypes are
97 // stored in the array then the incorrect destructor might be called.
90 // 98 //
91 // NEW_RESOURCE_ARRAY(type,size) 99 // NEW_RESOURCE_ARRAY(type,size)
92 // NEW_RESOURCE_OBJ(type) 100 // NEW_RESOURCE_OBJ(type)
93 // NEW_C_HEAP_ARRAY(type,size) 101 // NEW_C_HEAP_ARRAY(type,size)
94 // NEW_C_HEAP_OBJ(type) 102 // NEW_C_HEAP_OBJ(type)
103 // NEW_C_HEAP_OBJECT(type, memflags, pc, allocfail)
104 // NEW_C_HEAP_OBJECT_ARRAY(type, size, memflags, pc, allocfail)
105 // FREE_C_HEAP_OBJECT(type, objname, memflags)
106 // FREE_C_HEAP_OBJECT_ARRAY(type, size, arrayname, memflags)
95 // char* AllocateHeap(size_t size, const char* name); 107 // char* AllocateHeap(size_t size, const char* name);
96 // void FreeHeap(void* p); 108 // void FreeHeap(void* p);
97 // 109 //
98 // C-heap allocation can be traced using +PrintHeapAllocation. 110 // C-heap allocation can be traced using +PrintHeapAllocation.
99 // malloc and free should therefore never called directly. 111 // malloc and free should therefore never called directly.
193 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { 205 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC {
194 public: 206 public:
195 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0); 207 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0);
196 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant, 208 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant,
197 address caller_pc = 0); 209 address caller_pc = 0);
198 210 _NOINLINE_ void* operator new [](size_t size, address caller_pc = 0);
211 _NOINLINE_ void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
212 address caller_pc = 0);
199 void operator delete(void* p); 213 void operator delete(void* p);
214 void operator delete [] (void* p);
200 }; 215 };
201 216
202 // Base class for objects allocated on the stack only. 217 // Base class for objects allocated on the stack only.
203 // Calling new or delete will result in fatal error. 218 // Calling new or delete will result in fatal error.
204 219
205 class StackObj ALLOCATION_SUPER_CLASS_SPEC { 220 class StackObj ALLOCATION_SUPER_CLASS_SPEC {
206 private: 221 private:
207 void* operator new(size_t size); 222 void* operator new(size_t size);
208 void operator delete(void* p); 223 void operator delete(void* p);
224 void* operator new [](size_t size);
225 void operator delete [](void* p);
209 }; 226 };
210 227
211 // Base class for objects used as value objects. 228 // Base class for objects used as value objects.
212 // Calling new or delete will result in fatal error. 229 // Calling new or delete will result in fatal error.
213 // 230 //
227 // be defined as a an empty string "". 244 // be defined as a an empty string "".
228 // 245 //
229 class _ValueObj { 246 class _ValueObj {
230 private: 247 private:
231 void* operator new(size_t size); 248 void* operator new(size_t size);
232 void operator delete(void* p); 249 void operator delete(void* p);
250 void* operator new [](size_t size);
251 void operator delete [](void* p);
233 }; 252 };
234 253
235 254
236 // Base class for objects stored in Metaspace. 255 // Base class for objects stored in Metaspace.
237 // Calling delete will result in fatal error. 256 // Calling delete will result in fatal error.
508 ~ResourceObj(); 527 ~ResourceObj();
509 #endif // ASSERT 528 #endif // ASSERT
510 529
511 public: 530 public:
512 void* operator new(size_t size, allocation_type type, MEMFLAGS flags); 531 void* operator new(size_t size, allocation_type type, MEMFLAGS flags);
532 void* operator new [](size_t size, allocation_type type, MEMFLAGS flags);
513 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, 533 void* operator new(size_t size, const std::nothrow_t& nothrow_constant,
514 allocation_type type, MEMFLAGS flags); 534 allocation_type type, MEMFLAGS flags);
535 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant,
536 allocation_type type, MEMFLAGS flags);
537
515 void* operator new(size_t size, Arena *arena) { 538 void* operator new(size_t size, Arena *arena) {
516 address res = (address)arena->Amalloc(size); 539 address res = (address)arena->Amalloc(size);
517 DEBUG_ONLY(set_allocation_type(res, ARENA);) 540 DEBUG_ONLY(set_allocation_type(res, ARENA);)
518 return res; 541 return res;
519 } 542 }
543
544 void* operator new [](size_t size, Arena *arena) {
545 address res = (address)arena->Amalloc(size);
546 DEBUG_ONLY(set_allocation_type(res, ARENA);)
547 return res;
548 }
549
520 void* operator new(size_t size) { 550 void* operator new(size_t size) {
521 address res = (address)resource_allocate_bytes(size); 551 address res = (address)resource_allocate_bytes(size);
522 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) 552 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
523 return res; 553 return res;
524 } 554 }
527 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL); 557 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
528 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);) 558 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
529 return res; 559 return res;
530 } 560 }
531 561
562 void* operator new [](size_t size) {
563 address res = (address)resource_allocate_bytes(size);
564 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
565 return res;
566 }
567
568 void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) {
569 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
570 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);)
571 return res;
572 }
573
532 void operator delete(void* p); 574 void operator delete(void* p);
575 void operator delete [](void* p);
533 }; 576 };
534 577
535 // One of the following macros must be used when allocating an array 578 // One of the following macros must be used when allocating an array
536 // or object to determine whether it should reside in the C heap on in 579 // or object to determine whether it should reside in the C heap on in
537 // the resource area. 580 // the resource area.
558 (type*) (AllocateHeap((size) * sizeof(type), memflags)) 601 (type*) (AllocateHeap((size) * sizeof(type), memflags))
559 602
560 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\ 603 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
561 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags)) 604 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags))
562 605
563 #define FREE_C_HEAP_ARRAY(type,old,memflags) \ 606 #define FREE_C_HEAP_ARRAY(type, old, memflags) \
564 FreeHeap((char*)(old), memflags) 607 FreeHeap((char*)(old), memflags)
565 608
609 // allocate type in heap without calling ctor
610 // WARNING: type must not have virtual functions!!! There is no way to initialize vtable.
566 #define NEW_C_HEAP_OBJ(type, memflags)\ 611 #define NEW_C_HEAP_OBJ(type, memflags)\
567 NEW_C_HEAP_ARRAY(type, 1, memflags) 612 NEW_C_HEAP_ARRAY(type, 1, memflags)
568 613
569
570 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\ 614 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
571 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc)) 615 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
572 616
573 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\ 617 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\
574 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc)) 618 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc))
575 619
576 #define NEW_C_HEAP_OBJ2(type, memflags, pc)\ 620 #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail) \
577 NEW_C_HEAP_ARRAY2(type, 1, memflags, pc) 621 (type*) AllocateHeap(size * sizeof(type), memflags, pc, allocfail);
578 622
623 // !!! Attention, see comments above about the usage !!!
624
625 // allocate type in heap and call ctor
626 #define NEW_C_HEAP_OBJECT(objname, type, memflags, pc, allocfail)\
627 { \
628 objname = (type*)AllocateHeap(sizeof(type), memflags, pc, allocfail); \
629 if (objname != NULL) ::new ((void *)objname) type(); \
630 }
631
632 // allocate array of type, call ctor for every element in the array
633 #define NEW_C_HEAP_OBJECT_ARRAY(array_name, type, size, memflags, pc, allocfail) \
634 { \
635 array_name = (type*)AllocateHeap(size * sizeof(type), memflags, pc, allocfail); \
636 if (array_name != NULL) { \
637 for (int index = 0; index < size; index++) { \
638 ::new ((void*)&array_name[index]) type(); \
639 } \
640 } \
641 }
642
643 // deallocate type in heap, call dtor
644 #define FREE_C_HEAP_OBJECT(type, objname, memflags) \
645 if (objname != NULL) { \
646 ((type*)objname)->~type(); \
647 FREE_C_HEAP_ARRAY(type, objname, memflags); \
648 }
649
650 // deallocate array of type with size, call dtor for every element in the array
651 #define FREE_C_HEAP_OBJECT_ARRAY(type, array_name, size, memflags) \
652 { \
653 if (array_name != NULL) { \
654 for (int index = 0; index < size; index++) { \
655 ((type*)&array_name[index])->~type(); \
656 } \
657 FREE_C_HEAP_ARRAY(type, array_name, memflags); \
658 } \
659 }
579 660
580 extern bool warn_new_operator; 661 extern bool warn_new_operator;
581 662
582 // for statistics 663 // for statistics
583 #ifndef PRODUCT 664 #ifndef PRODUCT

mercurial