Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Support/MemAlloc.h
Line
Count
Source (jump to first uncovered line)
1
//===- MemAlloc.h - Memory allocation functions -----------------*- C++ -*-===//
2
//
3
// 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
6
//
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
23
namespace llvm {
24
25
499M
LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
26
499M
  void *Result = std::malloc(Sz);
27
499M
  if (Result == nullptr) {
28
0
    // It is implementation-defined whether allocation occurs if the space
29
0
    // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
30
0
    // non-zero, if the space requested was zero.
31
0
    if (Sz == 0)
32
0
      return safe_malloc(1);
33
0
    report_bad_alloc_error("Allocation failed");
34
0
  }
35
499M
  return Result;
36
499M
}
37
38
LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_calloc(size_t Count,
39
25.5M
                                                        size_t Sz) {
40
25.5M
  void *Result = std::calloc(Count, Sz);
41
25.5M
  if (Result == nullptr) {
42
0
    // It is implementation-defined whether allocation occurs if the space
43
0
    // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
44
0
    // non-zero, if the space requested was zero.
45
0
    if (Count == 0 || Sz == 0)
46
0
      return safe_malloc(1);
47
0
    report_bad_alloc_error("Allocation failed");
48
0
  }
49
25.5M
  return Result;
50
25.5M
}
51
52
63.4M
LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
53
63.4M
  void *Result = std::realloc(Ptr, Sz);
54
63.4M
  if (Result == nullptr) {
55
0
    // It is implementation-defined whether allocation occurs if the space
56
0
    // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
57
0
    // non-zero, if the space requested was zero.
58
0
    if (Sz == 0)
59
0
      return safe_malloc(1);
60
0
    report_bad_alloc_error("Allocation failed");
61
0
  }
62
63.4M
  return Result;
63
63.4M
}
64
65
}
66
#endif