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(mtNone,           "Unknown")                                                     \
134   //end
135 
136 #define MEMORY_TYPE_DECLARE_ENUM(type, human_readable) \
137   type,
138 
139 /*
140  * Memory types
141  */
142 enum class MEMFLAGS : uint8_t  {
143   MEMORY_TYPES_DO(MEMORY_TYPE_DECLARE_ENUM)
144   mt_number_of_types   // number of memory types (mtDontTrack
145                        // is not included as validate type)
146 };
147 // Extra insurance that MEMFLAGS truly has the same size as uint8_t.
148 STATIC_ASSERT(sizeof(MEMFLAGS) == sizeof(uint8_t));
149 
150 #define MEMORY_TYPE_SHORTNAME(type, human_readable) \
151   constexpr MEMFLAGS type = MEMFLAGS::type;
152 
153 // Generate short aliases for the enum values. E.g. mtGC instead of MEMFLAGS::mtGC.
154 MEMORY_TYPES_DO(MEMORY_TYPE_SHORTNAME)
155 
156 // Make an int version of the sentinel end value.
157 constexpr int mt_number_of_types = static_cast<int>(MEMFLAGS::mt_number_of_types);
158 
159 extern bool NMT_track_callsite;
160 
161 class NativeCallStack;
162 
163 
164 char* AllocateHeap(size_t size,
165                    MEMFLAGS flags,
166                    const NativeCallStack& stack,
167                    AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
168 char* AllocateHeap(size_t size,
169                    MEMFLAGS flags,
170                    AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
171 
172 char* ReallocateHeap(char *old,
173                      size_t size,
174                      MEMFLAGS flag,
175                      AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
176 
177 // handles null pointers
178 void FreeHeap(void* p);
179 
180 class CHeapObjBase {
181  public:
182   ALWAYSINLINE void* operator new(size_t size, MEMFLAGS f) {
183     return AllocateHeap(size, f);
184   }
185 
186   ALWAYSINLINE void* operator new(size_t size,
187                                   MEMFLAGS f,
188                                   const NativeCallStack& stack) {
189     return AllocateHeap(size, f, stack);
190   }
191 
192   ALWAYSINLINE void* operator new(size_t size,
193                                   MEMFLAGS f,
194                                   const std::nothrow_t&,
195                                   const NativeCallStack& stack) throw() {
196     return AllocateHeap(size, f, stack, AllocFailStrategy::RETURN_NULL);
197   }
198 
199   ALWAYSINLINE void* operator new(size_t size,
200                                   MEMFLAGS f,
201                                   const std::nothrow_t&) throw() {
202     return AllocateHeap(size, f, AllocFailStrategy::RETURN_NULL);
203   }
204 
205   ALWAYSINLINE void* operator new[](size_t size, MEMFLAGS f) {
206     return AllocateHeap(size, f);
207   }
208 
209   ALWAYSINLINE void* operator new[](size_t size,
210                                     MEMFLAGS f,
211                                     const NativeCallStack& stack) {
212     return AllocateHeap(size, f, stack);
213   }
214 
215   ALWAYSINLINE void* operator new[](size_t size,
216                                     MEMFLAGS f,
217                                     const std::nothrow_t&,
218                                     const NativeCallStack& stack) throw() {
219     return AllocateHeap(size, f, stack, AllocFailStrategy::RETURN_NULL);
220   }
221 
222   ALWAYSINLINE void* operator new[](size_t size,
223                                     MEMFLAGS f,
224                                     const std::nothrow_t&) throw() {
225     return AllocateHeap(size, f, AllocFailStrategy::RETURN_NULL);
226   }
227 
228   void operator delete(void* p)     { FreeHeap(p); }
229   void operator delete [] (void* p) { FreeHeap(p); }
230 };
231 
232 // Uses the implicitly static new and delete operators of CHeapObjBase
233 template<MEMFLAGS F>
234 class CHeapObj {
235  public:
236   ALWAYSINLINE void* operator new(size_t size) {
237     return CHeapObjBase::operator new(size, F);
238   }
239 
240   ALWAYSINLINE void* operator new(size_t size,
241                                   const NativeCallStack& stack) {
242     return CHeapObjBase::operator new(size, F, stack);
243   }
244 
245   ALWAYSINLINE void* operator new(size_t size, const std::nothrow_t& nt,
246                                   const NativeCallStack& stack) throw() {
247     return CHeapObjBase::operator new(size, F, nt, stack);
248   }
249 
250   ALWAYSINLINE void* operator new(size_t size, const std::nothrow_t& nt) throw() {
251     return CHeapObjBase::operator new(size, F, nt);
252   }
253 
254   ALWAYSINLINE void* operator new[](size_t size) {
255     return CHeapObjBase::operator new[](size, F);
256   }
257 
258   ALWAYSINLINE void* operator new[](size_t size,
259                                     const NativeCallStack& stack) {
260     return CHeapObjBase::operator new[](size, F, stack);
261   }
262 
263   ALWAYSINLINE void* operator new[](size_t size, const std::nothrow_t& nt,
264                                     const NativeCallStack& stack) throw() {
265     return CHeapObjBase::operator new[](size, F, nt, stack);
266   }
267 
268   ALWAYSINLINE void* operator new[](size_t size, const std::nothrow_t& nt) throw() {
269     return CHeapObjBase::operator new[](size, F, nt);
270   }
271 
272   void operator delete(void* p)     {
273     CHeapObjBase::operator delete(p);
274   }
275 
276   void operator delete [] (void* p) {
277     CHeapObjBase::operator delete[](p);
278   }
279 };
280 
281 // Base class for objects allocated on the stack only.
282 // Calling new or delete will result in fatal error.
283 
284 class StackObj {
285  public:
286   void* operator new(size_t size) = delete;
287   void* operator new [](size_t size) = delete;
288   void  operator delete(void* p) = delete;
289   void  operator delete [](void* p) = delete;
290 };
291 
292 // Base class for objects stored in Metaspace.
293 // Calling delete will result in fatal error.
294 //
295 // Do not inherit from something with a vptr because this class does
296 // not introduce one.  This class is used to allocate both shared read-only
297 // and shared read-write classes.
298 //
299 
300 class ClassLoaderData;
301 class MetaspaceClosure;
302 
303 class MetaspaceObj {
304   // There are functions that all subtypes of MetaspaceObj are expected
305   // to implement, so that templates which are defined for this class hierarchy
306   // can work uniformly. Within the sub-hierarchy of Metadata, these are virtuals.
307   // Elsewhere in the hierarchy of MetaspaceObj, type(), size(), and/or on_stack()
308   // can be static if constant.
309   //
310   // The following functions are required by MetaspaceClosure:
311   //   void metaspace_pointers_do(MetaspaceClosure* it) { <walk my refs> }
312   //   int size() const { return align_up(sizeof(<This>), wordSize) / wordSize; }
313   //   MetaspaceObj::Type type() const { return <This>Type; }
314   //
315   // The following functions are required by MetadataFactory::free_metadata():
316   //   bool on_stack() { return false; }
317   //   void deallocate_contents(ClassLoaderData* loader_data);
318 
319   friend class VMStructs;
320   // When CDS is enabled, all shared metaspace objects are mapped
321   // into a single contiguous memory block, so we can use these
322   // two pointers to quickly determine if something is in the
323   // shared metaspace.
324   // When CDS is not enabled, both pointers are set to null.
325   static void* _shared_metaspace_base;  // (inclusive) low address
326   static void* _shared_metaspace_top;   // (exclusive) high address
327 
328  public:
329 
330   // Returns true if the pointer points to a valid MetaspaceObj. A valid
331   // MetaspaceObj is MetaWord-aligned and contained within either
332   // non-shared or shared metaspace.
333   static bool is_valid(const MetaspaceObj* p);
334 
335 #if INCLUDE_CDS
336   static bool is_shared(const MetaspaceObj* p) {
337     // If no shared metaspace regions are mapped, _shared_metaspace_{base,top} will
338     // both be null and all values of p will be rejected quickly.
339     return (((void*)p) < _shared_metaspace_top &&
340             ((void*)p) >= _shared_metaspace_base);
341   }
342   bool is_shared() const { return MetaspaceObj::is_shared(this); }
343 #else
344   static bool is_shared(const MetaspaceObj* p) { return false; }
345   bool is_shared() const { return false; }
346 #endif
347 
348   void print_address_on(outputStream* st) const;  // nonvirtual address printing
349 
350   static void set_shared_metaspace_range(void* base, void* top) {
351     _shared_metaspace_base = base;
352     _shared_metaspace_top = top;
353   }
354 
355   static void* shared_metaspace_base() { return _shared_metaspace_base; }
356   static void* shared_metaspace_top()  { return _shared_metaspace_top;  }
357 
358 #define METASPACE_OBJ_TYPES_DO(f) \
359   f(Class) \
360   f(Symbol) \
361   f(TypeArrayU1) \
362   f(TypeArrayU2) \
363   f(TypeArrayU4) \
364   f(TypeArrayU8) \
365   f(TypeArrayOther) \
366   f(Method) \
367   f(ConstMethod) \
368   f(MethodData) \
369   f(ConstantPool) \
370   f(ConstantPoolCache) \
371   f(Annotations) \
372   f(MethodCounters) \
373   f(SharedClassPathEntry) \
374   f(RecordComponent)
375 
376 #define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type,
377 #define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;
378 
379   enum Type {
380     // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
381     METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)
382     _number_of_types
383   };
384 
385   static const char * type_name(Type type) {
386     switch(type) {
387     METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)
388     default:
389       ShouldNotReachHere();
390       return nullptr;
391     }
392   }
393 
394   static MetaspaceObj::Type array_type(size_t elem_size) {
395     switch (elem_size) {
396     case 1: return TypeArrayU1Type;
397     case 2: return TypeArrayU2Type;
398     case 4: return TypeArrayU4Type;
399     case 8: return TypeArrayU8Type;
400     default:
401       return TypeArrayOtherType;
402     }
403   }
404 
405   void* operator new(size_t size, ClassLoaderData* loader_data,
406                      size_t word_size,
407                      Type type, JavaThread* thread) throw();
408                      // can't use TRAPS from this header file.
409   void* operator new(size_t size, ClassLoaderData* loader_data,
410                      size_t word_size,
411                      Type type) throw();
412   void operator delete(void* p) { ShouldNotCallThis(); }
413 
414   // Declare a *static* method with the same signature in any subclass of MetaspaceObj
415   // that should be read-only by default. See symbol.hpp for an example. This function
416   // is used by the templates in metaspaceClosure.hpp
417   static bool is_read_only_by_default() { return false; }
418 };
419 
420 // Base class for classes that constitute name spaces.
421 
422 class Arena;
423 
424 extern char* resource_allocate_bytes(size_t size,
425     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
426 extern char* resource_allocate_bytes(Thread* thread, size_t size,
427     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
428 extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size,
429     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
430 extern void resource_free_bytes( Thread* thread, char *old, size_t size );
431 
432 //----------------------------------------------------------------------
433 // Base class for objects allocated in the resource area.
434 class ResourceObj {
435  public:
436   void* operator new(size_t size) {
437     return resource_allocate_bytes(size);
438   }
439 
440   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
441     return resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
442   }
443 
444   void* operator new [](size_t size) throw() = delete;
445   void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() = delete;
446 
447   void  operator delete(void* p) = delete;
448   void  operator delete [](void* p) = delete;
449 };
450 
451 class ArenaObj {
452  public:
453   void* operator new(size_t size, Arena *arena) throw();
454   void* operator new [](size_t size, Arena *arena) throw() = delete;
455 
456   void* operator new [](size_t size) throw() = delete;
457   void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() = delete;
458 
459   void  operator delete(void* p) = delete;
460   void  operator delete [](void* p) = delete;
461 };
462 
463 //----------------------------------------------------------------------
464 // Base class for objects allocated in the resource area per default.
465 // Optionally, objects may be allocated on the C heap with
466 // new (AnyObj::C_HEAP) Foo(...) or in an Arena with new (&arena).
467 // AnyObj's can be allocated within other objects, but don't use
468 // new or delete (allocation_type is unknown).  If new is used to allocate,
469 // use delete to deallocate.
470 class AnyObj {
471  public:
472   enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };
473   static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN;
474 #ifdef ASSERT
475  private:
476   // When this object is allocated on stack the new() operator is not
477   // called but garbage on stack may look like a valid allocation_type.
478   // Store negated 'this' pointer when new() is called to distinguish cases.
479   // Use second array's element for verification value to distinguish garbage.
480   uintptr_t _allocation_t[2];
481   bool is_type_set() const;
482   void initialize_allocation_info();
483  public:
484   allocation_type get_allocation_type() const;
485   bool allocated_on_stack_or_embedded() const { return get_allocation_type() == STACK_OR_EMBEDDED; }
486   bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; }
487   bool allocated_on_C_heap()   const { return get_allocation_type() == C_HEAP; }
488   bool allocated_on_arena()    const { return get_allocation_type() == ARENA; }
489 protected:
490   AnyObj(); // default constructor
491   AnyObj(const AnyObj& r); // default copy constructor
492   AnyObj& operator=(const AnyObj& r); // default copy assignment
493   ~AnyObj();
494 #endif // ASSERT
495 
496  public:
497   // CHeap allocations
498   void* operator new(size_t size, MEMFLAGS flags) throw();
499   void* operator new [](size_t size, MEMFLAGS flags) throw() = delete;
500   void* operator new(size_t size, const std::nothrow_t&  nothrow_constant, MEMFLAGS flags) throw();
501   void* operator new [](size_t size, const std::nothrow_t&  nothrow_constant, MEMFLAGS flags) throw() = delete;
502 
503   // Arena allocations
504   void* operator new(size_t size, Arena *arena);
505   void* operator new [](size_t size, Arena *arena) = delete;
506 
507   // Resource allocations
508   void* operator new(size_t size) {
509     address res = (address)resource_allocate_bytes(size);
510     DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
511     return res;
512   }
513   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
514     address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
515     DEBUG_ONLY(if (res != nullptr) set_allocation_type(res, RESOURCE_AREA);)
516     return res;
517   }
518 
519   void* operator new [](size_t size) = delete;
520   void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) = delete;
521   void  operator delete(void* p);
522   void  operator delete [](void* p) = delete;
523 
524 #ifndef PRODUCT
525   // Printing support
526   void print() const;
527   virtual void print_on(outputStream* st) const;
528 #endif // PRODUCT
529 };
530 
531 // One of the following macros must be used when allocating an array
532 // or object to determine whether it should reside in the C heap on in
533 // the resource area.
534 
535 #define NEW_RESOURCE_ARRAY(type, size)\
536   (type*) resource_allocate_bytes((size) * sizeof(type))
537 
538 #define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\
539   (type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
540 
541 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
542   (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
543 
544 #define NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(thread, type, size)\
545   (type*) resource_allocate_bytes(thread, (size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
546 
547 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
548   (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type))
549 
550 #define REALLOC_RESOURCE_ARRAY_RETURN_NULL(type, old, old_size, new_size)\
551   (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type),\
552                                     (new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
553 
554 #define FREE_RESOURCE_ARRAY(type, old, size)\
555   resource_free_bytes(Thread::current(), (char*)(old), (size) * sizeof(type))
556 
557 #define FREE_RESOURCE_ARRAY_IN_THREAD(thread, type, old, size)\
558   resource_free_bytes(thread, (char*)(old), (size) * sizeof(type))
559 
560 #define FREE_FAST(old)\
561     /* nop */
562 
563 #define NEW_RESOURCE_OBJ(type)\
564   NEW_RESOURCE_ARRAY(type, 1)
565 
566 #define NEW_RESOURCE_OBJ_RETURN_NULL(type)\
567   NEW_RESOURCE_ARRAY_RETURN_NULL(type, 1)
568 
569 #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail)\
570   (type*) AllocateHeap((size) * sizeof(type), memflags, pc, allocfail)
571 
572 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
573   (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
574 
575 #define NEW_C_HEAP_ARRAY(type, size, memflags)\
576   (type*) (AllocateHeap((size) * sizeof(type), memflags))
577 
578 #define NEW_C_HEAP_ARRAY2_RETURN_NULL(type, size, memflags, pc)\
579   NEW_C_HEAP_ARRAY3(type, (size), memflags, pc, AllocFailStrategy::RETURN_NULL)
580 
581 #define NEW_C_HEAP_ARRAY_RETURN_NULL(type, size, memflags)\
582   NEW_C_HEAP_ARRAY2(type, (size), memflags, AllocFailStrategy::RETURN_NULL)
583 
584 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
585   (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags))
586 
587 #define REALLOC_C_HEAP_ARRAY_RETURN_NULL(type, old, size, memflags)\
588   (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags, AllocFailStrategy::RETURN_NULL))
589 
590 #define FREE_C_HEAP_ARRAY(type, old) \
591   FreeHeap((char*)(old))
592 
593 // allocate type in heap without calling ctor
594 #define NEW_C_HEAP_OBJ(type, memflags)\
595   NEW_C_HEAP_ARRAY(type, 1, memflags)
596 
597 #define NEW_C_HEAP_OBJ_RETURN_NULL(type, memflags)\
598   NEW_C_HEAP_ARRAY_RETURN_NULL(type, 1, memflags)
599 
600 // deallocate obj of type in heap without calling dtor
601 #define FREE_C_HEAP_OBJ(objname)\
602   FreeHeap((char*)objname);
603 
604 
605 //------------------------------ReallocMark---------------------------------
606 // Code which uses REALLOC_RESOURCE_ARRAY should check an associated
607 // ReallocMark, which is declared in the same scope as the reallocated
608 // pointer.  Any operation that could __potentially__ cause a reallocation
609 // should check the ReallocMark.
610 class ReallocMark: public StackObj {
611 protected:
612   NOT_PRODUCT(int _nesting;)
613 
614 public:
615   ReallocMark()   PRODUCT_RETURN;
616   void check()    PRODUCT_RETURN;
617 };
618 
619 // Uses mmapped memory for all allocations. All allocations are initially
620 // zero-filled. No pre-touching.
621 template <class E>
622 class MmapArrayAllocator : public AllStatic {
623  private:
624   static size_t size_for(size_t length);
625 
626  public:
627   static E* allocate_or_null(size_t length, MEMFLAGS flags);
628   static E* allocate(size_t length, MEMFLAGS flags);
629   static void free(E* addr, size_t length);
630 };
631 
632 // Uses malloc:ed memory for all allocations.
633 template <class E>
634 class MallocArrayAllocator : public AllStatic {
635  public:
636   static size_t size_for(size_t length);
637 
638   static E* allocate(size_t length, MEMFLAGS flags);
639   static E* reallocate(E* addr, size_t new_length, MEMFLAGS flags);
640   static void free(E* addr);
641 };
642 
643 #endif // SHARE_MEMORY_ALLOCATION_HPP