1//===- MemAlloc.h - Memory allocation functions -----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
11/// This file defines counterparts of C library allocation functions defined in
12/// the namespace 'std'. The new allocation functions crash on allocation
13/// failure instead of returning null pointer.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_SUPPORT_MEMALLOC_H
18#define LLVM_SUPPORT_MEMALLOC_H
19
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/ErrorHandling.h"
22#include <cstdlib>
23
24namespace llvm {
25
26LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
27 void *Result = std::malloc(Sz);
28 if (Result == nullptr)
29 report_bad_alloc_error("Allocation failed");
30 return Result;
31}
32
33LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
34 size_t Sz) {
35 void *Result = std::calloc(Count, Sz);
36 if (Result == nullptr)
37 report_bad_alloc_error("Allocation failed");
38 return Result;
39}
40
41LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
42 void *Result = std::realloc(Ptr, Sz);
43 if (Result == nullptr)
44 report_bad_alloc_error("Allocation failed");
45 return Result;
46}
47
48}
49#endif
50