LLVM  8.0.1
MemAlloc.h
Go to the documentation of this file.
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"
22 #include <cstdlib>
23 
24 namespace llvm {
25 
27  void *Result = std::malloc(Sz);
28  if (Result == nullptr)
29  report_bad_alloc_error("Allocation failed");
30  return Result;
31 }
32 
33 LLVM_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 
41 LLVM_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
This class represents lattice values for constants.
Definition: AllocatorList.h:24
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_realloc(void *Ptr, size_t Sz)
Definition: MemAlloc.h:41
void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:26
#define LLVM_ATTRIBUTE_RETURNS_NONNULL
Definition: Compiler.h:230
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_calloc(size_t Count, size_t Sz)
Definition: MemAlloc.h:33