1336809Sdim//===- MemAlloc.h - Memory allocation functions -----------------*- C++ -*-===//
2336809Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6336809Sdim//
7336809Sdim//===----------------------------------------------------------------------===//
8336809Sdim/// \file
9336809Sdim///
10336809Sdim/// This file defines counterparts of C library allocation functions defined in
11336809Sdim/// the namespace 'std'. The new allocation functions crash on allocation
12336809Sdim/// failure instead of returning null pointer.
13336809Sdim///
14336809Sdim//===----------------------------------------------------------------------===//
15336809Sdim
16336809Sdim#ifndef LLVM_SUPPORT_MEMALLOC_H
17336809Sdim#define LLVM_SUPPORT_MEMALLOC_H
18336809Sdim
19336809Sdim#include "llvm/Support/Compiler.h"
20336809Sdim#include "llvm/Support/ErrorHandling.h"
21336809Sdim#include <cstdlib>
22336809Sdim
23336809Sdimnamespace llvm {
24336809Sdim
25336809SdimLLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
26336809Sdim  void *Result = std::malloc(Sz);
27353358Sdim  if (Result == nullptr) {
28353358Sdim    // It is implementation-defined whether allocation occurs if the space
29353358Sdim    // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
30353358Sdim    // non-zero, if the space requested was zero.
31353358Sdim    if (Sz == 0)
32353358Sdim      return safe_malloc(1);
33336809Sdim    report_bad_alloc_error("Allocation failed");
34353358Sdim  }
35336809Sdim  return Result;
36336809Sdim}
37336809Sdim
38336809SdimLLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
39336809Sdim                                                        size_t Sz) {
40336809Sdim  void *Result = std::calloc(Count, Sz);
41353358Sdim  if (Result == nullptr) {
42353358Sdim    // It is implementation-defined whether allocation occurs if the space
43353358Sdim    // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
44353358Sdim    // non-zero, if the space requested was zero.
45353358Sdim    if (Count == 0 || Sz == 0)
46353358Sdim      return safe_malloc(1);
47336809Sdim    report_bad_alloc_error("Allocation failed");
48353358Sdim  }
49336809Sdim  return Result;
50336809Sdim}
51336809Sdim
52336809SdimLLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
53336809Sdim  void *Result = std::realloc(Ptr, Sz);
54353358Sdim  if (Result == nullptr) {
55353358Sdim    // It is implementation-defined whether allocation occurs if the space
56353358Sdim    // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
57353358Sdim    // non-zero, if the space requested was zero.
58353358Sdim    if (Sz == 0)
59353358Sdim      return safe_malloc(1);
60336809Sdim    report_bad_alloc_error("Allocation failed");
61353358Sdim  }
62336809Sdim  return Result;
63336809Sdim}
64336809Sdim
65336809Sdim}
66336809Sdim#endif
67