WPILibC++ 2024.3.2
MemAlloc.h
Go to the documentation of this file.
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 WPIUTIL_WPI_MEMALLOC_H
17#define WPIUTIL_WPI_MEMALLOC_H
18
19#include "wpi/Compiler.h"
20#include "wpi/ErrorHandling.h"
21#include <cstdlib>
22
23namespace wpi {
24
25#ifdef _WIN32
26#pragma warning(push)
27// Warning on NONNULL, report is not known to abort
28#pragma warning(disable : 6387)
29#pragma warning(disable : 28196)
30#pragma warning(disable : 28183)
31#endif
32
34 void *Result = std::malloc(Sz);
35 if (Result == nullptr) {
36 // It is implementation-defined whether allocation occurs if the space
37 // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
38 // non-zero, if the space requested was zero.
39 if (Sz == 0)
40 return safe_malloc(1);
41 report_bad_alloc_error("Allocation failed");
42 }
43 return Result;
44}
45
47 size_t Sz) {
48 void *Result = std::calloc(Count, Sz);
49 if (Result == nullptr) {
50 // It is implementation-defined whether allocation occurs if the space
51 // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
52 // non-zero, if the space requested was zero.
53 if (Count == 0 || Sz == 0)
54 return safe_malloc(1);
55 report_bad_alloc_error("Allocation failed");
56 }
57 return Result;
58}
59
60LLVM_ATTRIBUTE_RETURNS_NONNULL inline void *safe_realloc(void *Ptr, size_t Sz) {
61 void *Result = std::realloc(Ptr, Sz);
62 if (Result == nullptr) {
63 // It is implementation-defined whether allocation occurs if the space
64 // requested is zero (ISO/IEC 9899:2018 7.22.3). Retry, requesting
65 // non-zero, if the space requested was zero.
66 if (Sz == 0)
67 return safe_malloc(1);
68 report_bad_alloc_error("Allocation failed");
69 }
70 return Result;
71}
72
73/// Allocate a buffer of memory with the given size and alignment.
74///
75/// When the compiler supports aligned operator new, this will use it to to
76/// handle even over-aligned allocations.
77///
78/// However, this doesn't make any attempt to leverage the fancier techniques
79/// like posix_memalign due to portability. It is mostly intended to allow
80/// compatibility with platforms that, after aligned allocation was added, use
81/// reduced default alignment.
83allocate_buffer(size_t Size, size_t Alignment);
84
85/// Deallocate a buffer of memory with the given size and alignment.
86///
87/// If supported, this will used the sized delete operator. Also if supported,
88/// this will pass the alignment to the delete operator.
89///
90/// The pointer must have been allocated with the corresponding new operator,
91/// most likely using the above helper.
92void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment);
93
94} // namespace wpi
95
96#ifdef _WIN32
97#pragma warning(pop)
98#endif
99
100#endif
#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
\macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a pointer that does not al...
Definition: Compiler.h:289
#define LLVM_ATTRIBUTE_RETURNS_NONNULL
Definition: Compiler.h:277
Definition: ntcore_cpp.h:26
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:33
void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment)
Deallocate a buffer of memory with the given size and alignment.
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_realloc(void *Ptr, size_t Sz)
Definition: MemAlloc.h:60
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_calloc(size_t Count, size_t Sz)
Definition: MemAlloc.h:46
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * allocate_buffer(size_t Size, size_t Alignment)
Allocate a buffer of memory with the given size and alignment.