Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:32

0001 //===- MemAlloc.h - Memory allocation functions -----------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// This file defines counterparts of C library allocation functions defined in
0011 /// the namespace 'std'. The new allocation functions crash on allocation
0012 /// failure instead of returning null pointer.
0013 ///
0014 //===----------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_SUPPORT_MEMALLOC_H
0017 #define LLVM_SUPPORT_MEMALLOC_H
0018 
0019 #include "llvm/Support/Compiler.h"
0020 #include "llvm/Support/ErrorHandling.h"
0021 #include <cstdlib>
0022 
0023 namespace llvm {
0024 
0025 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
0026   void *Result = std::malloc(Sz);
0027   if (Result == nullptr) {
0028     // It is implementation-defined whether allocation occurs if the space
0029     // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
0030     // non-zero, if the space requested was zero.
0031     if (Sz == 0)
0032       return safe_malloc(1);
0033     report_bad_alloc_error("Allocation failed");
0034   }
0035   return Result;
0036 }
0037 
0038 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
0039                                                         size_t Sz) {
0040   void *Result = std::calloc(Count, Sz);
0041   if (Result == nullptr) {
0042     // It is implementation-defined whether allocation occurs if the space
0043     // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
0044     // non-zero, if the space requested was zero.
0045     if (Count == 0 || Sz == 0)
0046       return safe_malloc(1);
0047     report_bad_alloc_error("Allocation failed");
0048   }
0049   return Result;
0050 }
0051 
0052 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
0053   void *Result = std::realloc(Ptr, Sz);
0054   if (Result == nullptr) {
0055     // It is implementation-defined whether allocation occurs if the space
0056     // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
0057     // non-zero, if the space requested was zero.
0058     if (Sz == 0)
0059       return safe_malloc(1);
0060     report_bad_alloc_error("Allocation failed");
0061   }
0062   return Result;
0063 }
0064 
0065 /// Allocate a buffer of memory with the given size and alignment.
0066 ///
0067 /// When the compiler supports aligned operator new, this will use it to
0068 /// handle even over-aligned allocations.
0069 ///
0070 /// However, this doesn't make any attempt to leverage the fancier techniques
0071 /// like posix_memalign due to portability. It is mostly intended to allow
0072 /// compatibility with platforms that, after aligned allocation was added, use
0073 /// reduced default alignment.
0074 LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void *
0075 allocate_buffer(size_t Size, size_t Alignment);
0076 
0077 /// Deallocate a buffer of memory with the given size and alignment.
0078 ///
0079 /// If supported, this will used the sized delete operator. Also if supported,
0080 /// this will pass the alignment to the delete operator.
0081 ///
0082 /// The pointer must have been allocated with the corresponding new operator,
0083 /// most likely using the above helper.
0084 void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment);
0085 
0086 } // namespace llvm
0087 #endif