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