1//===-- runtime/memory.cpp ------------------------------------------------===//
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 "flang/Runtime/memory.h"
10#include "terminator.h"
11#include "tools.h"
12#include <cstdlib>
13
14namespace Fortran::runtime {
15RT_OFFLOAD_VAR_GROUP_BEGIN
16
17RT_API_ATTRS void *AllocateMemoryOrCrash(
18 const Terminator &terminator, std::size_t bytes) {
19 if (void *p{std::malloc(size: bytes)}) {
20 return p;
21 }
22 if (bytes > 0) {
23 terminator.Crash(
24 "Fortran runtime internal error: out of memory, needed %zd bytes",
25 bytes);
26 }
27 return nullptr;
28}
29
30RT_API_ATTRS void *ReallocateMemoryOrCrash(
31 const Terminator &terminator, void *ptr, std::size_t newByteSize) {
32 if (void *p{Fortran::runtime::realloc(ptr: ptr, size: newByteSize)}) {
33 return p;
34 }
35 if (newByteSize > 0) {
36 terminator.Crash("Fortran runtime internal error: memory realloc returned "
37 "null, needed %zd bytes",
38 newByteSize);
39 }
40 return nullptr;
41}
42
43RT_API_ATTRS void FreeMemory(void *p) { std::free(ptr: p); }
44
45RT_OFFLOAD_VAR_GROUP_END
46} // namespace Fortran::runtime
47

source code of flang/runtime/memory.cpp