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