Line data Source code
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 :
24 : namespace llvm {
25 :
26 557081428 : LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
27 557081428 : void *Result = std::malloc(Sz);
28 557081428 : if (Result == nullptr)
29 0 : report_bad_alloc_error("Allocation failed");
30 557081428 : return Result;
31 : }
32 :
33 20209048 : LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
34 : size_t Sz) {
35 20209048 : void *Result = std::calloc(Count, Sz);
36 20209048 : if (Result == nullptr)
37 0 : report_bad_alloc_error("Allocation failed");
38 20209048 : return Result;
39 : }
40 :
41 55377243 : LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
42 55377243 : void *Result = std::realloc(Ptr, Sz);
43 55377243 : if (Result == nullptr)
44 0 : report_bad_alloc_error("Allocation failed");
45 55377243 : return Result;
46 : }
47 :
48 : }
49 : #endif
|