Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 1 | //===- MemAlloc.h - Memory allocation functions -----------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// |
| 10 | /// This file defines counterparts of C library allocation functions defined in |
| 11 | /// the namespace 'std'. The new allocation functions crash on allocation |
| 12 | /// failure instead of returning null pointer. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_SUPPORT_MEMALLOC_H |
| 17 | #define LLVM_SUPPORT_MEMALLOC_H |
| 18 | |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include "llvm/Support/ErrorHandling.h" |
| 21 | #include <cstdlib> |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) { |
| 26 | void *Result = std::malloc(Sz); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 27 | if (Result == nullptr) { |
| 28 | // It is implementation-defined whether allocation occurs if the space |
| 29 | // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting |
| 30 | // non-zero, if the space requested was zero. |
| 31 | if (Sz == 0) |
| 32 | return safe_malloc(1); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 33 | report_bad_alloc_error("Allocation failed"); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 34 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 35 | return Result; |
| 36 | } |
| 37 | |
| 38 | LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count, |
| 39 | size_t Sz) { |
| 40 | void *Result = std::calloc(Count, Sz); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 41 | if (Result == nullptr) { |
| 42 | // It is implementation-defined whether allocation occurs if the space |
| 43 | // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting |
| 44 | // non-zero, if the space requested was zero. |
| 45 | if (Count == 0 || Sz == 0) |
| 46 | return safe_malloc(1); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 47 | report_bad_alloc_error("Allocation failed"); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 48 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 49 | return Result; |
| 50 | } |
| 51 | |
| 52 | LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) { |
| 53 | void *Result = std::realloc(Ptr, Sz); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 54 | if (Result == nullptr) { |
| 55 | // It is implementation-defined whether allocation occurs if the space |
| 56 | // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting |
| 57 | // non-zero, if the space requested was zero. |
| 58 | if (Sz == 0) |
| 59 | return safe_malloc(1); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 60 | report_bad_alloc_error("Allocation failed"); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 61 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 62 | return Result; |
| 63 | } |
| 64 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 65 | /// Allocate a buffer of memory with the given size and alignment. |
| 66 | /// |
| 67 | /// When the compiler supports aligned operator new, this will use it to to |
| 68 | /// handle even over-aligned allocations. |
| 69 | /// |
| 70 | /// However, this doesn't make any attempt to leverage the fancier techniques |
| 71 | /// like posix_memalign due to portability. It is mostly intended to allow |
| 72 | /// compatibility with platforms that, after aligned allocation was added, use |
| 73 | /// reduced default alignment. |
| 74 | LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * |
| 75 | allocate_buffer(size_t Size, size_t Alignment); |
| 76 | |
| 77 | /// Deallocate a buffer of memory with the given size and alignment. |
| 78 | /// |
| 79 | /// If supported, this will used the sized delete operator. Also if supported, |
| 80 | /// this will pass the alignment to the delete operator. |
| 81 | /// |
| 82 | /// The pointer must have been allocated with the corresponding new operator, |
| 83 | /// most likely using the above helper. |
| 84 | void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment); |
| 85 | |
| 86 | } // namespace llvm |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 87 | #endif |