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, The _OBJECT variants |
89 // directly in the resource area or in the C-heap: |
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. |
|
98 // |
90 // |
99 // NEW_RESOURCE_ARRAY(type,size) |
91 // NEW_RESOURCE_ARRAY(type,size) |
100 // NEW_RESOURCE_OBJ(type) |
92 // NEW_RESOURCE_OBJ(type) |
101 // NEW_C_HEAP_ARRAY(type,size) |
93 // NEW_C_HEAP_ARRAY(type,size) |
102 // NEW_C_HEAP_OBJ(type) |
94 // 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) |
|
107 // char* AllocateHeap(size_t size, const char* name); |
95 // char* AllocateHeap(size_t size, const char* name); |
108 // void FreeHeap(void* p); |
96 // void FreeHeap(void* p); |
109 // |
97 // |
110 // C-heap allocation can be traced using +PrintHeapAllocation. |
98 // C-heap allocation can be traced using +PrintHeapAllocation. |
111 // malloc and free should therefore never called directly. |
99 // malloc and free should therefore never called directly. |
205 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { |
193 template <MEMFLAGS F> class CHeapObj ALLOCATION_SUPER_CLASS_SPEC { |
206 public: |
194 public: |
207 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0); |
195 _NOINLINE_ void* operator new(size_t size, address caller_pc = 0); |
208 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant, |
196 _NOINLINE_ void* operator new (size_t size, const std::nothrow_t& nothrow_constant, |
209 address caller_pc = 0); |
197 address caller_pc = 0); |
210 _NOINLINE_ void* operator new [](size_t size, address caller_pc = 0); |
198 |
211 _NOINLINE_ void* operator new [](size_t size, const std::nothrow_t& nothrow_constant, |
|
212 address caller_pc = 0); |
|
213 void operator delete(void* p); |
199 void operator delete(void* p); |
214 void operator delete [] (void* p); |
|
215 }; |
200 }; |
216 |
201 |
217 // Base class for objects allocated on the stack only. |
202 // Base class for objects allocated on the stack only. |
218 // Calling new or delete will result in fatal error. |
203 // Calling new or delete will result in fatal error. |
219 |
204 |
220 class StackObj ALLOCATION_SUPER_CLASS_SPEC { |
205 class StackObj ALLOCATION_SUPER_CLASS_SPEC { |
221 private: |
206 private: |
222 void* operator new(size_t size); |
207 void* operator new(size_t size); |
223 void operator delete(void* p); |
208 void operator delete(void* p); |
224 void* operator new [](size_t size); |
|
225 void operator delete [](void* p); |
|
226 }; |
209 }; |
227 |
210 |
228 // Base class for objects used as value objects. |
211 // Base class for objects used as value objects. |
229 // Calling new or delete will result in fatal error. |
212 // Calling new or delete will result in fatal error. |
230 // |
213 // |
527 ~ResourceObj(); |
508 ~ResourceObj(); |
528 #endif // ASSERT |
509 #endif // ASSERT |
529 |
510 |
530 public: |
511 public: |
531 void* operator new(size_t size, allocation_type type, MEMFLAGS flags); |
512 void* operator new(size_t size, allocation_type type, MEMFLAGS flags); |
532 void* operator new [](size_t size, allocation_type type, MEMFLAGS flags); |
|
533 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, |
513 void* operator new(size_t size, const std::nothrow_t& nothrow_constant, |
534 allocation_type type, MEMFLAGS flags); |
514 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 |
|
538 void* operator new(size_t size, Arena *arena) { |
515 void* operator new(size_t size, Arena *arena) { |
539 address res = (address)arena->Amalloc(size); |
516 address res = (address)arena->Amalloc(size); |
540 DEBUG_ONLY(set_allocation_type(res, ARENA);) |
517 DEBUG_ONLY(set_allocation_type(res, ARENA);) |
541 return res; |
518 return res; |
542 } |
519 } |
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 |
|
550 void* operator new(size_t size) { |
520 void* operator new(size_t size) { |
551 address res = (address)resource_allocate_bytes(size); |
521 address res = (address)resource_allocate_bytes(size); |
552 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) |
522 DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);) |
553 return res; |
523 return res; |
554 } |
524 } |
557 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL); |
527 address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL); |
558 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);) |
528 DEBUG_ONLY(if (res != NULL) set_allocation_type(res, RESOURCE_AREA);) |
559 return res; |
529 return res; |
560 } |
530 } |
561 |
531 |
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 |
|
574 void operator delete(void* p); |
532 void operator delete(void* p); |
575 void operator delete [](void* p); |
|
576 }; |
533 }; |
577 |
534 |
578 // One of the following macros must be used when allocating an array |
535 // One of the following macros must be used when allocating an array |
579 // or object to determine whether it should reside in the C heap on in |
536 // or object to determine whether it should reside in the C heap on in |
580 // the resource area. |
537 // the resource area. |
601 (type*) (AllocateHeap((size) * sizeof(type), memflags)) |
558 (type*) (AllocateHeap((size) * sizeof(type), memflags)) |
602 |
559 |
603 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\ |
560 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\ |
604 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags)) |
561 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags)) |
605 |
562 |
606 #define FREE_C_HEAP_ARRAY(type, old, memflags) \ |
563 #define FREE_C_HEAP_ARRAY(type,old,memflags) \ |
607 FreeHeap((char*)(old), memflags) |
564 FreeHeap((char*)(old), memflags) |
608 |
565 |
609 // allocate type in heap without calling ctor |
|
610 // WARNING: type must not have virtual functions!!! There is no way to initialize vtable. |
|
611 #define NEW_C_HEAP_OBJ(type, memflags)\ |
566 #define NEW_C_HEAP_OBJ(type, memflags)\ |
612 NEW_C_HEAP_ARRAY(type, 1, memflags) |
567 NEW_C_HEAP_ARRAY(type, 1, memflags) |
613 |
568 |
|
569 |
614 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\ |
570 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\ |
615 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc)) |
571 (type*) (AllocateHeap((size) * sizeof(type), memflags, pc)) |
616 |
572 |
617 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\ |
573 #define REALLOC_C_HEAP_ARRAY2(type, old, size, memflags, pc)\ |
618 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc)) |
574 (type*) (ReallocateHeap((char*)old, (size) * sizeof(type), memflags, pc)) |
619 |
575 |
620 #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail) \ |
576 #define NEW_C_HEAP_OBJ2(type, memflags, pc)\ |
621 (type*) AllocateHeap(size * sizeof(type), memflags, pc, allocfail); |
577 NEW_C_HEAP_ARRAY2(type, 1, memflags, pc) |
622 |
578 |
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 } |
|
660 |
579 |
661 extern bool warn_new_operator; |
580 extern bool warn_new_operator; |
662 |
581 |
663 // for statistics |
582 // for statistics |
664 #ifndef PRODUCT |
583 #ifndef PRODUCT |