WPILibC++  2020.3.2
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 //===----------------------------------------------------------------------===//
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef WPIUTIL_WPI_MEMALLOC_H
18 #define WPIUTIL_WPI_MEMALLOC_H
19 
20 #include "wpi/Compiler.h"
21 #include "wpi/ErrorHandling.h"
22 #include <cstdlib>
23 
24 namespace wpi {
25 
26 #ifdef _WIN32
27 #pragma warning(push)
28 // Warning on NONNULL, report is not known to abort
29 #pragma warning(disable : 6387)
30 #pragma warning(disable : 28196)
31 #pragma warning(disable : 28183)
32 #endif
33 
34 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_malloc(size_t Sz) {
35  void *Result = std::malloc(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_calloc(size_t Count,
42  size_t Sz) {
43  void *Result = std::calloc(Count, Sz);
44  if (Result == nullptr)
45  report_bad_alloc_error("Allocation failed");
46  return Result;
47 }
48 
49 LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
50  void *Result = std::realloc(Ptr, Sz);
51  if (Result == nullptr)
52  report_bad_alloc_error("Allocation failed");
53  return Result;
54 }
55 
56 #ifdef _WIN32
57 #pragma warning(pop)
58 #endif
59 
60 }
61 #endif
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
wpi::report_bad_alloc_error
void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.