blob: 0e5869141fd3d9b7f34464197e52f446917d1dc0 [file] [log] [blame]
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001//===- MemAlloc.h - Memory allocation functions -----------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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 Scullcdfcccc2018-10-05 20:58:37 +01006//
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
23namespace llvm {
24
25LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
26 void *Result = std::malloc(Sz);
Andrew Walbran3d2c1972020-04-07 12:24:26 +010027 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 Scullcdfcccc2018-10-05 20:58:37 +010033 report_bad_alloc_error("Allocation failed");
Andrew Walbran3d2c1972020-04-07 12:24:26 +010034 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010035 return Result;
36}
37
38LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
39 size_t Sz) {
40 void *Result = std::calloc(Count, Sz);
Andrew Walbran3d2c1972020-04-07 12:24:26 +010041 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 Scullcdfcccc2018-10-05 20:58:37 +010047 report_bad_alloc_error("Allocation failed");
Andrew Walbran3d2c1972020-04-07 12:24:26 +010048 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010049 return Result;
50}
51
52LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
53 void *Result = std::realloc(Ptr, Sz);
Andrew Walbran3d2c1972020-04-07 12:24:26 +010054 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 Scullcdfcccc2018-10-05 20:58:37 +010060 report_bad_alloc_error("Allocation failed");
Andrew Walbran3d2c1972020-04-07 12:24:26 +010061 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010062 return Result;
63}
64
65}
66#endif