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