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
9#include "llvm/Support/MemAlloc.h"
10#include <new>
11
12// These are out of line to have __cpp_aligned_new not affect ABI.
13
14LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void *
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(Reason: "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}
48