LLVM 24.0.0git
MemAlloc.cpp
Go to the documentation of this file.
1//===- MemAlloc.cpp - Memory allocation functions -------------------------===//
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
10#include <new>
11
12// These are out of line to have __cpp_aligned_new not affect ABI.
13
15llvm::allocate_buffer(size_t Size, size_t Alignment) {
16 void *Result;
17#ifdef __cpp_aligned_new
18 // Aligned allocation is slower, so only use it when it's needed.
19 if (Alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
20 Result = ::operator new(Size, std::align_val_t(Alignment), std::nothrow);
21 else
22#endif
23 Result = ::operator new(Size, std::nothrow);
24 if (Result == nullptr) {
25 report_bad_alloc_error("Buffer allocation failed");
26 }
27 return Result;
28}
29
30void llvm::deallocate_buffer(void *Ptr, size_t Size, size_t Alignment) {
31#ifdef __cpp_aligned_new
32 if (Alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__) {
33 ::operator delete(Ptr,
34#ifdef __cpp_sized_deallocation
35 Size,
36#endif
37 std::align_val_t(Alignment));
38 return;
39 }
40#endif
41 ::operator delete(Ptr
42#ifdef __cpp_sized_deallocation
43 ,
44 Size
45#endif
46 );
47}
#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:420
#define LLVM_ATTRIBUTE_RETURNS_NONNULL
Definition Compiler.h:402
This file defines counterparts of C library allocation functions defined in the namespace 'std'.
LLVM_ABI 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.
Definition MemAlloc.cpp:15
LLVM_ABI void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment)
Deallocate a buffer of memory with the given size and alignment.
Definition MemAlloc.cpp:30
LLVM_ABI void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.