1 /*
  2  * Copyright (c) 1997, 2023, 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_ALLOCATION_HPP
 26 #define SHARE_MEMORY_ALLOCATION_HPP
 27 
 28 #include "memory/allStatic.hpp"
 29 #include "utilities/debug.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 #include "utilities/macros.hpp"
 32 
 33 #include <new>
 34 
 35 class outputStream;
 36 class Thread;
 37 class JavaThread;
 38 
 39 class AllocFailStrategy {
 40 public:
 41   enum AllocFailEnum { EXIT_OOM, RETURN_NULL };
 42 };
 43 typedef AllocFailStrategy::AllocFailEnum AllocFailType;
 44 
 45 // The virtual machine must never call one of the implicitly declared
 46 // global allocation or deletion functions.  (Such calls may result in
 47 // link-time or run-time errors.)  For convenience and documentation of
 48 // intended use, classes in the virtual machine may be derived from one
 49 // of the following allocation classes, some of which define allocation
 50 // and deletion functions.
 51 // Note: std::malloc and std::free should never called directly.
 52 
 53 //
 54 // For objects allocated in the resource area (see resourceArea.hpp).
 55 // - ResourceObj
 56 //
 57 // For objects allocated in the C-heap (managed by: free & malloc and tracked with NMT)
 58 // - CHeapObj
 59 //
 60 // For objects allocated on the stack.
 61 // - StackObj
 62 //
 63 // For classes used as name spaces.
 64 // - AllStatic
 65 //
 66 // For classes in Metaspace (class data)
 67 // - MetaspaceObj
 68 //
 69 // The printable subclasses are used for debugging and define virtual
 70 // member functions for printing. Classes that avoid allocating the
 71 // vtbl entries in the objects should therefore not be the printable
 72 // subclasses.
 73 //
 74 // The following macros and function should be used to allocate memory
 75 // directly in the resource area or in the C-heap, The _OBJ variants
 76 // of the NEW/FREE_C_HEAP macros are used for alloc/dealloc simple
 77 // objects which are not inherited from CHeapObj, note constructor and
 78 // destructor are not called. The preferable way to allocate objects
 79 // is using the new operator.
 80 //
 81 // WARNING: The array variant must only be used for a homogeneous array
 82 // where all objects are of the exact type specified. If subtypes are
 83 // stored in the array then must pay attention to calling destructors
 84 // at needed.
 85 //
 86 // NEW_RESOURCE_ARRAY*
 87 // REALLOC_RESOURCE_ARRAY*
 88 // FREE_RESOURCE_ARRAY*
 89 // NEW_RESOURCE_OBJ*
 90 // NEW_C_HEAP_ARRAY*
 91 // REALLOC_C_HEAP_ARRAY*
 92 // FREE_C_HEAP_ARRAY*
 93 // NEW_C_HEAP_OBJ*
 94 // FREE_C_HEAP_OBJ
 95 //
 96 // char* AllocateHeap(size_t size, MEMFLAGS flags, const NativeCallStack& stack, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
 97 // char* AllocateHeap(size_t size, MEMFLAGS flags, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
 98 // char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
 99 // void FreeHeap(void* p);
100 //
101 
102 #define MEMORY_TYPES_DO(f)                                                           \
103   /* Memory type by sub systems. It occupies lower byte. */                          \
104   f(mtJavaHeap,       "Java Heap")   /* Java heap                                 */ \
105   f(mtClass,          "Class")       /* Java classes                              */ \
106   f(mtThread,         "Thread")      /* thread objects                            */ \
107   f(mtThreadStack,    "Thread Stack")                                                \
108   f(mtCode,           "Code")        /* generated code                            */ \
109   f(mtGC,             "GC")                                                          \
110   f(mtGCCardSet,      "GCCardSet")   /* G1 card set remembered set                */ \
111   f(mtCompiler,       "Compiler")                                                    \
112   f(mtJVMCI,          "JVMCI")                                                       \
113   f(mtInternal,       "Internal")    /* memory used by VM, but does not belong to */ \
114                                      /* any of above categories, and not used by  */ \
115                                      /* NMT                                       */ \
116   f(mtOther,          "Other")       /* memory not used by VM                     */ \
117   f(mtSymbol,         "Symbol")                                                      \
118   f(mtNMT,            "Native Memory Tracking")  /* memory used by NMT            */ \
119   f(mtClassShared,    "Shared class space")      /* class data sharing            */ \
120   f(mtChunk,          "Arena Chunk") /* chunk that holds content of arenas        */ \
121   f(mtTest,           "Test")        /* Test type for verifying NMT               */ \
122   f(mtTracing,        "Tracing")                                                     \
123   f(mtLogging,        "Logging")                                                     \
124   f(mtStatistics,     "Statistics")                                                  \
125   f(mtArguments,      "Arguments")                                                   \
126   f(mtModule,         "Module")                                                      \
127   f(mtSafepoint,      "Safepoint")                                                   \
128   f(mtSynchronizer,   "Synchronization")                                             \
129   f(mtServiceability, "Serviceability")                                              \
130   f(mtMetaspace,      "Metaspace")                                                   \
131   f(mtStringDedup,    "String Deduplication")                                        \
132   f(mtObjectMonitor,  "Object Monitors")                                             \
133   f(mtOMWorld,        "OM World")                                                    \
134   f(mtNone,           "Unknown")                                                     \
135   //end
136 
137 #define MEMORY_TYPE_DECLARE_ENUM(type, human_readable) \
138   type,
139 
140 /*
141  * Memory types
142  */
143 enum class MEMFLAGS : uint8_t  {
144   MEMORY_TYPES_DO(MEMORY_TYPE_DECLARE_ENUM)
145   mt_number_of_types   // number of memory types (mtDontTrack
146                        // is not included as validate type)
147 };
148 // Extra insurance that MEMFLAGS truly has the same size as uint8_t.
149 STATIC_ASSERT(sizeof(MEMFLAGS) == sizeof(uint8_t));
150 
151 #define MEMORY_TYPE_SHORTNAME(type, human_readable) \
152   constexpr MEMFLAGS type = MEMFLAGS::type;
153 
154 // Generate short aliases for the enum values. E.g. mtGC instead of MEMFLAGS::mtGC.
155 MEMORY_TYPES_DO(MEMORY_TYPE_SHORTNAME)
156 
157 // Make an int version of the sentinel end value.
158 constexpr int mt_number_of_types = static_cast<int>(MEMFLAGS::mt_number_of_types);
159 
160 extern bool NMT_track_callsite;
161 
162 class NativeCallStack;
163 
164 
165 char* AllocateHeap(size_t size,
166                    MEMFLAGS flags,
167                    const NativeCallStack& stack,
168                    AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
169 char* AllocateHeap(size_t size,
170                    MEMFLAGS flags,
171                    AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
172 
173 char* ReallocateHeap(char *old,
174                      size_t size,
175                      MEMFLAGS flag,
176                      AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
177 
178 // handles null pointers
179 void FreeHeap(void* p);
180 
181 class CHeapObjBase {
182  public:
183   ALWAYSINLINE void* operator new(size_t size, MEMFLAGS f) {
184     return AllocateHeap(size, f);
185   }
186 
187   ALWAYSINLINE void* operator new(size_t size,
188                                   MEMFLAGS f,
189                                   const NativeCallStack& stack) {
190     return AllocateHeap(size, f, stack);
191   }
192 
193   ALWAYSINLINE void* operator new(size_t size,
194                                   MEMFLAGS f,
195                                   const std::nothrow_t&,
196                                   const NativeCallStack& stack) throw() {
197     return AllocateHeap(size, f, stack, AllocFailStrategy::RETURN_NULL);
198   }
199 
200   ALWAYSINLINE void* operator new(size_t size,
201                                   MEMFLAGS f,
202                                   const std::nothrow_t&) throw() {
203     return AllocateHeap(size, f, AllocFailStrategy::RETURN_NULL);
204   }
205 
206   ALWAYSINLINE void* operator new[](size_t size, MEMFLAGS f) {
207     return AllocateHeap(size, f);
208   }
209 
210   ALWAYSINLINE void* operator new[](size_t size,
211                                     MEMFLAGS f,
212                                     const NativeCallStack& stack) {
213     return AllocateHeap(size, f, stack);
214   }
215 
216   ALWAYSINLINE void* operator new[](size_t size,
217                                     MEMFLAGS f,
218                                     const std::nothrow_t&,
219                                     const NativeCallStack& stack) throw() {
220     return AllocateHeap(size, f, stack, AllocFailStrategy::RETURN_NULL);
221   }
222 
223   ALWAYSINLINE void* operator new[](size_t size,
224                                     MEMFLAGS f,
225                                     const std::nothrow_t&) throw() {
226     return AllocateHeap(size, f, AllocFailStrategy::RETURN_NULL);
227   }
228 
229   void operator delete(void* p)     { FreeHeap(p); }
230   void operator delete [] (void* p) { FreeHeap(p); }
231 };
232 
233 // Uses the implicitly static new and delete operators of CHeapObjBase
234 template<MEMFLAGS F>
235 class CHeapObj {
236  public:
237   ALWAYSINLINE void* operator new(size_t size) {
238     return CHeapObjBase::operator new(size, F);
239   }
240 
241   ALWAYSINLINE void* operator new(size_t size,
242                                   const NativeCallStack& stack) {
243     return CHeapObjBase::operator new(size, F, stack);
244   }
245 
246   ALWAYSINLINE void* operator new(size_t size, const std::nothrow_t& nt,
247                                   const NativeCallStack& stack) throw() {
248     return CHeapObjBase::operator new(size, F, nt, stack);
249   }
250 
251   ALWAYSINLINE void* operator new(size_t size, const std::nothrow_t& nt) throw() {
252     return CHeapObjBase::operator new(size, F, nt);
253   }
254 
255   ALWAYSINLINE void* operator new[](size_t size) {
256     return CHeapObjBase::operator new[](size, F);
257   }
258 
259   ALWAYSINLINE void* operator new[](size_t size,
260                                     const NativeCallStack& stack) {
261     return CHeapObjBase::operator new[](size, F, stack);
262   }
263 
264   ALWAYSINLINE void* operator new[](size_t size, const std::nothrow_t& nt,
265                                     const NativeCallStack& stack) throw() {
266     return CHeapObjBase::operator new[](size, F, nt, stack);
267   }
268 
269   ALWAYSINLINE void* operator new[](size_t size, const std::nothrow_t& nt) throw() {
270     return CHeapObjBase::operator new[](size, F, nt);
271   }
272 
273   void operator delete(void* p)     {
274     CHeapObjBase::operator delete(p);
275   }
276 
277   void operator delete [] (void* p) {
278     CHeapObjBase::operator delete[](p);
279   }
280 };
281 
282 // Base class for objects allocated on the stack only.
283 // Calling new or delete will result in fatal error.
284 
285 class StackObj {
286  public:
287   void* operator new(size_t size) = delete;
288   void* operator new [](size_t size) = delete;
289   void  operator delete(void* p) = delete;
290   void  operator delete [](void* p) = delete;
291 };
292 
293 // Base class for objects stored in Metaspace.
294 // Calling delete will result in fatal error.
295 //
296 // Do not inherit from something with a vptr because this class does
297 // not introduce one.  This class is used to allocate both shared read-only
298 // and shared read-write classes.
299 //
300 
301 class ClassLoaderData;
302 class MetaspaceClosure;
303 
304 class MetaspaceObj {
305   // There are functions that all subtypes of MetaspaceObj are expected
306   // to implement, so that templates which are defined for this class hierarchy
307   // can work uniformly. Within the sub-hierarchy of Metadata, these are virtuals.
308   // Elsewhere in the hierarchy of MetaspaceObj, type(), size(), and/or on_stack()
309   // can be static if constant.
310   //
311   // The following functions are required by MetaspaceClosure:
312   //   void metaspace_pointers_do(MetaspaceClosure* it) { <walk my refs> }
313   //   int size() const { return align_up(sizeof(<This>), wordSize) / wordSize; }
314   //   MetaspaceObj::Type type() const { return <This>Type; }
315   //
316   // The following functions are required by MetadataFactory::free_metadata():
317   //   bool on_stack() { return false; }
318   //   void deallocate_contents(ClassLoaderData* loader_data);
319 
320   friend class VMStructs;
321   // When CDS is enabled, all shared metaspace objects are mapped
322   // into a single contiguous memory block, so we can use these
323   // two pointers to quickly determine if something is in the
324   // shared metaspace.
325   // When CDS is not enabled, both pointers are set to null.
326   static void* _shared_metaspace_base;  // (inclusive) low address
327   static void* _shared_metaspace_top;   // (exclusive) high address
328 
329  public:
330 
331   // Returns true if the pointer points to a valid MetaspaceObj. A valid
332   // MetaspaceObj is MetaWord-aligned and contained within either
333   // non-shared or shared metaspace.
334   static bool is_valid(const MetaspaceObj* p);
335 
336 #if INCLUDE_CDS
337   static bool is_shared(const MetaspaceObj* p) {
338     // If no shared metaspace regions are mapped, _shared_metaspace_{base,top} will
339     // both be null and all values of p will be rejected quickly.
340     return (((void*)p) < _shared_metaspace_top &&
341             ((void*)p) >= _shared_metaspace_base);
342   }
343   bool is_shared() const { return MetaspaceObj::is_shared(this); }
344 #else
345   static bool is_shared(const MetaspaceObj* p) { return false; }
346   bool is_shared() const { return false; }
347 #endif
348 
349   void print_address_on(outputStream* st) const;  // nonvirtual address printing
350 
351   static void set_shared_metaspace_range(void* base, void* top) {
352     _shared_metaspace_base = base;
353     _shared_metaspace_top = top;
354   }
355 
356   static void* shared_metaspace_base() { return _shared_metaspace_base; }
357   static void* shared_metaspace_top()  { return _shared_metaspace_top;  }
358 
359 #define METASPACE_OBJ_TYPES_DO(f) \
360   f(Class) \
361   f(Symbol) \
362   f(TypeArrayU1) \
363   f(TypeArrayU2) \
364   f(TypeArrayU4) \
365   f(TypeArrayU8) \
366   f(TypeArrayOther) \
367   f(Method) \
368   f(ConstMethod) \
369   f(MethodData) \
370   f(ConstantPool) \
371   f(ConstantPoolCache) \
372   f(Annotations) \
373   f(MethodCounters) \
374   f(SharedClassPathEntry) \
375   f(RecordComponent)
376 
377 #define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type,
378 #define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;
379 
380   enum Type {
381     // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
382     METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)
383     _number_of_types
384   };
385 
386   static const char * type_name(Type type) {
387     switch(type) {
388     METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)
389     default:
390       ShouldNotReachHere();
391       return nullptr;
392     }
393   }
394 
395   static MetaspaceObj::Type array_type(size_t elem_size) {
396     switch (elem_size) {
397     case 1: return TypeArrayU1Type;
398     case 2: return TypeArrayU2Type;
399     case 4: return TypeArrayU4Type;
400     case 8: return TypeArrayU8Type;
401     default:
402       return TypeArrayOtherType;
403     }
404   }
405 
406   void* operator new(size_t size, ClassLoaderData* loader_data,
407                      size_t word_size,
408                      Type type, JavaThread* thread) throw();
409                      // can't use TRAPS from this header file.
410   void* operator new(size_t size, ClassLoaderData* loader_data,
411                      size_t word_size,
412                      Type type) throw();
413   void operator delete(void* p) { ShouldNotCallThis(); }
414 
415   // Declare a *static* method with the same signature in any subclass of MetaspaceObj
416   // that should be read-only by default. See symbol.hpp for an example. This function
417   // is used by the templates in metaspaceClosure.hpp
418   static bool is_read_only_by_default() { return false; }
419 };
420 
421 // Base class for classes that constitute name spaces.
422 
423 class Arena;
424 
425 extern char* resource_allocate_bytes(size_t size,
426     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
427 extern char* resource_allocate_bytes(Thread* thread, size_t size,
428     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
429 extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size,
430     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
431 extern void resource_free_bytes( Thread* thread, char *old, size_t size );
432 
433 //----------------------------------------------------------------------
434 // Base class for objects allocated in the resource area.
435 class ResourceObj {
436  public:
437   void* operator new(size_t size) {
438     return resource_allocate_bytes(size);
439   }
440 
441   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
442     return resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
443   }
444 
445   void* operator new [](size_t size) throw() = delete;
446   void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() = delete;
447 
448   void  operator delete(void* p) = delete;
449   void  operator delete [](void* p) = delete;
450 };
451 
452 class ArenaObj {
453  public:
454   void* operator new(size_t size, Arena *arena) throw();
455   void* operator new [](size_t size, Arena *arena) throw() = delete;
456 
457   void* operator new [](size_t size) throw() = delete;
458   void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() = delete;
459 
460   void  operator delete(void* p) = delete;
461   void  operator delete [](void* p) = delete;
462 };
463 
464 //----------------------------------------------------------------------
465 // Base class for objects allocated in the resource area per default.
466 // Optionally, objects may be allocated on the C heap with
467 // new (AnyObj::C_HEAP) Foo(...) or in an Arena with new (&arena).
468 // AnyObj's can be allocated within other objects, but don't use
469 // new or delete (allocation_type is unknown).  If new is used to allocate,
470 // use delete to deallocate.
471 class AnyObj {
472  public:
473   enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };
474   static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN;
475 #ifdef ASSERT
476  private:
477   // When this object is allocated on stack the new() operator is not
478   // called but garbage on stack may look like a valid allocation_type.
479   // Store negated 'this' pointer when new() is called to distinguish cases.
480   // Use second array's element for verification value to distinguish garbage.
481   uintptr_t _allocation_t[2];
482   bool is_type_set() const;
483   void initialize_allocation_info();
484  public:
485   allocation_type get_allocation_type() const;
486   bool allocated_on_stack_or_embedded() const { return get_allocation_type() == STACK_OR_EMBEDDED; }
487   bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; }
488   bool allocated_on_C_heap()   const { return get_allocation_type() == C_HEAP; }
489   bool allocated_on_arena()    const { return get_allocation_type() == ARENA; }
490 protected:
491   AnyObj(); // default constructor
492   AnyObj(const AnyObj& r); // default copy constructor
493   AnyObj& operator=(const AnyObj& r); // default copy assignment
494   ~AnyObj();
495 #endif // ASSERT
496 
497  public:
498   // CHeap allocations
499   void* operator new(size_t size, MEMFLAGS flags) throw();
500   void* operator new [](size_t size, MEMFLAGS flags) throw() = delete;
501   void* operator new(size_t size, const std::nothrow_t&  nothrow_constant, MEMFLAGS flags) throw();
502   void* operator new [](size_t size, const std::nothrow_t&  nothrow_constant, MEMFLAGS flags) throw() = delete;
503 
504   // Arena allocations
505   void* operator new(size_t size, Arena *arena);
506   void* operator new [](size_t size, Arena *arena) = delete;
507 
508   // Resource allocations
509   void* operator new(size_t size) {
510     address res = (address)resource_allocate_bytes(size);
511     DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
512     return res;
513   }
514   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
515     address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
516     DEBUG_ONLY(if (res != nullptr) set_allocation_type(res, RESOURCE_AREA);)
517     return res;
518   }
519 
520   void* operator new [](size_t size) = delete;
521   void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) = delete;
522   void  operator delete(void* p);
523   void  operator delete [](void* p) = delete;
524 
525 #ifndef PRODUCT
526   // Printing support
527   void print() const;
528   virtual void print_on(outputStream* st) const;
529 #endif // PRODUCT
530 };
531 
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.
535 
536 #define NEW_RESOURCE_ARRAY(type, size)\
537   (type*) resource_allocate_bytes((size) * sizeof(type))
538 
539 #define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\
540   (type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
541 
542 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
543   (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
544 
545 #define NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(thread, type, size)\
546   (type*) resource_allocate_bytes(thread, (size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
547 
548 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
549   (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type))
550 
551 #define REALLOC_RESOURCE_ARRAY_RETURN_NULL(type, old, old_size, new_size)\
552   (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type),\
553                                     (new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
554 
555 #define FREE_RESOURCE_ARRAY(type, old, size)\
556   resource_free_bytes(Thread::current(), (char*)(old), (size) * sizeof(type))
557 
558 #define FREE_RESOURCE_ARRAY_IN_THREAD(thread, type, old, size)\
559   resource_free_bytes(thread, (char*)(old), (size) * sizeof(type))
560 
561 #define FREE_FAST(old)\
562     /* nop */
563 
564 #define NEW_RESOURCE_OBJ(type)\
565   NEW_RESOURCE_ARRAY(type, 1)
566 
567 #define NEW_RESOURCE_OBJ_RETURN_NULL(type)\
568   NEW_RESOURCE_ARRAY_RETURN_NULL(type, 1)
569 
570 #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail)\
571   (type*) AllocateHeap((size) * sizeof(type), memflags, pc, allocfail)
572 
573 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
574   (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
575 
576 #define NEW_C_HEAP_ARRAY(type, size, memflags)\
577   (type*) (AllocateHeap((size) * sizeof(type), memflags))
578 
579 #define NEW_C_HEAP_ARRAY2_RETURN_NULL(type, size, memflags, pc)\
580   NEW_C_HEAP_ARRAY3(type, (size), memflags, pc, AllocFailStrategy::RETURN_NULL)
581 
582 #define NEW_C_HEAP_ARRAY_RETURN_NULL(type, size, memflags)\
583   NEW_C_HEAP_ARRAY2(type, (size), memflags, AllocFailStrategy::RETURN_NULL)
584 
585 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
586   (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags))
587 
588 #define REALLOC_C_HEAP_ARRAY_RETURN_NULL(type, old, size, memflags)\
589   (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags, AllocFailStrategy::RETURN_NULL))
590 
591 #define FREE_C_HEAP_ARRAY(type, old) \
592   FreeHeap((char*)(old))
593 
594 // allocate type in heap without calling ctor
595 #define NEW_C_HEAP_OBJ(type, memflags)\
596   NEW_C_HEAP_ARRAY(type, 1, memflags)
597 
598 #define NEW_C_HEAP_OBJ_RETURN_NULL(type, memflags)\
599   NEW_C_HEAP_ARRAY_RETURN_NULL(type, 1, memflags)
600 
601 // deallocate obj of type in heap without calling dtor
602 #define FREE_C_HEAP_OBJ(objname)\
603   FreeHeap((char*)objname);
604 
605 
606 //------------------------------ReallocMark---------------------------------
607 // Code which uses REALLOC_RESOURCE_ARRAY should check an associated
608 // ReallocMark, which is declared in the same scope as the reallocated
609 // pointer.  Any operation that could __potentially__ cause a reallocation
610 // should check the ReallocMark.
611 class ReallocMark: public StackObj {
612 protected:
613   NOT_PRODUCT(int _nesting;)
614 
615 public:
616   ReallocMark()   PRODUCT_RETURN;
617   void check()    PRODUCT_RETURN;
618 };
619 
620 // Uses mmapped memory for all allocations. All allocations are initially
621 // zero-filled. No pre-touching.
622 template <class E>
623 class MmapArrayAllocator : public AllStatic {
624  private:
625   static size_t size_for(size_t length);
626 
627  public:
628   static E* allocate_or_null(size_t length, MEMFLAGS flags);
629   static E* allocate(size_t length, MEMFLAGS flags);
630   static void free(E* addr, size_t length);
631 };
632 
633 // Uses malloc:ed memory for all allocations.
634 template <class E>
635 class MallocArrayAllocator : public AllStatic {
636  public:
637   static size_t size_for(size_t length);
638 
639   static E* allocate(size_t length, MEMFLAGS flags);
640   static E* reallocate(E* addr, size_t new_length, MEMFLAGS flags);
641   static void free(E* addr);
642 };
643 
644 #endif // SHARE_MEMORY_ALLOCATION_HPP