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(RecordComponent) \
374   f(KlassTrainingData) \
375   f(MethodTrainingData) \
376   f(CompileTrainingData) \
377   f(SharedClassPathEntry)
378 
379 #define METASPACE_OBJ_TYPE_DECLARE(name) name ## Type,
380 #define METASPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;
381 
382   enum Type {
383     // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
384     METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_DECLARE)
385     _number_of_types
386   };
387 
388   static const char * type_name(Type type) {
389     switch(type) {
390     METASPACE_OBJ_TYPES_DO(METASPACE_OBJ_TYPE_NAME_CASE)
391     default:
392       ShouldNotReachHere();
393       return nullptr;
394     }
395   }
396 
397   static MetaspaceObj::Type array_type(size_t elem_size) {
398     switch (elem_size) {
399     case 1: return TypeArrayU1Type;
400     case 2: return TypeArrayU2Type;
401     case 4: return TypeArrayU4Type;
402     case 8: return TypeArrayU8Type;
403     default:
404       return TypeArrayOtherType;
405     }
406   }
407 
408   void* operator new(size_t size, ClassLoaderData* loader_data,
409                      size_t word_size,
410                      Type type, JavaThread* thread) throw();
411                      // can't use TRAPS from this header file.
412   void* operator new(size_t size, ClassLoaderData* loader_data,
413                      size_t word_size,
414                      Type type) throw();
415   void operator delete(void* p) { ShouldNotCallThis(); }
416 
417   // Declare a *static* method with the same signature in any subclass of MetaspaceObj
418   // that should be read-only by default. See symbol.hpp for an example. This function
419   // is used by the templates in metaspaceClosure.hpp
420   static bool is_read_only_by_default() { return false; }
421 };
422 
423 // Base class for classes that constitute name spaces.
424 
425 class Arena;
426 
427 extern char* resource_allocate_bytes(size_t size,
428     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
429 extern char* resource_allocate_bytes(Thread* thread, size_t size,
430     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
431 extern char* resource_reallocate_bytes( char *old, size_t old_size, size_t new_size,
432     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
433 extern void resource_free_bytes( Thread* thread, char *old, size_t size );
434 
435 //----------------------------------------------------------------------
436 // Base class for objects allocated in the resource area.
437 class ResourceObj {
438  public:
439   void* operator new(size_t size) {
440     return resource_allocate_bytes(size);
441   }
442 
443   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
444     return resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
445   }
446 
447   void* operator new [](size_t size) throw() = delete;
448   void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() = delete;
449 
450   void  operator delete(void* p) = delete;
451   void  operator delete [](void* p) = delete;
452 };
453 
454 class ArenaObj {
455  public:
456   void* operator new(size_t size, Arena *arena) throw();
457   void* operator new [](size_t size, Arena *arena) throw() = delete;
458 
459   void* operator new [](size_t size) throw() = delete;
460   void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) throw() = delete;
461 
462   void  operator delete(void* p) = delete;
463   void  operator delete [](void* p) = delete;
464 };
465 
466 //----------------------------------------------------------------------
467 // Base class for objects allocated in the resource area per default.
468 // Optionally, objects may be allocated on the C heap with
469 // new (AnyObj::C_HEAP) Foo(...) or in an Arena with new (&arena).
470 // AnyObj's can be allocated within other objects, but don't use
471 // new or delete (allocation_type is unknown).  If new is used to allocate,
472 // use delete to deallocate.
473 class AnyObj {
474  public:
475   enum allocation_type { STACK_OR_EMBEDDED = 0, RESOURCE_AREA, C_HEAP, ARENA, allocation_mask = 0x3 };
476   static void set_allocation_type(address res, allocation_type type) NOT_DEBUG_RETURN;
477 #ifdef ASSERT
478  private:
479   // When this object is allocated on stack the new() operator is not
480   // called but garbage on stack may look like a valid allocation_type.
481   // Store negated 'this' pointer when new() is called to distinguish cases.
482   // Use second array's element for verification value to distinguish garbage.
483   uintptr_t _allocation_t[2];
484   bool is_type_set() const;
485   void initialize_allocation_info();
486  public:
487   allocation_type get_allocation_type() const;
488   bool allocated_on_stack_or_embedded() const { return get_allocation_type() == STACK_OR_EMBEDDED; }
489   bool allocated_on_res_area() const { return get_allocation_type() == RESOURCE_AREA; }
490   bool allocated_on_C_heap()   const { return get_allocation_type() == C_HEAP; }
491   bool allocated_on_arena()    const { return get_allocation_type() == ARENA; }
492 protected:
493   AnyObj(); // default constructor
494   AnyObj(const AnyObj& r); // default copy constructor
495   AnyObj& operator=(const AnyObj& r); // default copy assignment
496   ~AnyObj();
497 #endif // ASSERT
498 
499  public:
500   // CHeap allocations
501   void* operator new(size_t size, MEMFLAGS flags) throw();
502   void* operator new [](size_t size, MEMFLAGS flags) throw() = delete;
503   void* operator new(size_t size, const std::nothrow_t&  nothrow_constant, MEMFLAGS flags) throw();
504   void* operator new [](size_t size, const std::nothrow_t&  nothrow_constant, MEMFLAGS flags) throw() = delete;
505 
506   // Arena allocations
507   void* operator new(size_t size, Arena *arena);
508   void* operator new [](size_t size, Arena *arena) = delete;
509 
510   // Resource allocations
511   void* operator new(size_t size) {
512     address res = (address)resource_allocate_bytes(size);
513     DEBUG_ONLY(set_allocation_type(res, RESOURCE_AREA);)
514     return res;
515   }
516   void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
517     address res = (address)resource_allocate_bytes(size, AllocFailStrategy::RETURN_NULL);
518     DEBUG_ONLY(if (res != nullptr) set_allocation_type(res, RESOURCE_AREA);)
519     return res;
520   }
521 
522   void* operator new [](size_t size) = delete;
523   void* operator new [](size_t size, const std::nothrow_t& nothrow_constant) = delete;
524   void  operator delete(void* p);
525   void  operator delete [](void* p) = delete;
526 
527 #ifndef PRODUCT
528   // Printing support
529   void print() const;
530   virtual void print_on(outputStream* st) const;
531 #endif // PRODUCT
532 };
533 
534 // One of the following macros must be used when allocating an array
535 // or object to determine whether it should reside in the C heap on in
536 // the resource area.
537 
538 #define NEW_RESOURCE_ARRAY(type, size)\
539   (type*) resource_allocate_bytes((size) * sizeof(type))
540 
541 #define NEW_RESOURCE_ARRAY_RETURN_NULL(type, size)\
542   (type*) resource_allocate_bytes((size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
543 
544 #define NEW_RESOURCE_ARRAY_IN_THREAD(thread, type, size)\
545   (type*) resource_allocate_bytes(thread, (size) * sizeof(type))
546 
547 #define NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(thread, type, size)\
548   (type*) resource_allocate_bytes(thread, (size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
549 
550 #define REALLOC_RESOURCE_ARRAY(type, old, old_size, new_size)\
551   (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type), (new_size) * sizeof(type))
552 
553 #define REALLOC_RESOURCE_ARRAY_RETURN_NULL(type, old, old_size, new_size)\
554   (type*) resource_reallocate_bytes((char*)(old), (old_size) * sizeof(type),\
555                                     (new_size) * sizeof(type), AllocFailStrategy::RETURN_NULL)
556 
557 #define FREE_RESOURCE_ARRAY(type, old, size)\
558   resource_free_bytes(Thread::current(), (char*)(old), (size) * sizeof(type))
559 
560 #define FREE_RESOURCE_ARRAY_IN_THREAD(thread, type, old, size)\
561   resource_free_bytes(thread, (char*)(old), (size) * sizeof(type))
562 
563 #define FREE_FAST(old)\
564     /* nop */
565 
566 #define NEW_RESOURCE_OBJ(type)\
567   NEW_RESOURCE_ARRAY(type, 1)
568 
569 #define NEW_RESOURCE_OBJ_RETURN_NULL(type)\
570   NEW_RESOURCE_ARRAY_RETURN_NULL(type, 1)
571 
572 #define NEW_C_HEAP_ARRAY3(type, size, memflags, pc, allocfail)\
573   (type*) AllocateHeap((size) * sizeof(type), memflags, pc, allocfail)
574 
575 #define NEW_C_HEAP_ARRAY2(type, size, memflags, pc)\
576   (type*) (AllocateHeap((size) * sizeof(type), memflags, pc))
577 
578 #define NEW_C_HEAP_ARRAY(type, size, memflags)\
579   (type*) (AllocateHeap((size) * sizeof(type), memflags))
580 
581 #define NEW_C_HEAP_ARRAY2_RETURN_NULL(type, size, memflags, pc)\
582   NEW_C_HEAP_ARRAY3(type, (size), memflags, pc, AllocFailStrategy::RETURN_NULL)
583 
584 #define NEW_C_HEAP_ARRAY_RETURN_NULL(type, size, memflags)\
585   NEW_C_HEAP_ARRAY2(type, (size), memflags, AllocFailStrategy::RETURN_NULL)
586 
587 #define REALLOC_C_HEAP_ARRAY(type, old, size, memflags)\
588   (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags))
589 
590 #define REALLOC_C_HEAP_ARRAY_RETURN_NULL(type, old, size, memflags)\
591   (type*) (ReallocateHeap((char*)(old), (size) * sizeof(type), memflags, AllocFailStrategy::RETURN_NULL))
592 
593 #define FREE_C_HEAP_ARRAY(type, old) \
594   FreeHeap((char*)(old))
595 
596 // allocate type in heap without calling ctor
597 #define NEW_C_HEAP_OBJ(type, memflags)\
598   NEW_C_HEAP_ARRAY(type, 1, memflags)
599 
600 #define NEW_C_HEAP_OBJ_RETURN_NULL(type, memflags)\
601   NEW_C_HEAP_ARRAY_RETURN_NULL(type, 1, memflags)
602 
603 // deallocate obj of type in heap without calling dtor
604 #define FREE_C_HEAP_OBJ(objname)\
605   FreeHeap((char*)objname);
606 
607 
608 //------------------------------ReallocMark---------------------------------
609 // Code which uses REALLOC_RESOURCE_ARRAY should check an associated
610 // ReallocMark, which is declared in the same scope as the reallocated
611 // pointer.  Any operation that could __potentially__ cause a reallocation
612 // should check the ReallocMark.
613 class ReallocMark: public StackObj {
614 protected:
615   NOT_PRODUCT(int _nesting;)
616 
617 public:
618   ReallocMark()   PRODUCT_RETURN;
619   void check()    PRODUCT_RETURN;
620 };
621 
622 // Uses mmapped memory for all allocations. All allocations are initially
623 // zero-filled. No pre-touching.
624 template <class E>
625 class MmapArrayAllocator : public AllStatic {
626  private:
627   static size_t size_for(size_t length);
628 
629  public:
630   static E* allocate_or_null(size_t length, MEMFLAGS flags);
631   static E* allocate(size_t length, MEMFLAGS flags);
632   static void free(E* addr, size_t length);
633 };
634 
635 // Uses malloc:ed memory for all allocations.
636 template <class E>
637 class MallocArrayAllocator : public AllStatic {
638  public:
639   static size_t size_for(size_t length);
640 
641   static E* allocate(size_t length, MEMFLAGS flags);
642   static E* reallocate(E* addr, size_t new_length, MEMFLAGS flags);
643   static void free(E* addr);
644 };
645 
646 #endif // SHARE_MEMORY_ALLOCATION_HPP